-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Revive token socket_record after expiration #5977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
If the socket_record expires in redis, but the instance managing it is still alive, then re-link the token to the sid. For `del` and `evicted`, do not re-link the token as this is an explicit disconnect.
CodSpeed Performance ReportMerging #5977 will not alter performanceComparing Summary
|
Greptile OverviewGreptile SummaryThis PR implements token revival logic when Redis records expire while the managing instance is still alive. The change also fixes a critical bug in Key Changes:
Issues Found:
Confidence Score: 3/5
Important Files ChangedFile Analysis
Sequence DiagramsequenceDiagram
participant Redis
participant Subscriber as Redis Keyspace Subscriber
participant TokenManager as RedisTokenManager
participant LocalDict as token_to_socket dict
Note over Redis: Token expires in Redis
Redis->>Subscriber: keyspace event: "expired"
Subscriber->>TokenManager: _handle_socket_record_del(token, expired=True)
TokenManager->>LocalDict: pop(token)
LocalDict-->>TokenManager: socket_record
Note over TokenManager: Check instance_id == self.instance_id
alt expired=True
TokenManager->>TokenManager: link_token_to_sid(token, sid)
TokenManager->>Redis: set(token_key, socket_record, ex=...)
TokenManager->>LocalDict: token_to_socket[token] = socket_record
Note over TokenManager: Token revived!
end
TokenManager->>LocalDict: sid_to_token.pop(sid)
Note over Redis: Explicit delete
Redis->>Subscriber: keyspace event: "del"
Subscriber->>TokenManager: _handle_socket_record_del(token, expired=False)
TokenManager->>LocalDict: pop(token)
LocalDict-->>TokenManager: socket_record
Note over TokenManager: Check instance_id == self.instance_id
alt expired=False
Note over TokenManager: No revival - token stays deleted
end
TokenManager->>LocalDict: sid_to_token.pop(sid)
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1 file reviewed, 1 comment
| expired: Whether the deletion was due to expiration. | ||
| """ | ||
| if ( | ||
| socket_record := self.token_to_socket.pop(token, None) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: the token has been popped from token_to_socket on line 242, but then re-inserted during link_token_to_sid on line 247 - this creates a race where the socket_record is temporarily missing
| socket_record := self.token_to_socket.pop(token, None) | |
| ) is not None and socket_record.instance_id == self.instance_id: | |
| if expired: | |
| # Keep the record alive as long as this process is alive and not deleted. | |
| await self.link_token_to_sid(token, socket_record.sid) | |
| self.sid_to_token.pop(socket_record.sid, None) |
Prompt To Fix With AI
This is a comment left during a code review.
Path: reflex/utils/token_manager.py
Line: 242:242
Comment:
**logic:** the token has been popped from `token_to_socket` on line 242, but then re-inserted during `link_token_to_sid` on line 247 - this creates a race where the socket_record is temporarily missing
```suggestion
) is not None and socket_record.instance_id == self.instance_id:
if expired:
# Keep the record alive as long as this process is alive and not deleted.
await self.link_token_to_sid(token, socket_record.sid)
self.sid_to_token.pop(socket_record.sid, None)
```
How can I resolve this? If you propose a fix, please make it concise.
If the socket_record expires in redis, but the instance managing it is still alive, then re-link the token to the sid.
For
delandevicted, do not re-link the token as this is an explicit disconnect.