-
-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to safely make sure that main() exit stops the loop? #91
Comments
Thanks for the detailed report. I will have a look in the next few days when I get a chance. |
Ok, I had a quick look at this. There are two parts to discuss:
Let's deal with them in turn. Interpretation of
|
Agreed! (I need to catch it once, in order to find where the cancellation actually came from, but then I absolutely intend to make it Not Do That.)
Ahh, yes, that makes total sense and gives me exactly the data I need – I can keep track of "we are shutting down now" on the loop or elsewhere. I agree that this makes more sense than asking aiorun to expose that as
Sounds fine to me :) thank you very much for taking the time to look at this! |
Some context: I use
aiorun.run(main(), stop_on_unhandled_errors=True)
to make sure thatmain
crashing will exit the program, because any exception inmain
probably indicates a programmer error (and a service manager can restart the process once it exits).Unfortunately,
main
being cancelled (e.g. because something it awaits was cancelled) is not treated as an "unhandled error", even though from my perspective it's unexpected behaviour and I want the process to exit and be restarted, rather than sitting there doing nothing. So, mymain
looks like this:in an effort to ensure that, no matter what, if
business_logic
doesn't work properly, at least the program will exit.However, this causes a new problem – when I ctrl-C the app, it prints an ugly traceback:
I think the flow of events here is something like:
_shutdown_handler
runs (with a short diversion vialoop.call_soon_threadsafe
) and callsloop.stop()
main
, which hits itsfinally
clause and callsloop.stop()
!main
, and would've completed given a few more event loop iterations)Is there a way I can solve this? How can
main
tell whether it's been cancelled because it was waiting on something that got cancelled (i.e. it must stop the loop) vs because aiorun detected a ctrl-C and is running cancelled tasks to completion (in which case aiorun will stop the loop)?The text was updated successfully, but these errors were encountered: