Reassociate sid and token when connecting websocket#5794
Conversation
CodSpeed Performance ReportMerging #5794 will not alter performanceComparing Summary
|
There was a problem hiding this comment.
Greptile Summary
This PR addresses a critical websocket reconnection issue in Reflex applications. The change modifies the link_token_to_sid method in reflex/app.py to update client state when a websocket reconnects, ensuring that background tasks can continue sending updates to the new connection.
The core problem being solved is that when a websocket connection drops and reconnects, any background tasks that were running before the disconnection would lose their ability to send updates to the frontend because they remained associated with the old session ID. The solution adds a state modification block that:
- Updates the
router_datadictionary with the new session ID (sid) - Recreates the
RouterDataobject from the updated data - Ensures continuity between background tasks and the frontend across connection disruptions
This change integrates with Reflex's existing state management system by leveraging the modify_state context manager (seen in the RAG context) which provides exclusive access to state modifications and handles delta emission to the frontend. The fix specifically targets the _substate_key for the root state, ensuring that the session ID update propagates correctly through the state hierarchy.
The implementation is particularly important for long-running operations that need to maintain real-time updates even when network connectivity is unstable, which is a common scenario in web applications.
Confidence score: 4/5
- This PR addresses a well-defined issue with a targeted solution that maintains websocket connectivity for background tasks
- Score reflects the focused nature of the change and clear problem-solution mapping, though websocket reconnection logic can be complex
- Pay close attention to the state modification logic in
reflex/app.pyto ensure proper integration with existing state management
1 file reviewed, no comments
If there are active background tasks for a given client_token, reassociate the token and sid in the state when reconnecting so that updates from the background task go to to the new websocket.
1a5c257 to
82d80cd
Compare
If there are active background tasks for a given client_token, reassociate the token and sid in the state when reconnecting so that updates from the background task go to to the new websocket.