""" Project-local monkey patches to smooth over known library edge cases. This module is auto-imported by Python's site module when it's on sys.path. """ from __future__ import annotations import errno def _patch_websockets_close_connection() -> None: try: from websockets.legacy import protocol as ws_protocol except Exception: return original_close_connection = ws_protocol.WebSocketCommonProtocol.close_connection async def _close_connection(self, *args, **kwargs): try: return await original_close_connection(self, *args, **kwargs) except OSError as exc: # Ignore "Socket is not connected" during shutdown; peer already closed. if getattr(exc, "errno", None) == errno.ENOTCONN: return None raise ws_protocol.WebSocketCommonProtocol.close_connection = _close_connection _patch_websockets_close_connection()