Repository navigation
fix(auth): prevent a token-refresh stampede on concurrent 401s - #538
Open
chrishagglund-ship-it wants to merge 2 commits into
Open
chrishagglund-ship-it wants to merge 2 commits into
chrishagglund-ship-it wants to merge 2 commits into
Conversation
After a failover every in-flight request gets 401 INVALID_TOKEN at once. Each one calls force_refresh_auth_token, which fetches a new token without a lock, so N rejected requests fetch N tokens. The async client has the same race when N coroutines make their first call together. The tests are marked xfail(strict=True) until the fix lands: - 20 rejected requests (sync): 21 token fetches, expected 2 - 20 rejected requests (async): 21 token fetches, expected 2 - 20 first calls (async): 20 token fetches, expected 1 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On a 401 with EXPIRED_TOKEN or INVALID_TOKEN, the client now passes the token the request carried to __refresh_auth_token_if_stale. Under a lock, it fetches a new token only if the cache still holds the rejected one; otherwise another thread or coroutine already replaced it, and the request retries with the cached token. The TTL renewal (sync and async) and the async client's first token fetch now check again under the same lock, so callers that need a new token together fetch it once. Each worker process still has its own client and token; this change only removes duplicate fetches within one process. Removes the xfail markers from the tests added in the previous commit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
chrishagglund-ship-it
marked this pull request as ready for review
November 20, 2026 18:42
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.
No description provided.