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.
This commit is contained in:
Lasse Blaauwbroek 2023-10-15 14:55:57 +02:00 committed by Jacob Alexander
parent 09f7cd0d08
commit ca8d120901

View file

@ -2502,10 +2502,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