Handle WebSocket disconnects cleanly in streamer _reader#327
Closed
ccjoeg wants to merge 1 commit intotastyware:masterfrom
Closed
Handle WebSocket disconnects cleanly in streamer _reader#327ccjoeg wants to merge 1 commit intotastyware:masterfrom
ccjoeg wants to merge 1 commit intotastyware:masterfrom
Conversation
When the DXFeed WebSocket drops (network error or server-requested reconnect with code 1012), the raw httpx_ws exceptions (WebSocketNetworkError, WebSocketDisconnect) propagate uncaught from _reader, crash the anyio TaskGroup, and surface as an ExceptionGroup with a full traceback. This makes it difficult for callers to distinguish routine disconnects from real errors and to implement appropriate retry strategies (e.g. fast reconnect for code 1012 SERVICE_RESTART vs longer backoff for network failures). This change: - Adds StreamerDisconnect exception with code/reason attributes - Catches WebSocketDisconnect and WebSocketNetworkError in both DXLinkStreamer._reader and AlertStreamer._reader, re-raising as StreamerDisconnect - Unwraps StreamerDisconnect from ExceptionGroup in DXLinkStreamer.__asynccontextmanager__ so callers receive a plain exception they can match on - Exports StreamerDisconnect from the package for caller use Callers can now catch StreamerDisconnect and inspect e.code to choose retry behavior, e.g. immediate reconnect for code 1012 vs 30s backoff for network errors. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Member
|
Please use the pattern from the docs: https://tastyworks-api.readthedocs.io/en/latest/data-streamer.html#auto-retries |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
StreamerDisconnectexception (withcodeandreasonattributes) totastytrade.utilsWebSocketDisconnectandWebSocketNetworkErrorin bothDXLinkStreamer._readerandAlertStreamer._reader, re-raising asStreamerDisconnectStreamerDisconnectfrom theExceptionGroupinDXLinkStreamer.__asynccontextmanager__so callers receive a plain exceptionStreamerDisconnectfrom the package__init__.pyMotivation
When the DXFeed WebSocket drops — whether from a network error or a server-requested reconnect (close code 1012,
SERVICE_RESTART) — the rawhttpx_wsexceptions (WebSocketNetworkError,WebSocketDisconnect) propagate uncaught from_reader. This crashes the anyioTaskGroupand surfaces as anExceptionGroupwith a full traceback:This makes it difficult for callers to:
Usage
After this change, callers can catch
StreamerDisconnectand inspecte.code:Test plan
StreamerDisconnectis raised (notExceptionGroup) on WebSocket closee.codeis populated correctly forWebSocketDisconnect(e.g. 1012)e.codeisNoneande.reasonis"network error"forWebSocketNetworkError🤖 Generated with Claude Code