Skip to content

Commit

Permalink
Delay disconnect on errors in order to make sending close messages mo…
Browse files Browse the repository at this point in the history
…re robust
  • Loading branch information
taras committed Feb 11, 2025
1 parent 24930e4 commit 9a6a9b9
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions picows/picows.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1212,15 +1212,15 @@ cdef class WSProtocol:
# But don't wait for any tcp confirmation, use abort()
# because normal disconnect may hang until OS TCP/IP timeout
# for ACK is fired.
await sleep(0.01)
self.transport.underlying_transport.abort()
self._loop.call_later(0.01, self.transport.underlying_transport.abort)
break
except asyncio.CancelledError:
if self._log_debug_enabled:
self._logger.log(PICOWS_DEBUG_LL, "Auto-ping loop cancelled")
except:
self._logger.exception("Auto-ping loop failed, disconnect websocket")
self.transport.send_close(WSCloseCode.INTERNAL_ERROR, b"an exception occurred in auto-ping loop")
self.transport.disconnect()
self._loop.call_later(0.01, self.transport.disconnect)

cdef inline tuple _try_read_upgrade_request(self):
cdef bytes data = PyBytes_FromStringAndSize(self._buffer.data, self._f_new_data_start_pos)
Expand Down Expand Up @@ -1347,11 +1347,11 @@ cdef class WSProtocol:
except _WSParserError as ex:
self._logger.error("WS parser error: %s, initiate disconnect", ex.args)
self.transport.send_close(ex.args[0], ex.args[1].encode())
self.transport.disconnect()
self._loop.call_later(0.01, self.transport.disconnect)
except:
self._logger.exception("WS parser failure, initiate disconnect")
self.transport.send_close(WSCloseCode.PROTOCOL_ERROR)
self.transport.disconnect()
self._loop.call_later(0.01, self.transport.disconnect)

cdef inline WSFrame _get_next_frame_impl(self): # -> Optional[WSFrame]
"""Return the next frame from the socket."""
Expand Down Expand Up @@ -1482,7 +1482,7 @@ cdef class WSProtocol:
except Exception as e:
self._logger.exception("Unhandled exception in on_ws_connected, initiate disconnect")
self.transport.send_close(WSCloseCode.INTERNAL_ERROR)
self.transport.disconnect()
self._loop.call_later(0.01, self.transport.disconnect)

cdef inline _invoke_on_ws_frame(self, WSFrame frame):
try:
Expand Down Expand Up @@ -1512,7 +1512,7 @@ cdef class WSProtocol:
if self._disconnect_on_exception:
self._logger.exception("Unhandled exception in on_ws_frame, initiate disconnect")
self.transport.send_close(WSCloseCode.INTERNAL_ERROR)
self.transport.disconnect()
self._loop.call_later(0.01, self.transport.disconnect)
else:
self._logger.exception("Unhandled exception in on_ws_frame")

Expand Down

0 comments on commit 9a6a9b9

Please sign in to comment.