Skip to content

fix: recover when a shared token refresh dies with its request - #75

Open
nicknisi wants to merge 1 commit into
mainfrom
fix/refresh-dedupe-cross-request
Open

nicknisi wants to merge 1 commit into
mainfrom
fix/refresh-dedupe-cross-request

Conversation

@nicknisi

Copy link
Copy Markdown
Member

Fixes #73.

On Cloudflare Workers, a promise created by one request can't settle in another. refreshTokens() shares in-flight refreshes across requests through a map on the singleton AuthKitCore. If the request that started a refresh ends early (for example, the client navigates away), every request waiting on it rejects with the Workers cross-request error. The .then(cleanup, cleanup) also never runs, so the dead promise stays in the map. Every later request with that refresh token then gets a 500 until the user signs in again.

Fix: a waiting request that gets anything other than a TokenRefreshError from a shared refresh removes the map entry (only if it still points to the dead promise) and refreshes in its own request. Real refresh failures still reach every waiter, and the dedupe from #33 still covers the normal case.

If the dead request's WorkOS call had already used up the refresh token, the retry fails with a normal TokenRefreshError and the user has to sign in again. That's expected.

Testing

  • New unit test: two requests waiting on a dead shared promise end up sharing one fresh refresh, and the map is empty afterward. The test fails without the fix.
  • Reproduced the bug in local workerd (wrangler 4.163) with a Worker that keeps one AuthKitCore per isolate. Before the fix, the concurrent request and every later request failed with the error from the issue. After the fix, both succeed.
  • pnpm test, typecheck, lint and format:check all pass.

On Cloudflare Workers, a promise created by one request can't settle in
another. If the request that started a deduped refresh ends early, every
waiter rejects and the map entry is never removed, so later requests with
that refresh token fail until the user signs in again.

Waiters now drop the dead entry on any non-TokenRefreshError rejection and
refresh in their own request.

Fixes #73
@greptile-apps

greptile-apps Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable issues were found.

Summary

refreshTokens() now recovers when a shared refresh rejects because its owning request ended.

  • Waiting requests can recover when a shared token refresh dies.

nicknisi explicitly acknowledged that recovery requires a new sign-in if the abandoned request already consumed the refresh token.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Shared refresh rejects] --> B{TokenRefreshError?}
  B -->|Yes| C[Return original error]
  B -->|No| D{Map still holds failed promise?}
  D -->|Yes| E[Remove failed promise]
  D -->|No| F[Call refreshTokens again]
  E --> F
  F --> G[Share replacement or start refresh]
Loading

Reviews (1) · Last reviewed commit: "fix: recover when a shared token refresh..." · Reviewed by Greptile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

refreshTokens dedupe shares a promise across requests, which breaks on Cloudflare Workers

1 participant