Skip to content

fix(account): a sign-out wins over sign-ins on their way, and unconfirmed revokes are retried - #3193

Closed
devswha wants to merge 4 commits into
mainfrom
fix/account-signout-races
Closed

devswha wants to merge 4 commits into
mainfrom
fix/account-signout-races

Conversation

@devswha

@devswha devswha commented Nov 6, 2026 •

Copy link
Copy Markdown
Owner

Fixes #3164.

Problem

Two sign-out races in the account client, both reproduced with the fake account service:

  1. Sign in again restored a session after sign-out. An ordinary sign-in (redirect, device code, native Apple) stored its session unconditionally. With a sign-in older than ten minutes, starting a method removal, choosing Sign in again and signing out in another tab before the return came back left a live session after the sign-out, and the queued removal then ran.
  2. A failed revoke was treated as done. When a link finished after a sign-out, its cleanup POST /v1/signout ignored the answer: a 429 (or 5xx, or a network error) left the store empty while the discarded refresh token still redeemed.

Fix

  • Sign-out epoch. The account store keeps an epoch that every sign-out replaces in the same transaction that removes the session. Every sign-in remembers the epoch when it starts (beginSignIn puts it in the pending record; the device flow and native Apple sign-in capture it before they start) and is stored only while the epoch is unchanged (keepSignIn). One that finishes after a sign-out is ended at the service and throws SignedOutError, so signInAgain never repeats the method change. clear() drops the epoch, so it counts as a sign-out too.
  • Revocations that must be confirmed. endAtService refreshes an expired access token first, then treats only 204 (or 401, already over) as ended. endSignIn keeps a sign-in it could not end in the store (ending, capped at 16) and AccountService.endLeftovers() ends those again; it runs at every sign-out and whenever the account screen (Settings, or the hosted shell's list) loads. signOut() uses the same path, so a sign-out the service did not confirm is retried later too, and still throws so the caller can log it.

No new UI or setting. Existing behaviour for a sign-in that finishes normally is unchanged. A sign-in started by an earlier build and finished after the update (no epoch) is not kept: that person signs in once more.

Validation

  • Failing-first: the three new tests in src/lib/account/methods.test.ts fail on origin/main (the source changes stashed) and pass with the fix.
    • re-authentication returning after a sign-out is ended, nothing is stored, and the queued method change finds no session;
    • a device sign-in approved after a sign-out is ended, not stored;
    • a link's sign-in whose cleanup revoke gets 429 is ended by endLeftovers() once the limit window passes (its refresh token no longer redeems).
  • bun test src/lib/account/ src/lib/relay/: 277 pass. One existing test (takes back the session of a sign-in cancelled while it was being written) now hooks updateMany, which the device flow writes through.
  • bun run check fast: green (typecheck, build, unit 12800 pass).

Review round (omo + Codex at 36fe1ab)

Fixed, each with a test that fails at 36fe1ab where noted:

  • Desktop sign-in took its epoch only after bridge.start() (and redirect sign-ins after the PKCE digest): the epoch is now taken first (test).
  • A pending sign-in from an earlier build (no epoch) was kept after a sign-out: it is no longer kept (the person signs in again once).
  • Leaving the account cleared ending: clear() keeps ending and moves the session it forgets into it (test).
  • A 401 to /v1/signout counted as ended: it is retried with the refresh token, and only a refused refresh counts as over (test).
  • A sign-in replacing another, and a refresh no tab keeps, left the other family live: both are ended now.
  • addMethod could report success after sign-out + new sign-in of the same account: it now checks the epoch.
  • signOut() waited behind the lock before changing the epoch, and leftovers were ended under the lock: the epoch changes at once, and endLeftovers() runs outside the lock.

(The 16-entry cap on ending mentioned in round 1 was removed in round 3.)

Review round 2 (Codex + omo at 22456cf)

  • HIGH (introduced by round 1): ending a replaced sign-in let an in-flight request's 401 retry run with another account's sign-in. call() now repeats a request only for the account it was made for, otherwise SignedOutError (test; it also closes the older trigger of the same retry).
  • The device flow now ends the sign-in it replaces too (test); addMethod throws SignedOutError instead of returning a revoked session when a sign-out lands during its cleanup.
  • Tests added for the round-1 behaviours: a new sign-in ends the one it replaces; a link is not kept across sign-out + same-account sign-in; a refresh answered after another sign-in took its place is ended.

Review round 3 (merge pass HOLD at 034269e)

Each fix has a test that fails at 034269e:

  • signOut() put the session on the list to end only after the revoke failed, so a page closed while /v1/signout was pending lost it. It now goes onto ending in the same transaction that drops the session and changes the epoch, and leaves it only once the service confirms; endSignIn does the same.
  • call()'s 401 retry checked only the account. It now also refuses to repeat after a sign-out (epoch changed), so a method removal queued before a sign-out never runs with the next sign-in of the same account.
  • A sign-in or link coming back after a sign-out was still redeemed at /v1/token (a link could add the identity when the revoke had failed). finishSignIn now checks the epoch before redeeming.
  • ending is no longer capped: an unconfirmed revocation is never dropped.

Remaining LOW: the IndexedDB clear() path is exercised only through the memory store in unit tests.

@coderabbitai

coderabbitai Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 30 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 4 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between c46dd12 and ead7c40.

Files selected for processing: 12

  • changelog.d/account-signout-wins.md
  • docs/account.md
  • src/components/AccountSection.tsx
  • src/lib/account/account.test.ts
  • src/lib/account/desktop.ts
  • src/lib/account/deviceSignIn.ts
  • src/lib/account/methods.test.ts
  • src/lib/account/native.ts
  • src/lib/account/service.ts
  • src/lib/account/signin.ts
  • src/lib/account/store.ts
  • src/lib/relay/shell.tsx
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@devswha

devswha commented Nov 6, 2026

Copy link
Copy Markdown
Owner Author

Review record (CodeRabbit skipped: review limit reached)

This PR touches authentication, so the code-review agents' findings and their resolution are recorded here.

Round 1 (omo BLOCK, Codex request changes at 36fe1ab): 5 HIGH + 5 MEDIUM, all reproduced with the fake account service. Fixed in 22456cf: desktop sign-in epoch taken before the loopback opens; a pre-update pending sign-in without an epoch is not kept; clear() (leaving the account) keeps ending and the session it forgets; a 401 to /v1/signout is retried with the refresh token; replaced sign-ins and discarded refresh rotations are ended; addMethod checks the epoch; signOut() changes the epoch before waiting for the lock, and endLeftovers() runs outside the lock. Not changed: ending stays capped at 16 (fixed bound on stored tokens).

Round 2 (Codex at 22456cf): one HIGH introduced by round 1 (an in-flight request's 401 retry could run with another account's sign-in after the replaced one was ended). Fixed in 034269e: call() repeats a request only for the account it was made for. Also: the device flow ends the sign-in it replaces, and addMethod throws instead of returning a revoked session. omo's missing-test MEDIUM was closed with three tests.

Final (034269e): omo CLEAR (no HIGH/MEDIUM); Codex targeted check PASS (HIGH closed, no new HIGH).

Remaining LOWs (no follow-up issue):

  • A device sign-in cancelled after its session was written drops the session it replaced without ending it (local loss predates this PR).
  • Two endLeftovers() runs at once can log one needless warning and keep an entry until the next run.
  • The IndexedDB clear() path is covered only through the memory store in unit tests.

@devswha

devswha commented Nov 6, 2026

Copy link
Copy Markdown
Owner Author

Review record, round 3 (merge pass HOLD at 034269e -> ead7c40)

CodeRabbit still skipped (review limit). Fixed, each with a test that fails at 034269e:

  • signOut() now puts the session on ending in the same transaction that drops it (and changes the epoch), and removes it only once the service confirms; endSignIn adds before asking too. A page closed mid-sign-out loses nothing.
  • call()'s 401 retry also refuses after a sign-out (epoch changed), not only for another account.
  • finishSignIn checks the epoch before redeeming at /v1/token, so a link coming back after a sign-out with a failed revoke adds nothing.
  • ending is no longer capped.

omo at ead7c40: CLEAR, no HIGH/MEDIUM. Remaining LOWs (no follow-up issue):

  • A device sign-in cancelled right after being written drops the session it replaced without ending it (predates this PR).
  • A sign-out landing between the pre-redeem epoch check and /v1/token can still let a link be added (its sign-in is then ended).
  • Overlapping endLeftovers() runs on repeated loads repeat requests for the same entries.
  • ending has no age limit; the IndexedDB clear() path is covered only through the memory store.

@devswha

devswha commented Nov 6, 2026

Copy link
Copy Markdown
Owner Author

Parked as draft after the round limit.

What this PR fixes (#3164): a sign-out wins over any sign-in, re-authentication or link still on its way (sign-out epoch), and revocations the service did not confirm are kept and retried, including through a closed page and leaving the account.

The merge pass's delta review at ead7c40 reproduced:

  • HIGH service.ts:498: the epoch is compared only on the 401 retry; it must be captured when the call is made and checked before every attempt.
  • HIGH signin.ts:97: a link redemption already in flight still adds the identity when the local sign-out's revoke fails; this needs the account service to bind link redemption to a still-valid session.
  • MEDIUM: the uncapped retry list ignores 429/Retry-After; it needs backoff and a storage bound.

These are tracked in #3198. The work resumes as one design pass across the client and the account service.

@devswha

devswha commented Nov 9, 2026

Copy link
Copy Markdown
Owner Author

Closing (owner decision): a rare two-window race; #3164 stays open to revisit when account use grows.

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.

Account: a sign-out can lose to an in-flight sign-in, re-authentication or link cleanup

1 participant