带有 AssertionError 的龙卷风 websocket 崩溃

Posted

技术标签:

【中文标题】带有 AssertionError 的龙卷风 websocket 崩溃【英文标题】:tornado websocket crash with AssertionError 【发布时间】:2019-08-10 17:13:05 【问题描述】:

当我从 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

【问题讨论】:

【参考方案1】:

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

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

【讨论】:

以上是关于带有 AssertionError 的龙卷风 websocket 崩溃的主要内容,如果未能解决你的问题,请参考以下文章

如何运行瓶子 + 龙卷风 + ssl (https) + spdy

在HAproxy中,我的websocket连接在50秒后关闭。怎么改呢?

龙卷风网络服务器中的 json rpc

Python 的 SignalR 替代方案

Tornado WebSocket 与 Django ORM 与共享会话

故障公告龙卷风继续袭击,服务器们顽强抵抗