fix: deduplicate concurrent WorkOS session refreshes#384
Open
fix: deduplicate concurrent WorkOS session refreshes#384
Conversation
When multiple requests arrive with the same expired session cookie, only one refresh call is made to WorkOS. Concurrent requests await the same promise instead of each consuming the single-use refresh token independently. Fixes studio page sign-out caused by batch submission race condition. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
3 tasks
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
refresh()call is made to WorkOS — concurrent requests await the same promiseRoot Cause Analysis
The Bug
When the studio page fires multiple concurrent API requests (batch submission sends up to 5 at a time via
Promise.allSettled), and the user's WorkOS access token has expired, all concurrent requests race to refresh the same session. WorkOS refresh tokens are single-use (rotated on refresh). Only 1 request wins; the others fail.Failure Trace
useBatchSubmit.ts:84) fires up to 5 concurrentPromise.allSettledrequestsrequireAuthmiddleware with the same expired session cookieauthenticateWorkOS()→authenticateAndGetWorkOSSession()returnsnull(expired)refreshWorkOSSession()— but WorkOS refresh tokens are single-usecatchblock → returnsnullnullresult →handleWorkOSAuthFailure()→ clears thewos-sessioncookie and returns 401logout()→ navigates to sign-inWhy Only Studio
The studio page is the only feature that fires concurrent authenticated API requests. Normal pages make sequential requests, so the refresh happens once and subsequent requests use the new cookie. Studio's batch pattern creates the race.
Contributing Factors
logout()on any 401 with no debounce (fixed in companion frontend PR)handleWorkOSAuthFailureclearswos-sessioncookie, so even if request test fluent ffmpeg #1 set a new cookie, the 401 response from request bring up skeleton #2 clears ituseStudioJobProgress.tspolls every 10s with multiple concurrent GET requestsChanges
src/utils/workos.util.ts: AddedpendingRefreshesMap that caches in-flight refresh promises keyed byauthToken. The promise is cleaned up via.finally()after resolution.Companion PR
Test plan
🤖 Generated with Claude Code