flask socket-io, sometimes client calls freeze the server
<p>I occasionally have a problem with flask socket-io freezing, and I have no clue how to fix it.</p>

<p>My client connects to my socket-io server and performs some chat sessions. It works nicely. But for some reason, sometimes from the client side, there is some call that blocks the whole server (The server is stuck in the process, and all other calls are frozen). What is strange is that the server can be blocked as long as the client side app is not totally shutdown.This is an ios-app / web page, and I must totally close the app or the safari page. Closing the socket itself, and even deallocating it doesn't resolve the problem. When the app is in the background, the sockets are closed and deallocated but the problem persists.</p>

<p>This is a small server, and it deals with both html pages and the socket-server so I have no idea if it is the socket itself or the html that blocks the process. But each time the server was freezing, the log showed some socket calls.</p>

<p>Here is how I configured my server:</p>

<pre><code>socketio = SocketIO(app, ping_timeout=5)
socketio.run(app, host='0.0.0.0', port=5001, debug=True, ssl_context=context)
</code></pre>

<p>So my question is:
What can freeze the server (this seems to happen when I leave the app or web-site open for a long time while doing nothing). If I use the services normally the server never freezes. And how I can prevent it from happening (Even if I don't know what causing this, is there a way to blindly stop my server from being stuck at a call? </p>

<p>Thanks you for the answers</p>

<p>According to your comment above, you are using the Flask development web server, without the help of an asynchronous framework such as eventlet or gevent. Besides this option being highly inefficient, you should know that this web server is not battle tested, it is meant for short lived tests during development. I'm not sure it is able to run for very long, specially under the unusual conditions Flask-SocketIO puts it through, which regular Flask apps do not exercise. I think it is quite possible that you are hitting some obscure bug in Werkzeug that causes it to hang.</p>

<p>My recommendation is that you install eventlet and try again. All you need to do is <code>pip install eventlet</code>, and assuming you are not passing an explicit <code>async_mode</code> argument, then just by installing this package Flask-SocketIO should configure itself to use it.</p>

<p>I would also remove the explicit timeout setting. In almost all cases, the defaults are sufficient to maintain a healthy connection.</p>

python freeze flask-socketio