首页 > 解决方案 > 带有 AssertionError 的龙卷风 websocket 崩溃

问题描述

当我从 websocket 发送数据时出现一些错误,我认为客户端也在发送数据并与该数据发生冲突

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'

我认为引起这个问题的代码是

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

标签: python-3.xwebsockettornado

解决方案


不要ws_connection直接访问属性;这是不支持的。

相反,在您的处理程序中覆盖on_close并在此时从客户端集中删除。


推荐阅读