Skip to content

Remove token/sid associations when server is exiting#5802

Merged
adhami3310 merged 3 commits into
mainfrom
masenf/clear-token-sid-associations
Sep 22, 2025
Merged

Remove token/sid associations when server is exiting#5802
adhami3310 merged 3 commits into
mainfrom
masenf/clear-token-sid-associations

Conversation

@masenf

@masenf masenf commented Sep 19, 2025

Copy link
Copy Markdown
Collaborator

This allows existing tokens to reconnect to redis after a hot or cold reload of the app. Otherwise, the old associations for the token remain in place and when the same client reconnects, it is given a new_token, since the requested token is already "taken" in redis.

This allows existing tokens to reconnect to redis after a hot or cold reload of
the app. Otherwise, the old associations for the token remain in place and when
the same client reconnects, it is given a new_token, since the requested token
is already "taken" in redis.

@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 adds cleanup logic to properly handle token/session ID associations when the Reflex server shuts down. The changes introduce a new disconnect_all method to the TokenManager base class that clears all token-to-session mappings, and integrates this cleanup into the application lifespan management.

The core issue being addressed is that after server restarts (hot or cold reloads), existing client tokens would be rejected because their associations remained in Redis. When clients attempted to reconnect with valid tokens, the server would treat them as "already taken" and force clients to obtain new tokens, breaking session continuity.

The implementation works by:

  1. Adding a disconnect_all method in token_manager.py that collects all token-sid pairs from both mapping dictionaries (token_to_sid and sid_to_token) and calls disconnect_token for each pair
  2. Integrating this cleanup into the lifespan management in lifespan.py by calling disconnect_all() during server shutdown with proper error handling

This change fits into Reflex's broader architecture by leveraging the existing lifespan mixin system for graceful shutdown procedures and building upon the established TokenManager pattern for session management. The cleanup ensures that Redis-backed deployments maintain proper session continuity across server restarts, which is crucial for production applications where users expect seamless reconnection experiences.

Confidence score: 3/5

  • This PR addresses a real issue but has some implementation concerns that could cause partial cleanup failures
  • Score reflects potential edge cases with the cleanup logic and placement outside the AsyncExitStack context manager
  • Pay close attention to the error handling patterns and the token-sid pair collection logic in token_manager.py

2 files reviewed, 1 comment

Edit Code Review Bot Settings | Greptile

Comment thread reflex/utils/token_manager.py
@codspeed-hq

codspeed-hq Bot commented Sep 19, 2025

Copy link
Copy Markdown

CodSpeed Performance Report

Merging #5802 will not alter performance

Comparing masenf/clear-token-sid-associations (224c0f9) with main (4468e14)

Summary

✅ 8 untouched

@adhami3310 adhami3310 merged commit 477348f into main Sep 22, 2025
41 checks passed
@adhami3310 adhami3310 deleted the masenf/clear-token-sid-associations branch September 22, 2025 20:47
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