Skip to content

Commit

Permalink
Handle exceptions from server callbacks
Browse files Browse the repository at this point in the history
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:
python/cpython#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.
  • Loading branch information
LasseBlaauwbroek committed Oct 15, 2023
1 parent cc08821 commit 1a72d3f
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion capnp/lib/capnp.pyx
Expand Up @@ -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

Expand Down

0 comments on commit 1a72d3f

Please sign in to comment.