Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flask-socket.io: frequent time-outs

I have a flask-socket.io application that is pretty standard:

server: eventlet I start the app using: socketio.run(app, host='0.0.0.0')

Frequently but not always I have some kind of timeout:

Traceback (most recent call last):
  File "/projects/ici_chat_prototype01/env/lib/python3.5/site-packages/eventlet/wsgi.py", line 507, in handle_one_response
    result = self.application(self.environ, start_response)
  File "/projects/ici_chat_prototype01/env/lib/python3.5/site-packages/flask/app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "/projects/ici_chat_prototype01/env/lib/python3.5/site-packages/flask_socketio/__init__.py", line 42, in __call__
    start_response)
  File "/projects/ici_chat_prototype01/env/lib/python3.5/site-packages/engineio/middleware.py", line 47, in __call__
    return self.engineio_app.handle_request(environ, start_response)
  File "/projects/ici_chat_prototype01/env/lib/python3.5/site-packages/socketio/server.py", line 360, in handle_request
    return self.eio.handle_request(environ, start_response)
  File "/projects/ici_chat_prototype01/env/lib/python3.5/site-packages/engineio/server.py", line 267, in handle_request
    environ, start_response)
  File "/projects/ici_chat_prototype01/env/lib/python3.5/site-packages/engineio/socket.py", line 89, in handle_get_request
    start_response)
  File "/projects/ici_chat_prototype01/env/lib/python3.5/site-packages/engineio/socket.py", line 130, in _upgrade_websocket
    return ws(environ, start_response)
  File "/projects/ici_chat_prototype01/env/lib/python3.5/site-packages/engineio/async_eventlet.py", line 19, in __call__
    return super(WebSocketWSGI, self).__call__(environ, start_response)
  File "/projects/ici_chat_prototype01/env/lib/python3.5/site-packages/eventlet/websocket.py", line 127, in __call__
    self.handler(ws)
  File "/projects/ici_chat_prototype01/env/lib/python3.5/site-packages/engineio/socket.py", line 155, in _websocket_handler
    pkt = ws.wait()
  File "/projects/ici_chat_prototype01/env/lib/python3.5/site-packages/eventlet/websocket.py", line 633, in wait
    for i in self.iterator:
  File "/projects/ici_chat_prototype01/env/lib/python3.5/site-packages/eventlet/websocket.py", line 503, in _iter_frames
    message = self._recv_frame(message=fragmented_message)
  File "/projects/ici_chat_prototype01/env/lib/python3.5/site-packages/eventlet/websocket.py", line 526, in _recv_frame
    header = recv(2)
  File "/projects/ici_chat_prototype01/env/lib/python3.5/site-packages/eventlet/websocket.py", line 442, in _get_bytes
    d = self.socket.recv(numbytes - len(data))
  File "/projects/ici_chat_prototype01/env/lib/python3.5/site-packages/eventlet/greenio/base.py", line 360, in recv
    return self._recv_loop(self.fd.recv, b'', bufsize, flags)
  File "/projects/ici_chat_prototype01/env/lib/python3.5/site-packages/eventlet/greenio/base.py", line 354, in _recv_loop
    self._read_trampoline()
  File "/projects/ici_chat_prototype01/env/lib/python3.5/site-packages/eventlet/greenio/base.py", line 325, in _read_trampoline
    timeout_exc=socket_timeout('timed out'))
  File "/projects/ici_chat_prototype01/env/lib/python3.5/site-packages/eventlet/greenio/base.py", line 207, in _trampoline
    mark_as_closed=self._mark_as_closed)
  File "/projects/ici_chat_prototype01/env/lib/python3.5/site-packages/eventlet/hubs/__init__.py", line 163, in trampoline
    return hub.switch()
  File "/projects/ici_chat_prototype01/env/lib/python3.5/site-packages/eventlet/hubs/hub.py", line 295, in switch
    return self.greenlet.switch()
socket.timeout: timed out

I am not able to interpret this traceback. Can somebody with some experience in flask-socket.io help?

I am not posting any code because I would not know where to start. All files in the traceback are from the installed modules.

EDIT: I got some more info on the socket.io requests. After the above Exception the following requests are logged:

127.0.0.1 - - [04/Jan/2018 10:10:51] "GET /socket.io/?EIO=3&transport=websocket&sid=f93955151a3a4576b2e96427cc27121e HTTP/1.1" 500 0 60.061493
127.0.0.1 - - [04/Jan/2018 10:10:51] "GET /socket.io/?EIO=3&transport=polling&t=1515056991349-3&sid=f93955151a3a4576b2e96427cc27121e HTTP/1.1" 400 218 60.001593
127.0.0.1 - - [04/Jan/2018 10:10:52] "GET /socket.io/?EIO=3&transport=polling&t=1515057052758-4 HTTP/1.1" 200 381 0.000875
(12472) accepted ('127.0.0.1', 39520) 
127.0.0.1 - - [04/Jan/2018 10:10:52] "POST /socket.io/?EIO=3&transport=polling&t=1515057052767-5&sid=10663b1e21e6492b81b5455ebc805408 HTTP/1.1" 200 219 0.001145
like image 564
caliph Avatar asked Dec 18 '17 19:12

caliph


Video Answer


1 Answers

You may use socketio.run(app, host='0.0.0.0', port=5000, debug=True) if you want to switch in debug mode.


Then you may take a look on the socket.io official documentation website, most especialy on client-api and server-api. Beause in the httpsserver-options part of the server-api (for javascript), you can see that there is some options when running the server, like:

  pingInterval: 10000,
  pingTimeout: 5000,

I expect that those arguments could be re-used as kwargs when you run you server with socketio.run(app, host='0.0.0.0', port=5000, debug=True,pingInterval = 10000, pingTimeout= 5000)


And in the flask-socketio documentation, in the "Error Handling" part,there is nice tips to to deal with exceptions

You may add something like

@socketio.on_error_default  # handles all namespaces without an explicit error handler
def default_error_handler(e):
    pass

Another tips could be to adapt this previous error_handler for timeout issue and re-run the server when this event will be triggered.

You may also note that:

The message and data arguments of the current request can also be inspected with the request.event variable, which is useful for error logging and debugging outside the event handler

You may also use this following code to handle the final socket.timeout: timed out error :

try:
    socketio.run(app,...
except socket.error as socketerror:
    print("Error: ", socketerror)
like image 74
A STEFANI Avatar answered Oct 22 '22 00:10

A STEFANI