fix(auth): OAuth refresh race and Scheduler thread leak#16
Merged
Conversation
Two refreshes in flight with the same refresh token caused the second to get a 401 from the provider (token already rotated) and log the user out. Guard _refresh_token with a per-user asyncio.Lock and add regression tests for refresh/validation races. Also stop expiring old access tokens in the Keycloak test mock on refresh - real Keycloak access tokens are stateless JWTs that stay valid until expiration. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
_schedule_token_refresh checked self._scheduler but assigned self.scheduler, so the check never passed and every refresh created a new Scheduler with its own daemon thread. Use _scheduler consistently so a single instance is created lazily and reused. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.
What changed
Two OAuth token-refresh fixes in
src/auth/oauth_token_manager.py:_refresh_tokenis now guarded by a per-userasyncio.Lock._schedule_token_refreshcheckedself._schedulerbut assignedself.scheduler(no underscore), so the check never passed and every refresh created a newScheduler, each starting its own daemon thread. The attribute is now used consistently, so a single scheduler is created lazily and reused.Tests
validate_user, and two concurrent refreshes with the same token.cd src && PYTHONPATH=. python -m pytest tests/auth/ -q: 84 passed, 4 skipped.Notes for reviewers
This branch previously carried the Vuetify migration (merged in #15); this PR contains only the two auth commits on top.
🤖 Generated with Claude Code