Repository navigation
fix(lock): expire MemoryLocker locks whose request ended on Cloudflare Workers - #2540
Conversation
…e Workers On Workers a timer dies with the request that started it, so a holder or waiter whose request ended left its key locked forever. Lock expiry is now a deadline on performance.now(), enforced by later callers and by every waiter's own timers, which wake by the time a waiter ahead could expire. A handover starts the new holder's ttl, and waiters whose timeout passed are skipped.
More templates
@orpc/ai-sdk
@orpc/arktype
@orpc/bun
@orpc/client
@orpc/cloudflare
@orpc/contract
@orpc/effect
@orpc/evlog
@orpc/hibernation
@orpc/json-schema
@orpc/experimental-lock
@orpc/mcp
@orpc/msw
@orpc/nest
@orpc/next
@orpc/node
@orpc/openapi
@orpc/experimental-openapi-inference
@orpc/opentelemetry
@orpc/pinia-colada
@orpc/pino
@orpc/publisher
@orpc/ratelimit
@orpc/server
@orpc/shared
@orpc/swr
@orpc/tanstack-query
@orpc/trpc
@orpc/valibot
@orpc/zod
commit: |
|
No actionable comments were generated in the recent review. Summary
Merge Risk:
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Merging this PR will not alter performance1 warning
Comparing Footnotes
|
Deploying with
|
| Status | Name | Latest Commit | Preview URL | Updated (UTC) |
|---|---|---|---|---|
| ✅ Deployment successful! View logs |
orpc | 87697cf | Commit Preview URL Branch Preview URL |
Nov 23 2026, 09:37 AM |
There was a problem hiding this comment.
Test payload sanity check.
deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏
| wakeAt = Math.min(wakeAt, now + ahead.ttl) | ||
| } | ||
|
|
||
| timer = setTimeout(check, Math.min(MAX_TIMEOUT_DELAY, Math.max(1, wakeAt - now))) |
There was a problem hiding this comment.
ℹ️ Minor suggestions only. One unbounded wake loop worth a look; otherwise this is a solid fix.
(Heads-up: an earlier submission of mine — "Test payload sanity check." with a short body comment — was a malformed tooling payload, not a review. This one supersedes it.)
Approach: sound — expiry becomes a performance.now() deadline that whichever request is still alive enforces, instead of depending on a timer the holder started. Same shape of fix as #2535, and it does resolve the "locked for good" failure on Workers, including the waiter-handover case.
Reviewed changes
- Deadline-based entry —
MemoryLockEntry.expiresAtreplaces the stored timer handle, andlock()starts withreleaseExpired(), so any later caller reclaims a holder whose deadline passed (also deleting an expired entry nobody waits for). - Waiter wake chain —
wait()recordsttl/deadline, keeps a separatedeadlineTimerso timeouts are not late on Node's early-firing timers, and arms a wake timer atmin(deadline, entry.expiresAt, now + ttl of each waiter ahead). - Handover —
release()skips waiters whosedeadlinepassed, installs the next live one withexpiresAt = now + next.ttl, and drops the entry when nobody took the lock. - Timer overflow —
MAX_TIMEOUT_DELAYmakesttl/timeoutofInfinityor above 2^31-1 skip their timers instead of overflowing to 1ms. - Tests — 13 new cases, incl. a
withoutTimers()helper that stubsglobalThis.setTimeoutto stand in for an ended request, plus the two follow-ups covering a handover where every waiter had already timed out and a settled waiter whoseclearTimeoutnever lands.
I ran packages/lock (48 passed, 18 skipped) plus a scratch fuzz harness mixing never-settling holders with 200 live callers on one key: mutual exclusion held (maxActive === 1), every live caller completed with timeout: Infinity, and entries drained to 0. A waiter's captured entry can never diverge from the map's entry while it is live, because an entry leaves the map only once all of its waiters are settled.
deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

Follow-up to #2534 and #2535. Those fixed the lazy router. This fixes the same problem in
MemoryLocker.Problem
On Cloudflare Workers, a timer dies with the request that started it, and
clearTimeoutfrom another request does nothing.MemoryLockeronly expired a holder through asetTimeoutthat the holder itself started. So if the holder's request ended, for example because the client disconnected, the key stayed locked for good. Every later caller then timed out. The same thing happened when a waiter was handed the lock after its own request had already ended.Fix
Lock expiry is now a deadline on
performance.now(), enforced by whichever request is still running:lock()call takes over a holder whose time has passed.expiresAt, ornow + ttlof any waiter still ahead of it. Every check runs from the waiter's own request.The holder's own expiry timer stays as a fallback, so on Node an expired lock is still freed promptly when nobody is waiting.
ttlandtimeoutabove 2^31-1, includingInfinity, skip or clamp their timers instead of overflowing to 1ms.Inside a Durable Object all requests share one context, so behaviour there is unchanged.
Notes for reviewers
performance.now()reaches the target, so the timer, not the clock, decides when a waiter times out. Folding it into the wake timer made timeouts up to about 1ms late. The testtimes out by its own timer when only setTimeout is fakedcovers this.Tis queued ahead, the waiters behind it wake about once everyTfor as long as the lock is held. That waiter could get the lock at any moment, from another request, and its own request may already have ended. Each wake scans the waiters ahead, which is linear in the waiter's position. With the same ttl for everyone, this is about one wake per ttl. A cached bound from join time would be cheaper but goes stale: a waiter behind a short-ttl waiter that left would keep waking every 1ms.stops waking often once the short-ttl waiters ahead have leftcovers this.Testing
setTimeoutto simulate a request that has ended. They cover a dead holder, a dead waiter handed the lock, a handover to a shorter ttl, skipping timed-out waiters, a system clock jump, timers past the 2^31-1 limit, and how often waiters wake.handle_cross_request_promise_resolutionon, the default since 2024-10-14.@cloudflare/vitest-pluginalways turns this flag off, so the package's workerd tests can't reproduce the bug. Holders and waiters whose requests end early used to leave the key locked forever. Now the lock is acquired after the ttl.pnpm checkpasses. The lock tests pass (46).