fix(oauth): refresh a login once across worker processes
Issue
Fixes #1807
What changed
e2e runstarts each worker as its own process, butcreateOAuthFetchshared a refresh only inside one process. With--workers 4and an expired subscription login, four workers sent the same refresh token. ChatGPT, SuperGrok, and OpenCode Console rotate refresh tokens, so they rejected all but one.FileCredentialStore.exclusive(providerId, refresh)holds a lock file per login,oauth.json.<provider>.refresh.lock, across the refresh's re-read of the store, its token request, and its write (packages/e2e/src/oauth/store.ts). A worker that waited for the lock re-reads the store, finds the fresh tokens, and sends no refresh.- The lock is
withFileLock()inpackages/e2e/src/internal/file-lock.ts. The store's existing write lock uses it too. - The lock file names its holder: host and pid namespace, pid, and a random token. The holder touches the file every 2.5 s while it holds the lock, and touches and removes only a lock that holds its own token.
- A waiter takes the lock over in two cases:
- at once, when the holder's pid on this host and pid namespace has exited
- once the lock has gone untouched for 10 s, whoever it names
- The 10 s bound covers locks the pid cannot vouch for: another host's or sandbox's, an empty lock from an older e2e, and a pid another program now has. Waiters take turns through a
.breakfile to remove a lock and check the holder again on their turn, so a lock created after the first check is never removed. - A waiter gives the lock 30 s, then fails with
OAuthError('TIMEOUT'). The holder's token request has no deadline: the vendor may rotate the token before a late answer, and dropping that answer would leave the stored token revoked. - The 2 s poll for a token another process rotated (
rotatedElsewhereinpackages/e2e/src/oauth/fetch.ts) stays, and now runs after the lock is released. An e2e released before this change refreshes without the lock, so a run of it beside a run of this one still needs the poll. E2E_OAUTH_CREDENTIALSis unchanged. That store cannot save anything, so each process still refreshes on its own, asdocs/subscriptions.mdxalready says.
sequenceDiagram
participant W1 as worker 1
participant W2 as worker 2..4
participant L as oauth.json.openai.refresh.lock
participant V as vendor
W1->>L: create, holder named
W2->>L: wait (holder runs, lock touched)
W1->>V: refresh rt-0
V-->>W1: at-1, rt-1
W1->>L: write oauth.json, release
W2->>L: take, re-read oauth.json
Note over W2: at-1 is fresh, no refresh sent
Verified
Ran it locally: yes
- Scratch project with test files that each call
generateText({ model: chatgpt('gpt-test') })through the reale2e/oauth/chatgptconstructor, run withnpx e2e run --workers 4 --no-cache. A fakeauth.openai.comandchatgpt.comrotates refresh tokens and answers a reused one with400 refresh_token_reused. The stored login starts expired. - The main column ran at
2030b175, the merge base before main was merged in, built in its own worktree. Branch rows ran at6e07fa71or later. The rows marked4b1bf6d3ran again at the head, after merging main, and the row marked4747e779just before it.
| Vendor behavior | main | this branch |
|---|---|---|
| Refresh answers in 400 ms | 4 passed, 4 refreshes of rt-0 (3 rejected) | 4 passed, 1 refresh |
Refresh answers in 2.5 s (4b1bf6d3) | 3 of 4 failed: ChatGPT token request failed (400: refresh_token_reused); run \npx e2e login openai`` | 4 passed, 1 refresh |
Reuse revokes the token family (4b1bf6d3) | 4 of 4 failed, stored login dead | 4 passed, 1 refresh |
| Refresh answers in 12 s | same failure as the 2.5 s row | 4 passed, 1 refresh |
Refresh answers in 25 s (5d95a6ed) | same failure as the 2.5 s row | 4 passed, 1 refresh, the new token stored |
First token request never answers (5d95a6ed) | 3 passed, 1 TEST_TIMEOUT after 120 s | 0 passed: the holder hit TEST_TIMEOUT, and 3 waiters failed after 30 s with another e2e process is still refreshing the openai login after 30 s; run again once it finishes |
| Revoked login, 8 workers | 8 LOGIN_REQUIRED, 6.0 s | 8 LOGIN_REQUIRED, 5.8 s |
Lock left by an exited worker (4747e779) | not applicable | 4 passed, 1 refresh |
| Empty lock 60 s old, as an older e2e leaves it | not applicable | 4 passed, 1 refresh, about 10 s |
| Lock naming live pid 1, untouched for 1 hour | not applicable | 4 passed, 1 refresh, 4 s |
| A main run and a branch run at once, 2 workers each, the branch run starting 0.2 s later | 4 passed (main beside main) | 4 passed. Without the kept poll, the branch run failed in 5 of 6 trials |
Vendor log, 2.5 s case, main:
07.919 refresh rt-0 -> 400 refresh_token_reused
07.943 refresh rt-0 -> 400 refresh_token_reused
07.950 refresh rt-0 -> 400 refresh_token_reused
10.361 refresh rt-0 -> 200 at-1 rt-1
10.373 responses at-1 -> 200
Same case, this branch:
04.184 refresh rt-0 -> 200 at-1 rt-1
04.197 responses at-1 -> 200
04.232 responses at-1 -> 200
04.234 responses at-1 -> 200
04.242 responses at-1 -> 200
Process-level checks of the lock at 0be69e58, outside the CLI. The lock's logic has not changed since, only its module and how it writes the holder token:
- 16 processes started in the same millisecond on an orphaned lock: one holder at a time in 10 of 10 trials.
- A caller inside a
bwrap --unshare-pidsandbox waited for a holder outside it: the log reads+holder -holder +caller -caller. Before the pid namespace was part of the holder's name, the caller entered while the holder held the lock. - A holder stopped with
kill -STOPfor 11 s mid-refresh lost its lock to the next waiter:+A +B -B -A. That is the accepted cost of the 10 s bound, below.
Tests: file-lock.test.ts covers the lock, and store.test.ts and fetch.test.ts cover the store and the refresh. passes on any other refresh failure as it is passes on the merge base too: it pins main's behavior through the restructured refresh.
Each race mutation below was checked against the test meant to catch it. Every cell is 10 runs, on this 8-CPU Linux machine:
| Mutation | Test | All CPUs | Pinned to 1 CPU | 2 CPUs under load |
|---|---|---|---|---|
Takeover turn removed (no .break file) | lets one waiter through a crashed holder's lock when another takes it over during the check in its takeover turn | 10/10 | 10/10 | 10/10 |
| Re-check in the turn removed | both lets one waiter through a crashed holder's lock cases | 10/10 | 10/10 | 10/10 |
| Re-read inside the refresh lock removed | uses the token another process stored while this one waited for the lock, sending no refresh | 10/10 | 10/10 | not run |
The two-waiter tests start a second real waiter inside the first one's pid check, so the race happens on every run. With the pid namespace dropped from the holder's name, names its host, pid namespace, and pid in the lock fails. The crash-loop test (12 processes, half the holds ending like a crash, until 300 crashes) is a soak check on real processes. With the turn removed it failed 15 of 15 runs on all CPUs, 8 of 8 on 1 CPU, and 6 of 8 on 2 CPUs under load. Unmutated, it passed 31 of 31.
The testbed and benchmarks were not run: none of their configs uses a subscription login, so this change does not reach them.
For maintainers
- The lock is hand-written, about 170 lines in
src/internal/file-lock.ts: holder token, pid and pid-namespace check, heartbeat, and the.breakturn. No lock helper exists in the repo.proper-lockfileuses the same heartbeat-and-stale design and would replace most of it, at the cost of a dependency. - A holder paused for more than 10 s mid-refresh (a debugger, laptop sleep) loses its lock, and a second refresh can go out. Judging only by pid would keep its lock, but then a pid another program has since been given would block every run until someone removes the file. I chose the heartbeat bound.
- A token request that never answers now fails the other workers of that run after 30 s, where on main they each refreshed on their own. A deadline on the request would let the next worker refresh, but it also drops an answer that arrives late after the vendor rotated the token, and that revokes the stored login. I chose to lose a run rather than the login.
- With retries on, a retried holder joins the same hung request, so its test times out twice.
- The run leaves the lock file behind. The next run takes it over at once, because the pid it names has exited.
- The stale bound is 10 s. A loaded CI machine that pauses a holder longer loses its lock. 20 to 25 s would still sit under the 30 s wait.
- On Windows,
claim()may getEPERMfrom a lock file that another process is deleting, and that fails the refresh. I have not run this on Windows. A retry onEPERMwould cover it. - A judgment step (
assert,waitFor,extract) has a 30 s default budget, the same as the lock wait. Such a step waiting on another worker's refresh ends withSTEP_TIMEOUTrather than the lock'sTIMEOUT. The docs say so.
🤖 Generated with Claude Code