Skip to content

fix: resolve race condition in token refresh interceptor (#20) - #97

Merged
Senthil455 merged 1 commit into
mainfrom
fix/issue-20-token-refresh-race-condition
Jun 17, 2026
Merged

fix: resolve race condition in token refresh interceptor (#20)#97
Senthil455 merged 1 commit into
mainfrom
fix/issue-20-token-refresh-race-condition

Conversation

@Senthil455

Copy link
Copy Markdown
Owner

Description

Moves the original._retry = true flag before the
efreshPromise check in the axios response interceptor to fix a race condition when multiple requests receive 401 simultaneously.

Root Cause

When two API calls get a 401 at the same time:

  • Request A enters the interceptor, finds no refreshPromise, sets _retry = true, creates refreshPromise
  • Request B enters the interceptor, finds refreshPromise exists, awaits it, gets the token, retries
  • Request B's _retry was never set to true
  • If the retry also returns 401, Request B enters the interceptor again with _retry still undefined
  • This causes the retry to attempt a new refresh, creating a loop

Changes

  • Moved original._retry = true before the refreshPromise check in rontend/src/lib/api.ts
  • Both the initial request and concurrent requests now properly mark themselves as retried

Testing

Existing refresh flow continues to work. The fix prevents the infinite loop scenario under concurrent 401 conditions.

Fixes #20

Move original._retry flag before the refreshPromise check so
concurrent 401 requests dont loop when retrying after refresh.

Fixes #20
@Senthil455
Senthil455 merged commit dfa8b97 into main Jun 17, 2026
7 of 19 checks passed

@Senthil455 Senthil455 left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review looks good. The key fix is moving \original._retry = true\ before the
efreshPromise\ check, which eliminates the race window where two concurrent refresh attempts could both pass the guard.

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.

CRITICAL: Token refresh is fundamentally broken - user is logged out after first token expiry

1 participant