Skip to content

Reassociate sid and token when connecting websocket#5794

Merged
adhami3310 merged 3 commits into
mainfrom
masenf/reassociate-websocket
Sep 17, 2025
Merged

Reassociate sid and token when connecting websocket#5794
adhami3310 merged 3 commits into
mainfrom
masenf/reassociate-websocket

Conversation

@masenf

@masenf masenf commented Sep 16, 2025

Copy link
Copy Markdown
Collaborator

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.

@codspeed-hq

codspeed-hq Bot commented Sep 16, 2025

Copy link
Copy Markdown

CodSpeed Performance Report

Merging #5794 will not alter performance

Comparing masenf/reassociate-websocket (80655b9) with main (f226bb8)

Summary

✅ 8 untouched

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. Updates the router_data dictionary with the new session ID (sid)
  2. Recreates the RouterData object from the updated data
  3. 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.py to ensure proper integration with existing state management

1 file reviewed, no comments

Edit Code Review Bot Settings | Greptile

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.
@masenf masenf force-pushed the masenf/reassociate-websocket branch from 1a5c257 to 82d80cd Compare September 17, 2025 18:06
@adhami3310 adhami3310 merged commit ba4070d into main Sep 17, 2025
41 checks passed
@adhami3310 adhami3310 deleted the masenf/reassociate-websocket branch September 17, 2025 19:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants