Repository navigation
fix: reconnect the websocket and replay subscriptions after a drop - #345
saravanan619 wants to merge 2 commits into
Conversation
|
Thanks for this, reconnecting and replaying the subscriptions is a big improvement. One case it doesn't cover yet: a connection that goes quiet without an error. After a laptop sleep, a Wi-Fi switch or a NAT/router timeout, the TCP socket can stay "open" with nothing arriving. A small way to catch it: track when the last message (or We hit exactly this after Mac sleeps in a bot we run on the API; a read-timeout like the above fixed it. Happy to help with a test if useful. |
Bug
When the WebSocket connection drops,
WebsocketManagernever reconnects and never tells the caller (#340):run()callsws.run_forever()with noreconnectargument, there are noon_close/on_errorhandlers, andws_readystaysTrueforever, so a latersubscribe()raisesWebSocketConnectionClosedExceptionand 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_openonly replaysqueued_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 toactive_subscriptions, so one message triggers the callback N times after N reconnects).Fix
run()now callsrun_forever(reconnect=5), so websocket-client re-establishes the connection and re-fireson_open.on_close/on_errorhandlers that setws_ready = False, sosubscribe()queues while disconnected instead of sending into a dead socket.on_opendrainsqueued_subscriptionsbefore replaying them, then re-sends everyactive_subscriptionsentry, so no subscription is lost or duplicated on reconnect.ActiveSubscriptionnow also stores the subscription dict so it can be re-sent.subscribe()no longer raises when the socket drops between thews_readycheck and the send: the subscription is queued and delivered on the nexton_open.send_pingcatchesWebSocketConnectionClosedException, 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, stubbedWebSocketApp): active subscriptions replayed exactly once on reconnect, callbacks not duplicated after two reconnects, the queue is not replayed twice,on_close/on_errorclearws_readyand queue-then-deliver works,subscribe()while the socket is down queues instead of raising,run()passesreconnect=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_openoverlaps with #319 (which also drains the queue while fixing duplicate single-subscription queueing); happy to rebase once that lands.Fixes #340
Fixes #338