Skip to content

fix(auth): prevent a token-refresh stampede on concurrent 401s - #538

Open
chrishagglund-ship-it wants to merge 2 commits into
mainfrom
fix/token-refresh-stampede
Open

chrishagglund-ship-it wants to merge 2 commits into
mainfrom
fix/token-refresh-stampede

Conversation

@chrishagglund-ship-it

Copy link
Copy Markdown
Contributor

No description provided.

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
chrishagglund-ship-it marked this pull request as ready for review November 20, 2026 18:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant