-
-
Notifications
You must be signed in to change notification settings - Fork 890
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
SocketIO server refuses to shutdown with Ctrl+C (sometimes) #199
Comments
No need to monkey patch. There is a bug in Werkzeug's handling of the Ctrl-C signal when in multithreaded mode that I've also noticed, but haven't had time to characterize and submit as a bug to that project yet. The bug is present only when using multiple threads, which is a requirement of this extension. If you install eventlet or gevent you will bypass the issue, as Werkzeug is not used in those cases. I'll leave this bug open as a reminder to investigate the problem. |
Thanks for the quick response @miguelgrinberg I tried it with
Tried it with
|
@lukeyeager I think I see the eventlet problem in their code. Take a look here: https://github.com/eventlet/eventlet/blob/503c584a7d30d7ced1509df2c0c9262be1157589/eventlet/wsgi.py#L864. They catch the first Ctrl-C, print "wsgi exiting" and then keep going, without allowing the interrupt exception to bubble up the stack. That's why you have to hit Ctrl-C again to really break the app. I'm not sure why these frameworks appear a bit unpolished in many ways. I also have a lot of issues with clients closing their browsers without properly closing the socket. That also generates all sorts of errors that IMHO these frameworks should suppress. |
Two But I can't get either |
I resolved #203 with your help - thanks! Monkey patching the std libs didn't change the shutdown behavior in any way (which is probably to be expected). |
I also came across this issue. What's the state of it? Is there a fix or possible work-around in sight? |
The workaround is to hit Ctrl-C twice, and if that doesn't work, to kill the web server. Unfortunately this isn't a bug on this side. Handing these signals is tricky, so many of the web servers I support have this problem. |
M'Key. Hitting Ctrl-C twice is no solution as my server runs inside a docker container and |
@roba91 if you have a specific use case that is preventing you from doing what you need, I recommend that you take the issue to the web server project you are using. I've seen this problem with eventlet, gevent, Werkzeug and more recently asyncio based servers, so it is widespread. |
Reading https://github.com/eventlet/eventlet/blob/503c584a7d30d7ced1509df2c0c9262be1157589/eventlet/wsgi.py#L867 it seems that eventlet waits for all green threads to finish. Thus, I assume that Flask-SocketIO or python-socketio or so has some threads that stay alive. UPDATE: |
@roba91 the mistake in eventlet's case lies (I think) in assuming that all greenlets are going to exit promply. That works more or less okay for HTTP, but when you have a WebSocket connection you have a greenlet that is not going to end unless it's told to, or the other side closes the connection. If I wanted to properly handle Ctrl-C from my side, I would need to catch the I still think there should be better handling from eventlet's side though. At least there should be an official mechanism to give applications the chance to clean up before exiting. |
Hello guys. It should close idle HTTP connections promptly. Please suggest how can I help with websockets. AFAIK there is no idle/busy state for them. |
@temoto When I implemented websocket support in sanic I had the same problem. The way I solved it, is by keeping track of all the active websocket tasks, and then cancel them as part of the exit signal handler. Here is the specific commit that does this: miguelgrinberg/sanic@fd823c6. Would the same approach work for eventlet? This is, I think, a good approach, because if the websocket handler function does not care, then the task cancelled exception can be caught by the framework, but when the application needs to do some cleanup, it can catch the exception to do so before exiting. |
@miguelgrinberg if I understood correctly, that's raising custom cancel exception in every running task. Thank you for sharing. Seems quite applicable here too. |
Yes, that's correct. Asyncio will raise |
While this is a problem, it is often an issue in the web servers and not in this package. I'm going to close as won't fix for now and then look at individual issues that may be caused in this package if they reappear. |
I sometimes run into problems when I want to shut down Flask-SocketIO. The server intermittently takes several seconds to respond to Ctrl+c. I stumbled across the magic incantation for one application, but now I'm working on a little demo app and am running into the same type of issue I've seen before.
Full source code - https://github.com/lukeyeager/flask-sqlalchemy-socketio-demo/blob/6cc45a2fb7/main.py#L64
Issue with more details - lukeyeager/flask-sqlalchemy-socketio-demo#2
I'm not using Gevent or Eventlet - so I shouldn't need monkey-patching, right?
Thanks for the help!
The text was updated successfully, but these errors were encountered: