Fixed potential race condition between call_later() and run_forever()#3897
Merged
petyaslavova merged 5 commits intomasterfrom Jan 5, 2026
Merged
Fixed potential race condition between call_later() and run_forever()#3897petyaslavova merged 5 commits intomasterfrom
petyaslavova merged 5 commits intomasterfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a potential race condition in the token manager's start() method that could occur when scheduling callbacks before the event loop thread was fully initialized. The fix improves thread-safety by using proper synchronization primitives and thread-safe scheduling methods.
Key Changes:
- Replaced
asyncio.Eventwiththreading.Eventfor cross-thread synchronization to properly coordinate between the main thread and event loop thread - Changed from
loop.call_later()toloop.call_soon_threadsafe()for thread-safe callback scheduling - Simplified the
_renew_token()method signature by removing theinit_eventparameter and handling initialization signaling through a wrapper function
Comments suppressed due to low confidence (1)
redis/auth/token_manager.py:310
- The
_renew_tokenmethod signature has been updated to remove theinit_eventparameter, but the async version_renew_token_asyncat line 310 still retains this parameter. This creates an inconsistency between the two methods. For consistency and maintainability, consider whether_renew_token_asyncshould also be updated to remove theinit_eventparameter if it's no longer needed, or document why the async version still requires it.
):
"""
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
❌ The following Jit checks failed to run:
- secret-detection-trufflehog
#jit_bypass_commit in this PR to bypass, Jit Admin privileges required.
More info in the Jit platform.
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.
Description of change
Please provide a description of the change here.
Pull Request check-list
Please make sure to review and check all of these items:
PR fixes a race condition between
loop.call_later()andloop.run_forever()methods, so the renewal task is scheduled before the event loop is actually running