fix(auth-next-server): deduplicate concurrent token refresh to prevent invalid_grant#2865
Open
dom-murray wants to merge 1 commit intomainfrom
Open
fix(auth-next-server): deduplicate concurrent token refresh to prevent invalid_grant#2865dom-murray wants to merge 1 commit intomainfrom
dom-murray wants to merge 1 commit intomainfrom
Conversation
…prevent invalid_grant OAuth refresh tokens are single-use. Without concurrency protection, parallel requests hitting the same expired token each fire a separate refresh, causing the second caller to receive a 400 invalid_grant and set RefreshTokenError on the session. Promise deduplication ensures all concurrent callers share one in-flight refresh per token. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
|
View your CI Pipeline Execution ↗ for commit ccecbe5
☁️ Nx Cloud last updated this comment at |
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.
Summary
400 invalid_grantand haveRefreshTokenErrorset on their sessionrefreshAccessToken: concurrent callers with the same refresh token share one in-flight promise instead of each making a separate network requestdoRefreshAccessToken; the public API is unchangedHow it works
The map entry is cleaned up via
.finally()so the next genuine refresh (after the token rotates) works normally.Test plan
RefreshTokenErrorforceRefresh(zkEvm registration flow) still works correctly via thetrigger === 'update'path in the JWT callback (that path callsrefreshAccessTokendirectly and benefits from the same deduplication)🤖 Generated with Claude Code
Note
Medium Risk
Touches server-side OAuth token refresh behavior; while the API is unchanged, incorrect deduping/cleanup could cause stale refresh results or masking of refresh errors under load.
Overview
Prevents
invalid_granterrors under parallel requests by deduplicating concurrentrefreshAccessTokencalls perclientId+refreshToken, ensuring only one refresh request is sent and other callers await the same promise.Refactors the actual HTTP refresh logic into an internal
doRefreshAccessTokenhelper and cleans up the in-flight promise cache via.finally()so subsequent refreshes proceed normally.Reviewed by Cursor Bugbot for commit ccecbe5. Bugbot is set up for automated code reviews on this repo. Configure here.