pythonwebsockettornado

Tornado websocket crash with AssertionError


I have some error when im sending data from websocket and I think the client is sending data as well and colliding with that data

ERROR:tornado.general:Uncaught exception, closing connection.
Traceback (most recent call last):
  File "/home/ori/.local/lib/python3.7/site-packages/tornado/iostream.py", line 706, in _handle_events
    self._handle_write()
  File "/home/ori/.local/lib/python3.7/site-packages/tornado/iostream.py", line 964, in _handle_write
    self._write_buffer.advance(num_bytes)
  File "/home/ori/.local/lib/python3.7/site-packages/tornado/iostream.py", line 202, in advance
    assert 0 < size <= self._size
AssertionError
ERROR:asyncio:Exception in callback None()
handle: <Handle cancelled>
Traceback (most recent call last):
  File "/usr/lib/python3.7/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "/home/ori/.local/lib/python3.7/site-packages/tornado/platform/asyncio.py", line 138, in _handle_events
    handler_func(fileobj, events)
  File "/home/ori/.local/lib/python3.7/site-packages/tornado/iostream.py", line 706, in _handle_events
    self._handle_write()
  File "/home/ori/.local/lib/python3.7/site-packages/tornado/iostream.py", line 964, in _handle_write
    self._write_buffer.advance(num_bytes)
  File "/home/ori/.local/lib/python3.7/site-packages/tornado/iostream.py", line 202, in advance
    assert 0 < size <= self._size
AssertionError
'NoneType' object has no attribute 'stream'

and the code that I think is cousing this problem is

def send_all(message):
    for ws in web_socket_clients:
        try:
            if not ws.ws_connection.stream.socket:
                web_socket_clients.remove(ws)
            else:
                try:
                    ws.write_message(json.dumps(message))
                except AssertionError as a:
                    pass
        except AssertionError:
            pass

Solution

  • Don't access attributes of ws_connection directly; this is not supported.

    Instead, override on_close in your handler and remove from the client set at this point.