Repository navigation
[wrangler] Reuse OAuth credentials refreshed by another process - #16874
Conversation
🦋 Changeset detectedLatest commit: f450170 The changes in this PR will be included in the next version bump. This PR includes changesets to release 4 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
LGTM |
@cloudflare/autoconfig
@cloudflare/build-output-utils
@cloudflare/codemods
@cloudflare/config
@cloudflare/containers-shared
create-cloudflare
@cloudflare/deploy-helpers
@cloudflare/kv-asset-handler
miniflare
@cloudflare/pages-functions
@cloudflare/pages-shared
@cloudflare/runtime-types
@cloudflare/unenv-preset
@cloudflare/vite-plugin
@cloudflare/vitest-plugin
@cloudflare/workers-auth
@cloudflare/workers-editor-shared
@cloudflare/workers-utils
wrangler
commit: |
|
Codeowners approval required for this PR:
Show detailed file reviewers |
Co-authored-by: clanky-the-clanker[bot] <342202840+clanky-the-clanker[bot]@users.noreply.github.com>
There was a problem hiding this comment.
✅ Approved — The earlier import cleanup has been made, and nothing new turned up. The recheck logic is correct and well tested.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
No security issues found. The change only narrows when a refresh token is exchanged. It does not change how credentials are stored or sent.
📖 AGENTS.md: ✅ LGTM
This change does not need any AGENTS.md updates.
📋 Code Quality: ✅ LGTM
The earlier note is fixed: token-exchange.ts now has a single import type { AccessToken, OAuthFlowState, RefreshToken } from "./state". refreshToken() in flow.ts uses one stored-credential read both to decide whether to skip the refresh and to pick the token to exchange. Missing, invalid and exactly-now expiries all still go on to refresh, and the tests cover each case.
⚡ Performance: ✅ LGTM
No performance issues found. The extra storage read happens only on the refresh path, and it replaces the read that exchangeRefreshTokenForAccessToken used to do.
📝 Documentation: ✅ LGTM
The changeset and JSDoc match the new behaviour.
Reviewed commit: f4501704
workers-devprod
left a comment
There was a problem hiding this comment.
Codeowners reviews satisfied
Addresses #15089. Alternative approach to #15136.
When another Wrangler process has already refreshed our credentials, reuse its valid access token instead of immediately refreshing again. This prevents an unnecessary extra rotation that can leave an already-used refresh token on disk.
How the failure happens
The expiry check and refresh-token read currently happen separately. A process can decide to refresh using an old expiry time, then read a newer refresh token written by a sibling. It goes ahead with a refresh that is no longer needed.
For example, A, B, and C all start with expired credentials and refresh token
T0:T0. The server returns the same replacement,T1, to both.T1. A has not saved its response yet.T1, exchanges it forT2, and savesT2.T1overT2.T1from disk. C already used it, and the server's reuse tolerance has elapsed, so the refresh fails.The refresh requests can all be quick. The stale token remains on disk until a later refresh needs it.
What changes
Immediately before an exchange,
refreshToken()now reads the complete credential snapshot again:In the example above, C sees B's valid access token and skips step 3.
T1is never consumed by C. A's late write consequently saves the same, still-unusedT1already on disk, and the later refresh succeeds.The fresh-read behavior introduced by #13910 is preserved. Both the expiry decision and token selection now use that fresh read.
Why prefer this to a file lock for this failure?
The problem in this sequence is the unnecessary
T1→T2refresh. Checking current credentials removes that request, which also removes the opportunity for A to overwrite its result.This uses the existing credential-storage interface, including file and keyring-backed storage. It adds no lock files, ownership records, PID checks, stale-lock recovery, waiting, or lock timeouts. There is no new dependency or platform-specific coordination to maintain.
This does not serialize every refresh. Ordinary overlapping exchanges of the same token remain possible; in five production trials, both requests returned the same usable replacement. The change prevents a caller that has already fallen behind a sibling from unnecessarily advancing to another token generation.
Validation
T1, C exchanged it forT2; deliberately saving A's heldT1overT2causedinvalid_grantafter a real 65-second wait. RetainingT2passed the matching control. All three initial HTTP requests in the failing trial took less than a second.Scope: this fixes the reproduced unnecessary-refresh sequence. The issue's logs do not establish that exact ordering, and this is not a guarantee against every possible long process suspension after reading a token. A full cross-process lock provides broader serialization guarantees. Keeping this as a draft allows validation against the original workload before deciding whether those broader guarantees are needed.