Fix race conditions in StateManagerRedis lock detection#6196
Merged
Conversation
adhami3310
previously approved these changes
Mar 19, 2026
Contributor
Greptile SummaryThis PR fixes race conditions in Key changes:
Confidence Score: 3/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant C as Caller (modify_state)
participant SMR as StateManagerRedis
participant ET as _lock_task (ensure_task)
participant PS as Redis pubsub
C->>SMR: _wait_for_lock(lock_key, lock_id)
SMR->>SMR: _try_get_lock() → fails (lock held)
SMR->>ET: _ensure_lock_task()
Note over ET: Creates task if not running
ET->>PS: _enable_keyspace_notifications()
ET->>PS: pubsub.psubscribe(**handlers)
PS-->>ET: subscription confirmed
ET->>SMR: _lock_updates_subscribed.set()
SMR->>SMR: await _lock_updates_subscribed.wait() ✓
SMR->>SMR: _lock_waiter(lock_key) registered
SMR->>SMR: _request_lock_release(lock_key, lock_id)
PS-->>ET: keyspace event: lock del
ET->>SMR: _handle_lock_release(event)
SMR->>SMR: lock_released_event.set()
SMR->>SMR: _try_get_lock() → success
SMR-->>C: lock acquired
Last reviewed commit: "Fix race conditions ..." |
Fix other race conditions also present in the tests. In short: both the framework and tests must deterministically wait for the Redis pubsub psubscribe call to complete to ensure that events, such as lock release, are properly recorded. Co-authored-by: Farhan Ali Raza <farhanalirazaazeemi@gmail.com>
8d22705 to
4eaf1c3
Compare
ignore TimeoutError for this call, because we still want to enter the lock waiter loop, even if there was some redis problem, just the waiter might have to wait for the full lock expiration (fallback case to previous behavior). but we should _NEVER_ deadlock the process waiting for the psubscribe event and we should _NEVER_ exit _wait_for just because the timeout for the previous expired.
adhami3310
approved these changes
Mar 19, 2026
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.
Fix other race conditions also present in the tests.
In short: both the framework and tests must deterministically wait for the Redis pubsub psubscribe call to complete to ensure that events, such as lock release, are properly recorded.