I have the application logic for a connection all wrapped in a big try/except/finally block:
async def serve(self, stream: trio.SocketStream):
try:
async with trio.open_nursery() as nursery:
pass # code goes here to do some reading and writing
except Exception as e:
print("Got exception:", e)
except trio.Cancelled:
print("Cancelled")
raise
# some other handlers, mostly just logging
finally:
# ... some other stuff ...
# ... then shut the connection:
with trio.move_on_after(1):
await stream.aclose()
I'm not clear whether I should try to close the connection in the finally clause. If the exception is due to an application error (invalid message from the connection, or coroutine cancelled) this seems like the right thing to do, but if the exception is that the connection was closed from the other end then this seems counter productive - I expect the old exception would get masked by a new one. Any thoughts? I could maybe wrap it in its own try/except and just ignore any exceptions.
On the python-trio/general Gitter chat, Joshua Oreman @oremanj kindly offered this answer: