Skip to content
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

asyncio signal handlers don't work on Windows #108

Closed
veaviticus opened this issue Oct 24, 2016 · 7 comments
Closed

asyncio signal handlers don't work on Windows #108

veaviticus opened this issue Oct 24, 2016 · 7 comments

Comments

@veaviticus
Copy link

Sanic attempts to register signal handlers here:
https://github.com/channelcat/sanic/blob/master/sanic/server.py#L227

But using the asyncio event loop (as is the fallback when uvloop can't be imported) does not implement signal handlers. See https://docs.python.org/3/library/asyncio-eventloops.html#windows where it states this, and the code at https://github.com/python/asyncio/blob/master/asyncio/events.py#L480

Commenting out the signal lines in sanic allows sanic to run on windows (yay) but then you can't Ctrl-C out of it, you must kill the python process

@veaviticus
Copy link
Author

This appears to be the most successful way of handling interrupts while in an event loop.
http://stackoverflow.com/a/24775107/375344

I have it set in my windows Sanic app to issue the call back once per second to process the interrupts, and its working pretty well.

The "fix" that's supposed to be in 3.5 (python/asyncio#191) doesn't work for me

@Badg
Copy link

Badg commented Jan 4, 2017

Windows signal handling in general is a mess. If you just want to catch CTRL+C, you might want to run the loop in a thread and keep the main thread available for an interrupt. This gets messy though.

If you're sending signals from other (non parent/child) processes (ex: running using pythonw.exe and then interacting from a different console), be careful. Without serious effort, all signals sent this way result in insta-death with no cleanup. There's a very small discussion (written by yours truly) of the mess here as well as a workaround strategy (conveniently packaged with the library that the page is documenting).

Pretty much all signal handling I'm personally doing on Windows with asyncio involves either threads and/or daughter processes, which eventually call a separate stop_threadsafe method, which then starts the shutdown process. It's roundabout, but reliable.

@seemethere
Copy link
Member

Until uvloop supports windows and signals aren't all wonky on windows we don't plan to add windows support!

@Tronic
Copy link
Member

Tronic commented Sep 10, 2019

In 2019 this is still broken and the current situation is quite unacceptable. I agree that fixing it is very hard, in particular in the multi-worker mode. Having a single worker crash reliably on Ctrl+C would be a big improvement already.

The Sanic trio branch terminates cleanly on Ctrl+C in single and multiple worker modes, which shouldn't be surprising considering that Trio's author wrote this.

@karman1102
Copy link

In 2019 this is still broken and the current situation is quite unacceptable.

Consumes a lot of time, changing directory and running this all over again in windows cmd. Still an issue

@Tronic
Copy link
Member

Tronic commented May 22, 2020

#1808 which is included in 20.03 should already fix this. Which Sanic version are you using? I've also noticed that some IDEs cannot send Ctrl+C properly. Can't investigate further at this time but it would be good to properly triage this if further changes are required.

@karman1102
Copy link

Thank you so much @Tronic! Had been looking everywhere for a solution, didn't know a minor update would suffice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants