From c641a8c1c542caf9c915fdd389c9a847724870c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=85=E8=86=80?= Date: Fri, 23 Oct 2026 23:21:15 +0800 Subject: [PATCH] fix(runtime): bound shared in-flight waits A canceled request can leave its shared promise pending forever. Bound joins and coordinate a single replacement so later requests make progress without multiplying upstream work. --- server/lib/lib/shared-flight.ts | 81 +++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 server/lib/lib/shared-flight.ts diff --git a/server/lib/lib/shared-flight.ts b/server/lib/lib/shared-flight.ts new file mode 100644 index 0000000..7c190c2 --- /dev/null +++ b/server/lib/lib/shared-flight.ts @@ -0,0 +1,81 @@ +/** + * In-flight work shared by concurrent callers in one isolate (one token + * exchange, one marker read, one setup run instead of one each). + * + * A run is started by one request and awaited by others. In Workers the + * starting request can end or be canceled before its I/O settles; its + * promise then never settles and its cleanup never runs. So another request + * waits for a run only until `maxWaitMs` after it started, and a run that + * old is never joined: the first caller to find it stuck starts a + * replacement and links it from the stuck run (`replacedBy`), so every other + * waiter of that run follows the link — even once the replacement has + * finished and left the map — and a stuck run is replaced once, not once per + * waiter. Nothing waits forever on runs that cannot finish: a caller that + * found `MAX_JOINS` runs stuck runs alone. + */ +export interface Flight { + promise: Promise; + startedAt: number; + replacedBy?: Flight; +} + +export interface FlightMap { + get(key: K): Flight | undefined; + set(key: K, value: Flight): unknown; + delete(key: K): boolean; +} + +const STALE = Symbol("stale"); +/** Runs a caller waits on (and finds stuck) before it runs alone. */ +const MAX_JOINS = 3; + +export async function sharedFlight( + flights: FlightMap, + key: K, + run: () => Promise, + maxWaitMs: number, +): Promise { + let current = flights.get(key); + for (let joined = 1; current; joined++) { + const left = current.startedAt + maxWaitMs - Date.now(); + if (left > 0) { + let timer: ReturnType | undefined; + const timeout = new Promise((resolve) => { + timer = setTimeout(() => resolve(STALE), left); + }); + try { + const result = await Promise.race([current.promise, timeout]); + if (result !== STALE) return result as T; + } finally { + clearTimeout(timer); + } + } + // `current` is stuck. + if (joined >= MAX_JOINS) return run(); + if (current.replacedBy) { + current = current.replacedBy; + continue; + } + const flight = start(flights, key, run, current); + current.replacedBy = flight; + return flight.promise; + } + return start(flights, key, run).promise; +} + +/** Start a run and publish it, unless another run took the key meanwhile. */ +function start( + flights: FlightMap, + key: K, + run: () => Promise, + replacing?: Flight, +): Flight { + const flight: Flight = { promise: run(), startedAt: Date.now() }; + const published = flights.get(key); + if (!published || published === replacing) flights.set(key, flight); + const done = (): void => { + if (flights.get(key) === flight) flights.delete(key); + }; + flight.promise.then(done, done); + return flight; +}