Skip to content

Commit d8840cc

Browse files
authored
fix: ensure unsubscribe only attempts on open WebSocket connections (#51)
1 parent b7d9abc commit d8840cc

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

common/metric_types.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,16 @@ async def _collect_ws_data():
9191

9292
finally:
9393
if websocket:
94-
try:
95-
await asyncio.shield(self.unsubscribe(websocket))
96-
except asyncio.CancelledError:
97-
logging.info("Unsubscribe completed despite cancellation")
98-
except Exception as e:
99-
logging.warning(f"Error during unsubscribe (non-critical): {e!s}")
100-
94+
# Only try to unsubscribe if connection is still open
95+
if websocket.open:
96+
try:
97+
await asyncio.shield(self.unsubscribe(websocket))
98+
except asyncio.CancelledError:
99+
logging.info("Unsubscribe completed despite cancellation")
100+
except Exception as e:
101+
logging.warning(f"Error during unsubscribe (non-critical): {e!s}")
102+
103+
# Close can be called even on closed connections (it's idempotent)
101104
try:
102105
await asyncio.shield(websocket.close())
103106
except asyncio.CancelledError:

tests/test_api_read.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def main() -> None:
2929
setup_environment()
3030

3131
# Import handler after environment setup
32-
from api.read.hyperliquid import handler
32+
from api.read.arbitrum import handler
3333

3434
server = HTTPServer(("localhost", 8000), handler)
3535
print("Server started at http://localhost:8000")

0 commit comments

Comments
 (0)