'Socket session keeps dying on Gunicorn server using flask-socketio

I have set up a flask-socketio API which is running on a gunicorn server:

socket = SocketIO(app, async_mode=None, cors_allowed_origins="*")

The system service which runs the flask application is as follows:

[Unit]
Description=Gunicorn instance to serve AU api
After=network.target

[Service]
User=worxmanager
Group=www-data
WorkingDirectory=/var/www/au/sockets/api
Environment="PATH=/var/www/au/sockets/env/bin"
ExecStart=/var/www/au/sockets/env/bin/gunicorn --worker-class eventlet -w 1 --bind unix:au.sock -m 007 wsgi:app

[Install]
WantedBy=multi-user.target

The way I route the requests to the sockets is using NGINX:

location / {
    proxy_read_timeout 3600;
    include proxy_params;
    proxy_http_version 1.1;
    proxy_buffering off;
    proxy_pass http://unix:/var/www/au/sockets/api/au.sock;
}

This works fine for smaller payloads but from time to time, I get the following error on the server:

Feb 22 08:17:10 wxm-plastfix gunicorn[14099]:     start_response)
Feb 22 08:17:10 wxm-plastfix gunicorn[14099]:   File "/var/www/au/sockets/env/lib/python3.6/site-packages/engineio/middleware.py", line 60, in __call__
Feb 22 08:17:10 wxm-plastfix gunicorn[14099]:     return self.engineio_app.handle_request(environ, start_response)
Feb 22 08:17:10 wxm-plastfix gunicorn[14099]:   File "/var/www/au/sockets/env/lib/python3.6/site-packages/socketio/server.py", line 571, in handle_request
Feb 22 08:17:10 wxm-plastfix gunicorn[14099]:     return self.eio.handle_request(environ, start_response)
Feb 22 08:17:10 wxm-plastfix gunicorn[14099]:   File "/var/www/au/sockets/env/lib/python3.6/site-packages/engineio/server.py", line 390, in handle_request
Feb 22 08:17:10 wxm-plastfix gunicorn[14099]:     socket = self._get_socket(sid)
Feb 22 08:17:10 wxm-plastfix gunicorn[14099]:   File "/var/www/au/sockets/env/lib/python3.6/site-packages/engineio/server.py", line 612, in _get_socket
Feb 22 08:17:10 wxm-plastfix gunicorn[14099]:     raise KeyError('Session is disconnected')
Feb 22 08:17:10 wxm-plastfix gunicorn[14099]: KeyError: 'Session is disconnected'

and on the front end I get the following error:

Firefox can’t establish a connection to the server at wss://api.sockets.com/socket.io/?EIO=4&transport=websocket&sid=p6D40hzICsPeRdbCAA91.

Any idea how I can solve this issue? Do I need to modify the service file or my SocketIO setup?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source