Flask-socketio: socketio stuck after long run?

Created on 15 Apr 2020  路  6Comments  路  Source: miguelgrinberg/Flask-SocketIO

Hi, will socketio stuck after run for some amount of time?? Basically I setup an flask-socketio server below (run using python app.py) to redirect the message receive to my client. The server is able to run but after a certain amount of time (a few days) it just stuck and the server just sent PING data None PONG data None

051cff920da64de4af1be3dc57870e1a: Received packet PING data None
051cff920da64de4af1be3dc57870e1a: Sending packet PONG data None
012b691824f0499c95930c86d166cb9e: Received packet PING data None
012b691824f0499c95930c86d166cb9e: Sending packet PONG data None
704c32cdbdf34d82821fd21f50414a26: Received packet PING data None
704c32cdbdf34d82821fd21f50414a26: Sending packet PONG data None
ecc11d5e23384677afd7c65d886a586c: Received packet PING data None
ecc11d5e23384677afd7c65d886a586c: Sending packet PONG data None

I am using redis as well. Any idea that might caused this? Help is appreciated thank you.


from gevent import monkey
monkey.patch_all()
from flask import Flask, render_template
from flask_socketio import SocketIO, emit

app = Flask(__name__)
app.config["REDIS_URL"] = 'redis://localhost:6379/0'

socketio

socketio = SocketIO(app, message_queue=app.config["REDIS_URL"], cors_allowed_origins = '*', logger=True, engineio_logger=True)

@app.route('/')
def index():
return "Nothing to display", 200

@socketio.on('insert')
def handle_message(data):
socketio.emit(data)
socketio.sleep(0)

@socketio.on('update')
def handle_message(data):
socketio.emit(data)
socketio.sleep(0)

@socketio.on('delete')
def handle_message(data):
socketio.emit(data)
socketio.sleep(0)

if __name__ == '__main__':
socketio.run(app, host="0.0.0.0" ,port=50050)

question

All 6 comments

Do you have all the logs? I need to see logs for the moment the server stopped working to tell you if there was an issue.

After I check back the logs, it seems to stuck(sending None only) after the following. I deleted some of my directory from belows.

It seems to implies that Exception1 is not json serializable. Exception1 is declare as follows:

class Exception1 (Exception):
def __init__(self,args,kwargs):
Exception.__init__(self,
args,kwargs)

Usage:
list1 = ["error1", error2]
raise Exception1 (f"Failure in {list1}")

Socketio stuck after the type error from above.


Traceback (most recent call last):

File "src/gevent/greenlet.py", line 766, in gevent._greenlet.Greenlet.run

File "C:\ProgramData\Anaconda3\envs_\lib\site-packages\socketio\pubsub_manager.py", line 150, in _thread

self._handle_emit(data)

File "C:\ProgramData\Anaconda3\envs_\lib\site-packages\socketio\pubsub_manager.py", line 108, in _handle_emit

callback=callback)

File "C:\ProgramData\Anaconda3\envs_\lib\site-packages\socketio\base_manager.py", line 141, in emit

self.server._emit_internal(sid, event, data, namespace, id)

File "C:\ProgramData\Anaconda3\envs_\lib\site-packages\socketio\server.py", line 577, in _emit_internal

binary=binary))

File "C:\ProgramData\Anaconda3\envs_\lib\site-packages\socketio\server.py", line 581, in _send_packet

encoded_packet = pkt.encode()

File "C:\ProgramData\Anaconda3\envs_\lib\site-packages\socketio\packet.py", line 71, in encode

encoded_packet += self.json.dumps(data, separators=(',', ':'))

File "C:\ProgramData\Anaconda3\envs_\lib\json__init__.py", line 238, in dumps

**kw).encode(obj)

File "C:\ProgramData\Anaconda3\envs_\lib\json\encoder.py", line 199, in encode

chunks = self.iterencode(o, _one_shot=True)

File "C:\ProgramData\Anaconda3\envs_\lib\json\encoder.py", line 257, in iterencode

return _iterencode(o, 0)

File "C:\ProgramData\Anaconda3\envs_\lib\json\encoder.py", line 180, in default

o.__class__.__name__)

TypeError: Object of type 'Exception1' is not JSON serializable

2020-04-17T16:15:48Z >> failed with TypeError

1cc1e237704541f9ad6e3404faa82b2e: Received packet PING data None

1cc1e237704541f9ad6e3404faa82b2e: Sending packet PONG data None

1cc1e237704541f9ad6e3404faa82b2e: Received packet PING data None

1cc1e237704541f9ad6e3404faa82b2e: Sending packet PONG data None

1cc1e237704541f9ad6e3404faa82b2e: Received packet PING data None

1cc1e237704541f9ad6e3404faa82b2e: Sending packet PONG data None

1cc1e237704541f9ad6e3404faa82b2e: Received packet PING data None

1cc1e237704541f9ad6e3404faa82b2e: Sending packet PONG data None

This suggests you are trying to emit that exception, which isn't possible as only JSON compatible data types and bytes are allowed.

Thanks for the reply, I try to explicitly convert the exception into string only using str(e). However, socketio stop responding after receiving this error, only able to PING PONG with data NONE and upgrade to connection to websocket, isnt socketio suppose to except this error and continue to work as expected?

@wbtan7 can you create a simple example that shows how the server stops responding? If you can, I can investigate. Normally an error should not prevent other events from happening.

@miguelgrinberg I will close this first, since I am not able to replicate the error with a simple example yet right now. Thanks for your help.

Was this page helpful?
0 / 5 - 0 ratings