capnproto / pycapnp

Cap'n Proto serialization/RPC system - Python bindings
BSD 2-Clause "Simplified" License
458 stars 125 forks source link

Handle exceptions from server callbacks #337

Closed LasseBlaauwbroek closed 8 months ago

LasseBlaauwbroek commented 9 months ago

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.

LasseBlaauwbroek commented 8 months ago

There has been no activity on my upstream issue. This current fix seems to be working fine. I propose to merge this. We can amend/change to follow the solution upstream comes up with if and when it becomes available.