Skip to content

fix(auth): wait out a concurrent refresh until its lock goes stale - #189

Merged
scottlovegrove merged 2 commits into
mainfrom
scottl/refresh-lock-wait
Nov 25, 2026
Merged

scottlovegrove merged 2 commits into
mainfrom
scottl/refresh-lock-wait

Conversation

@scottlovegrove

Copy link
Copy Markdown
Collaborator

Raised in this review on todoist/todoist-cli#678. A process waiting on the refresh lock gives up after 2s (LOCK_WAIT_TIMEOUT_MS), but the holder's token request alone can take up to 10s (the PKCE and DCR providers' REFRESH_TIMEOUT_MS), plus the store reads and writes around it. With one-hour access tokens (todoist-cli since 7.4.0, comms-cli, outline-cli, the Automations CLI), several commands started together after the token has expired, such as a laptop waking or an agent running calls in parallel, hit this whenever the refresh is slow: the holder refreshes fine and every waiter fails with AUTH_REFRESH_TRANSIENT: Timed out waiting for a concurrent refresh to complete. todoist-cli only falls back to the stored token while it is still valid, so once it has expired the command fails.

Waiters now wait just past LOCK_STALE_MS (15s), the point where cli-core already treats a holder as dead. The wait ends either with the lock released, after which the waiter re-reads under the lock and adopts the rotated bundle without sending a second request, or with the lock stolen once it is stale. The trade-off: a waiter behind a crashed holder (Ctrl-C mid-refresh) waits up to 15s instead of failing after 2s. Today it fails, and so does every retry until the lock goes stale.

The new test holds the lock for 14s of simulated time (a Date.now spy, with the real 50ms polls) and fails on main with the timeout error. Under the new limit the existing timeout test would need 16s of real time, so it drives the clock the same way and still checks that a waiter whose wait runs out on a fresh lock throws AUTH_REFRESH_TRANSIENT without sending a request. I also ran three concurrent refreshes against the built package with a fake token endpoint taking 3s: on main the two waiters fail, on this branch all three get the new token from one request (and still do at 9s).

A process waiting on the refresh lock gave up after 2s, but the holder
may need up to 10s for the token request alone, plus the store reads
and writes around it. With one-hour access tokens, several commands
started together after the token expired (a laptop waking, an agent
firing calls in parallel) now hit this routinely: the holder refreshes
fine, and every waiter fails with "Timed out waiting for a concurrent
refresh to complete."

Waiters now wait just past the 15s stale threshold, so the wait ends
with the lock released (and the rotated bundle adopted) or stolen once
it counts as abandoned.
@scottlovegrove scottlovegrove self-assigned this Nov 25, 2026

@doistbot doistbot left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR widens the refresh-lock waiter timeout from 2s to just past the 15s stale threshold so waiters adopt a slow-but-alive holder's rotated token instead of failing, with clock-driven tests covering both the adoption and timeout paths and a matching README update. No inline issues were flagged; one edge case worth knowing about (not necessarily fixing): a waiter whose lock gets stolen by another waiter inherits a fresh lock with almost no wait time left on it.

I also included a few optional follow-up notes in the details below.

Optional follow-up notes (2)
  • P3 src/auth/refresh.ts:68: The fixed deadline leaves a gap when another waiter steals the stale lock. Scenario: a holder crashes mid-refresh with two waiters. At ~15s the first waiter steals and starts a refresh that can take up to 10s (the providers' REFRESH_TIMEOUT_MS); the second waiter's deadline is ~1s later, tryAcquire keeps failing on the fresh lock, and it throws AUTH_REFRESH_TRANSIENT and re-reads once before the new holder has finished — the same failure this PR set out to fix, just shifted to a rarer case. Resetting the deadline when the observed lock changes (e.g. compare the lock's token or mtime each poll and re-arm the staleness window on change) would make a waiter behind a re-taken lock wait a full staleness window too.
  • P3 src/auth/refresh.test.ts:239: The lock's mtime comes from the real clock (writeFile at L227) while Date.now is mocked, so after now += 14_000 the age the waiter computes is 14s plus however much real time passed between the write and this line. That leaves only ~900ms of slack before lockIsStale trips: if the runner stalls for about a second there (plausible with a loaded vitest pool), the waiter steals the lock, POSTs, and the test fails with refreshSpy called. Pin the mtime to the mocked clock instead, e.g. await utimes(lockPath, new Date(now), new Date(now)) right after installing the spy at L235, so the jump produces exactly a 14s age regardless of real elapsed time.

Share Feedback • Review Logs • Trace

The lock's mtime came from the real clock while Date.now was mocked, so
real time spent between writing the lock and the 14s jump counted
towards its age. A runner stalled for about a second would see it as
stale, steal it and POST. Pin the mtime to the mocked clock so the jump
gives an exact 14s age.
@scottlovegrove
scottlovegrove merged commit 675865e into main Nov 25, 2026
5 checks passed
@scottlovegrove
scottlovegrove deleted the scottl/refresh-lock-wait branch November 25, 2026 10:43
doist-release-bot Bot added a commit that referenced this pull request Nov 25, 2026
## [2.0.2](v2.0.1...v2.0.2) (2026-11-25)

### Bug Fixes

* **auth:** wait out a concurrent refresh until its lock goes stale ([#189](#189)) ([675865e](675865e))
@doist-release-bot

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 2.0.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants