python-trio / trio-websocket

WebSocket client and server implementation for Python Trio
MIT License
70 stars 26 forks source link

_handle_ping_event() occasionally sends to closed socket #184

Open brandyn opened 1 year ago

brandyn commented 1 year ago

The short of it is that _handle_ping_event() sometimes tries to send a reply to a closing socket, which raises an (avoidable) exception from within the websocket thread.

This can lead to unexpected MultiErrors crashing the app. That should be improved with python 3.11's ExceptionGroups, but still there's no reason for websockets to be throwing this particular exception while closing down a pipe.

Here is the exception:

  Traceback (most recent call last):
    File ".../python3.10/site-packages/trio_websocket/_impl.py", line 1205, in _reader_task
      await handler(event)
    File ".../python3.10/site-packages/trio_websocket/_impl.py", line 1145, in _handle_ping_event
      await self._send(event.response())
    File ".../python3.10/site-packages/trio_websocket/_impl.py", line 1249, in _send
      data = self._wsproto.send(event)
    File ".../python3.10/site-packages/wsproto/__init__.py", line 64, in send
      data += self.connection.send(event)
    File ".../python3.10/site-packages/wsproto/connection.py", line 107, in send
      raise LocalProtocolError(
  wsproto.utilities.LocalProtocolError: Event Pong(payload=bytearray(b'')) cannot be sent in state ConnectionState.LOCAL_CLOSING.