From 1a72d3f0f27e5905350a7c7602619e71fcfdaaf4 Mon Sep 17 00:00:00 2001 From: Lasse Blaauwbroek Date: Sun, 15 Oct 2023 14:55:57 +0200 Subject: [PATCH] Handle exceptions from server callbacks In its current form, when a server callback throws an exception, it is completely swallowed. Only when the asyncio loop is being shut down might one possibly see that error. On top of that, the connection is never closed, causing any clients to hang, and a memory leak in the server. This is a proposed fix that reports the exception to the asyncio exception handler. It also makes sure that the connection is always closed, even if the callback doesn't close it explicitly. Note that the design of AsyncIoStream is directly based on the design of Python's asyncio streams: https://docs.python.org/3/library/asyncio-stream.html These streams appear to have exactly the same flaw. I've reported this here: https://github.com/python/cpython/issues/110894. Since I don't really know what I'm doing, it might be worth seeing what kind of solution they might come up with and model our solution after theirs. --- capnp/lib/capnp.pyx | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/capnp/lib/capnp.pyx b/capnp/lib/capnp.pyx index a708846..b17b8bd 100644 --- a/capnp/lib/capnp.pyx +++ b/capnp/lib/capnp.pyx @@ -2475,10 +2475,24 @@ cdef class _PyAsyncIoStreamProtocol(DummyBaseClass, asyncio.BufferedProtocol): self.write_in_progress = False self.read_eof = False self.read_overflow_buffer = bytearray() + def done(task): + if self.transport is not None: + self.transport.close() + exc = task.exception() + if exc is not None: + context = { + 'message': "Exception in pycapnp server callback", + 'exception': exc, + 'task': task, + 'protocol': self, + 'transport': self.transport + } + asyncio.get_running_loop().call_exception_handler(context) if self.connected_callback is not None: callback_res = self.connected_callback(self.callback_arg) if asyncio.iscoroutine(callback_res): - self._task = asyncio.get_running_loop().create_task(callback_res) + self._task = asyncio.create_task(callback_res) + self._task.add_done_callback(done) self.connected_callback = None self.callback_arg = None