trio-websocket icon indicating copy to clipboard operation
trio-websocket copied to clipboard

_handle_ping_event() occasionally sends to closed socket

Open brandyn opened this issue 1 year ago • 0 comments

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.

brandyn avatar Nov 14 '23 21:11 brandyn