Skip to content

fix: reconnect the websocket and replay subscriptions after a drop - #345

Open
saravanan619 wants to merge 2 commits into
hyperliquid-dex:masterfrom
saravanan619:fix/websocket-reconnect-340
Open

saravanan619 wants to merge 2 commits into
hyperliquid-dex:masterfrom
saravanan619:fix/websocket-reconnect-340

Conversation

@saravanan619

Copy link
Copy Markdown

Bug

When the WebSocket connection drops, WebsocketManager never reconnects and never tells the caller (#340): run() calls ws.run_forever() with no reconnect argument, there are no on_close / on_error handlers, and ws_ready stays True forever, so a later subscribe() raises WebSocketConnectionClosedException and existing subscriptions go dark with no error. The docs state the WS server "disconnects periodically and without announcement" and that clients must handle reconnects; this SDK does not.

On top of that (#338), even if the socket were re-established, on_open only replays queued_subscriptions, which are only populated before the first connect, and it never clears the queue. So subscriptions made after connecting are silently never re-sent, while pre-connect subscriptions are re-sent on every reconnect and their callbacks fire once per reconnect (the queue replay re-appends to active_subscriptions, so one message triggers the callback N times after N reconnects).

Fix

  • run() now calls run_forever(reconnect=5), so websocket-client re-establishes the connection and re-fires on_open.
  • Added on_close / on_error handlers that set ws_ready = False, so subscribe() queues while disconnected instead of sending into a dead socket.
  • on_open drains queued_subscriptions before replaying them, then re-sends every active_subscriptions entry, so no subscription is lost or duplicated on reconnect. ActiveSubscription now also stores the subscription dict so it can be re-sent.
  • subscribe() no longer raises when the socket drops between the ws_ready check and the send: the subscription is queued and delivered on the next on_open.
  • send_ping catches WebSocketConnectionClosedException, so the ping thread survives the reconnect window instead of dying on the first ping sent while the socket is down.

Tests

Added tests/websocket_manager_reconnect_test.py (7 tests, no network, stubbed WebSocketApp): active subscriptions replayed exactly once on reconnect, callbacks not duplicated after two reconnects, the queue is not replayed twice, on_close/on_error clear ws_ready and queue-then-deliver works, subscribe() while the socket is down queues instead of raising, run() passes reconnect=5, and the ping thread survives a failed send.

Verified red-then-green: all 7 new tests fail on unpatched code and pass after the fix. Full suite: 43 passed. Black clean.

Note: the queue-draining part of on_open overlaps with #319 (which also drains the queue while fixing duplicate single-subscription queueing); happy to rebase once that lands.

Fixes #340
Fixes #338

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant