From b6ea909e9afec32388d668e2530effd0109d38f1 Mon Sep 17 00:00:00 2001 From: Lasse Blaauwbroek Date: Wed, 8 Nov 2023 18:55:30 +0100 Subject: [PATCH] Corner case for cancelled server methods that raise exceptions When a server method is cancelled, but it nonetheless raises an exception (other than `CancelledError`), this exception cannot be reported to the caller (because it has cancelled that call). The only place where it can go is to the asyncio exception handler... --- capnp/lib/capnp.pyx | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/capnp/lib/capnp.pyx b/capnp/lib/capnp.pyx index 8227d42..8669fe3 100644 --- a/capnp/lib/capnp.pyx +++ b/capnp/lib/capnp.pyx @@ -76,6 +76,15 @@ cdef class _VoidPromiseFulfiller: def void_task_done_callback(method_name, _VoidPromiseFulfiller fulfiller, task): if fulfiller.fulfiller == NULL: + if not task.cancelled(): + exc = task.exception() + if exc is not None: + context = { + 'message': f"Cancelled server method {method_name} raised an exception", + 'exception': exc, + 'task': task, + } + asyncio.get_running_loop().call_exception_handler(context) return if task.cancelled():