aio-libs/aiozmq

asyncio.CancelledError should be caught in "try_log"

nirvana-msu opened this issue · 1 comments

If aiozmp RPC server is terminated with KeyboardInterrupt (e.g. ctrl+c), there is inexplicable mess of asyncio.CancelledError being logged for every pending remote request - instead of replying back to the caller with a serialized exception, like it is the case for any other exception being raised (and then exiting cleanly). This happens because you only catch Exception, but KeyboardInterrupt does not inherit from it.

KeyboardInterrupt should be caught here, in addition to Exception:

except Exception as exc:

and also here:

except Exception as exc:

You'd need to reply to caller (same as on other exceptions) and then re-raise or somehow exit cleanly.

I think you can scratch the above. On a more careful examination, what seems to be missing is catching CancelledError within try_log. As of Python 3.8, CancelledError extends from BaseException and not Exception (discussion) so it is never caught by except Exception. If you catch that and return, there is just one KeyboardInterrupt logged which is what we want.

The only thing I can't figure out is whether it's actually possible to reply to remote callers? It appears that the transport is closed by that point and I get warnings like WARNING: write to closed ZMQ socket., and then exceptions, if I try to send a reply. I guess I'll have to settle for leaving the caller hanging...