Python tornado SSL websocket 连接

Python tornado SSL websocket connection

我正在使用在 Python 3.5 中编码的 websocket 服务器。这是提供我的网站实时信息所需要的。

我使用的 "server"-library 称为 tornado (version 4.5b2),它处理 websocket 连接和 http 请求。为了向后兼容(如果浏览器尚不支持 websocket),我使用 python 库 sockjs-tornado(版本 1.0.3),它还为 websocket 连接添加了一些附加功能。

现在我的问题是,如果我启动服务器,我会在控制台中收到很多 tornado ssl 错误。它不会崩溃,但是如果大约 80 个客户端连接到 websocket 3 错误经常发生。我使用加密连接并且它可以工作,但是如果发生这些错误之一,一些客户端将被踢出 websocket 连接。

Python 脚本使用多线程,服务器受 cloudflare 保护,它是某种中间人,将每个请求重定向到服务器或用户。

以下是 3 个错误:

​
ERROR:tornado.general:Uncaught exception, closing connection.
Traceback (most recent call last):
  File "/home/website/python/tornado/iostream.py", line 523, in _handle_events
    self._handle_write()
  File "/home/website/python/tornado/iostream.py", line 1398, in _handle_write
    super(SSLIOStream, self)._handle_write()
  File "/home/website/python/tornado/iostream.py", line 847, in _handle_write
    assert self._write_buffer_size >= 0
AssertionError
ERROR:tornado.application:Exception in callback None
Traceback (most recent call last):
  File "/home/website/python/tornado/ioloop.py", line 888, in start
    handler_func(fd_obj, events)
  File "/home/website/python/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/home/website/python/tornado/iostream.py", line 523, in _handle_events
    self._handle_write()
  File "/home/website/python/tornado/iostream.py", line 1398, in _handle_write
    super(SSLIOStream, self)._handle_write()
  File "/home/website/python/tornado/iostream.py", line 847, in _handle_write
    assert self._write_buffer_size >= 0
AssertionError

此错误不像第一个错误那样频繁出现。

ERROR:tornado.application:Uncaught exception GET /sockjs/398/ssputw1s/websocket
HTTPServerRequest(protocol='https', host='****.com:8443', method='GET', uri='/sockjs/398/ssputw1s/websocket', version='HTTP/1.1', remote_ip='****', headers={'Accept-Encoding': 'gzip', 'Cf-Visitor': '{"scheme":"https"}', 'Sec-Websocket-Key': '****', 'Host': '****.com:8443', 'X-Forwarded-Proto': 'https', 'Cache-Control': 'no-cache', 'Sec-Websocket-Version': '13', 'Cf-Ipcountry': 'FR', 'X-Forwarded-For': '****', 'Sec-Websocket-Extensions': 'x-webkit-deflate-frame', 'Origin': 'https://****.com', 'Cf-Connecting-Ip': '****', 'Cookie': '****', 'Upgrade': 'websocket', 'Cf-Ray': '34edb1a17c456908-CDG', 'User-Agent': '****', 'Connection': 'Upgrade', 'Pragma': 'no-cache'})
Traceback (most recent call last):
  File "/home/website/python/tornado/web.py", line 1464, in _stack_context_handle_exception
    raise_exc_info((type, value, traceback))
  File "<string>", line 4, in raise_exc_info
  File "/home/website/python/tornado/stack_context.py", line 316, in wrapped
    ret = fn(*args, **kwargs)
  File "/home/website/python/tornado/websocket.py", line 865, in _on_masked_frame_data
    self._on_frame_data(_websocket_mask(self._frame_mask, data))
  File "/home/website/python/tornado/websocket.py", line 910, in _on_frame_data
    self._receive_frame()
  File "/home/website/python/tornado/websocket.py", line 784, in _receive_frame
    self.stream.read_bytes(2, self._on_frame_start)
  File "/home/website/python/tornado/iostream.py", line 324, in read_bytes
    self._try_inline_read()
  File "/home/website/python/tornado/iostream.py", line 711, in _try_inline_read
    pos = self._read_to_buffer_loop()
  File "/home/website/python/tornado/iostream.py", line 625, in _read_to_buffer_loop
    if self._read_to_buffer() == 0:
  File "/home/website/python/tornado/iostream.py", line 738, in _read_to_buffer
    chunk = self.read_from_fd()
  File "/home/website/python/tornado/iostream.py", line 1487, in read_from_fd
    chunk = self.socket.read(self.read_chunk_size)
  File "/usr/lib64/python3.5/ssl.py", line 799, in read
    return self._sslobj.read(len, buffer)
  File "/usr/lib64/python3.5/ssl.py", line 585, in read
    v = self._sslobj.read(len)
ssl.SSLWantWriteError: The operation did not complete (write) (_ssl.c:2090)

有时会出现这个错误:

ERROR:tornado.general:Uncaught exception, closing connection.
Traceback (most recent call last):
  File "/home/website/python/tornado/iostream.py", line 523, in _handle_events
    self._handle_write()
  File "/home/website/python/tornado/iostream.py", line 1398, in _handle_write
    super(SSLIOStream, self)._handle_write()
  File "/home/website/python/tornado/iostream.py", line 872, in _handle_write
    del self._write_buffer[:self._write_buffer_pos]
BufferError: Existing exports of data: object cannot be re-sized
ERROR:tornado.application:Exception in callback None
Traceback (most recent call last):
  File "/home/website/python/tornado/ioloop.py", line 888, in start
    handler_func(fd_obj, events)
  File "/home/website/python/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/home/website/python/tornado/iostream.py", line 523, in _handle_events
    self._handle_write()
  File "/home/website/python/tornado/iostream.py", line 1398, in _handle_write
    super(SSLIOStream, self)._handle_write()
  File "/home/website/python/tornado/iostream.py", line 872, in _handle_write
    del self._write_buffer[:self._write_buffer_pos]
BufferError: Existing exports of data: object cannot be re-sized

看起来像是库中的错误 - 即导致多个实体同时使用相同写入缓冲区的竞争条件。

  • 第一个错误 - _write_buffer_size 下溢 - 另一个实体在不适当的时候从中读取
  • 第三个错误 - buffer in use 不应该

要捕获它,您需要跟踪所有使用 _write_buffer 和更改 _write_buffer_size 的操作以及它们的来源。

这看起来像是 this issue 中正在讨论的 Tornado 4.5b2 中的错误。期待它在 4.5b3 或 4.5 final 中得到修复,同时你可以回到 4.4 版本。