Draft: Cancel cleanup tasks before stopping the main loop
Description
This makes two changes to facilitate a more orderly shutdown of cleanup when it receives a signal:
- Check for the
asyncio.CancelledError
exception and break out of the cleanup loop if it's raised - Cancel each individual cleanup task in
stop
, which is what gets called onSIGTERM
, before callingself._main_loop.stop()
.
This should make cancellations more reliable and prevent errors like RuntimeError('Event loop stopped before Future completed.')
, which I've had happen in some cases.