From 7996c49487398a6bccf9e0d2780e940a1210d023 Mon Sep 17 00:00:00 2001 From: Afloat Date: Wed, 18 Nov 2026 18:49:59 +0800 Subject: [PATCH] fix: abort superseded cache keepalive requests Let each snapshot own cancellation through response-body consumption and release its upstream connection when replaced or disarmed. Preserve the existing timeout and failure policy, including early Node 20 support. Fixes #7142 --- src/cache-keepalive.ts | 98 ++++++----- tests/issue7142-keepalive-flight.test.ts | 210 +++++++++++++++++++++++ 2 files changed, 266 insertions(+), 42 deletions(-) create mode 100644 tests/issue7142-keepalive-flight.test.ts diff --git a/src/cache-keepalive.ts b/src/cache-keepalive.ts index 1dcb82fd07..036e82d4cc 100644 --- a/src/cache-keepalive.ts +++ b/src/cache-keepalive.ts @@ -176,6 +176,7 @@ interface KeepaliveEntry { lastHitAt?: number; timerId: ReturnType | undefined; pingInFlight: boolean; + pingController?: AbortController; } const registry = new Map(); @@ -345,6 +346,7 @@ function armInternal(inp: KeepaliveArmInput): void { pingInFlight: false, }; registry.set(inp.sessionId, entry); + prev?.pingController?.abort(); entry.timerId = scheduleFn(() => { void firePing(entry); }, entry.cadenceMs); @@ -372,52 +374,63 @@ async function firePing(entry: KeepaliveEntry): Promise { return; } entry.pingInFlight = true; + const controller = new AbortController(); + entry.pingController = controller; + // Keep cancellation live until the response body is consumed. Relay the + // deadline without AbortSignal.any, which is absent in early Node 20. + const timeout = AbortSignal.timeout(PING_TIMEOUT_MS); + const onTimeout = () => controller.abort(timeout.reason); + timeout.addEventListener("abort", onTimeout, { once: true }); entry.lastPingAt = clock(); - let resp: Response; try { - resp = await sendPing(entry); - } catch (err) { - entry.pingInFlight = false; - handleFailure(entry, `network: ${err instanceof Error ? err.message : String(err)}`); - return; - } - entry.pingInFlight = false; - if (registry.get(entry.sessionId) !== entry) return; - let text: string; - try { - text = await resp.text(); - } catch (err) { - if (resp.status !== 429) { - handleFailure(entry, `response body: ${err instanceof Error ? err.message : String(err)}`); + let resp: Response; + try { + resp = await sendPing(entry, controller.signal); + } catch (err) { + handleFailure(entry, `network: ${err instanceof Error ? err.message : String(err)}`); return; } - text = ""; - } - if (registry.get(entry.sessionId) !== entry) return; - if (!resp.ok) { - if (resp.status === 429) { - disarm(entry.sessionId, "rate-limited(429)"); + if (registry.get(entry.sessionId) !== entry) return; + let text: string; + try { + text = await resp.text(); + } catch (err) { + if (resp.status !== 429) { + handleFailure(entry, `response body: ${err instanceof Error ? err.message : String(err)}`); + return; + } + text = ""; + } + if (registry.get(entry.sessionId) !== entry) return; + if (!resp.ok) { + if (resp.status === 429) { + disarm(entry.sessionId, "rate-limited(429)"); + return; + } + handleFailure(entry, `http ${resp.status}: ${text.slice(0, 200)}`); return; } - handleFailure(entry, `http ${resp.status}: ${text.slice(0, 200)}`); - return; - } - let usage: Record | undefined; - try { - const j = JSON.parse(text) as { usage?: unknown }; - if (j && typeof j === "object" && j.usage && typeof j.usage === "object") usage = j.usage as Record; - } catch { - usage = undefined; - } - const cached = usage ? usageTotals("anthropic", usage).cached : undefined; - entry.pingCount += 1; - if (typeof cached === "number" && cached > 0) { - entry.failStreak = 0; - entry.lastHitAt = clock(); - loggerLog("info", `[${entry.sessionId}] [keepalive] ping#${entry.pingCount} hit=${cached}tok`); - reschedule(entry); - } else { - disarm(entry.sessionId, "prefix-dead(cache_read=0)"); + let usage: Record | undefined; + try { + const j = JSON.parse(text) as { usage?: unknown }; + if (j && typeof j === "object" && j.usage && typeof j.usage === "object") usage = j.usage as Record; + } catch { + usage = undefined; + } + const cached = usage ? usageTotals("anthropic", usage).cached : undefined; + entry.pingCount += 1; + if (typeof cached === "number" && cached > 0) { + entry.failStreak = 0; + entry.lastHitAt = clock(); + loggerLog("info", `[${entry.sessionId}] [keepalive] ping#${entry.pingCount} hit=${cached}tok`); + reschedule(entry); + } else { + disarm(entry.sessionId, "prefix-dead(cache_read=0)"); + } + } finally { + timeout.removeEventListener("abort", onTimeout); + entry.pingInFlight = false; + entry.pingController = undefined; } } @@ -439,7 +452,7 @@ function reschedule(entry: KeepaliveEntry): void { }, entry.cadenceMs); } -async function sendPing(entry: KeepaliveEntry): Promise { +async function sendPing(entry: KeepaliveEntry, signal: AbortSignal): Promise { const headers: Record = {}; for (const [k, v] of Object.entries(entry.headers)) { const lk = k.toLowerCase(); @@ -453,7 +466,7 @@ async function sendPing(entry: KeepaliveEntry): Promise { method: "POST", headers, body: buildPingBody(entry.body), - signal: AbortSignal.timeout(PING_TIMEOUT_MS), + signal, }; if (entry.dispatcher) init.dispatcher = entry.dispatcher; return await fetch(entry.upstreamUrl, init as RequestInit) as Response; @@ -464,6 +477,7 @@ function disarm(sessionId: string, reason: string): void { if (!entry) return; registry.delete(sessionId); if (entry.timerId !== undefined) cancelFn(entry.timerId); + entry.pingController?.abort(); loggerLog("info", `[${sessionId}] [keepalive] disarmed reason=${reason} pings=${entry.pingCount}`); } diff --git a/tests/issue7142-keepalive-flight.test.ts b/tests/issue7142-keepalive-flight.test.ts new file mode 100644 index 0000000000..07bf8e8ae4 --- /dev/null +++ b/tests/issue7142-keepalive-flight.test.ts @@ -0,0 +1,210 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import { test, type TestContext } from "node:test"; +import { Agent } from "undici"; +import { + _resetKeepaliveForTest, + _setKeepaliveSchedulerForTest, + keepaliveStatusFor, + noteSuccessfulForward, + type KeepaliveArmInput, +} from "../src/cache-keepalive.ts"; +import { _resetSessionsForTest, getSession } from "../src/session.ts"; + +const ENV_KEYS = [ + "BILI_CACHE_KEEPALIVE", "BILI_CACHE_KEEPALIVE_TTL_FACTOR", "BILI_CACHE_KEEPALIVE_MAX_DURATION_MS", + "BILI_CACHE_KEEPALIVE_MAX_PINGS", "BILI_CACHE_KEEPALIVE_ASSUME_TTL", + "BILI_CACHE_KEEPALIVE_ARM_WITHOUT_BREAKPOINTS", "BILI_CACHE_KEEPALIVE_INTERVAL_MS", +] as const; +const HIT = JSON.stringify({ usage: { input_tokens: 1, cache_read_input_tokens: 1234 } }); +const BODY = JSON.stringify({ + model: "keepalive-test", max_tokens: 32, stream: true, + system: [{ type: "text", text: "cached prefix", cache_control: { type: "ephemeral" } }], + messages: [{ role: "user", content: "hello" }], +}); +const NO_BREAKPOINT = JSON.stringify({ model: "keepalive-test", messages: [{ role: "user", content: "hello" }] }); + +function deferred(): { promise: Promise; resolve: (value: T) => void } { + let resolve!: (value: T) => void; + return { promise: new Promise(r => { resolve = r; }), resolve }; +} + +async function within(promise: Promise, label: string): Promise { + let timer: ReturnType | undefined; + try { + return await Promise.race([ + promise, + new Promise((_, reject) => { timer = setTimeout(() => reject(new Error(`${label} timed out`)), 2000); }), + ]); + } finally { + if (timer !== undefined) clearTimeout(timer); + } +} + +async function setup(t: TestContext, phase: "headers" | "body") { + const env = Object.fromEntries(ENV_KEYS.map(key => [key, process.env[key]])); + for (const key of ENV_KEYS) delete process.env[key]; + process.env.BILI_CACHE_KEEPALIVE = "1"; + process.env.BILI_CACHE_KEEPALIVE_MAX_PINGS = "1"; + _resetKeepaliveForTest(); + _resetSessionsForTest(); + const timers = new Map void>(); + let timerId = 0; + _setKeepaliveSchedulerForTest({ + schedule(fn) { const id = ++timerId; timers.set(id, fn); return id; }, + cancel(id) { timers.delete(id as number); }, + }); + const received = deferred(); + const consumingBody = deferred(); + const oldClosed = deferred(); + let held: http.ServerResponse; + let pingCount = 0; + const requests: Array<{ url: string; body: string }> = []; + const server = http.createServer((req, res) => { + let body = ""; + req.on("data", chunk => { body += chunk; }); + req.on("end", () => { + requests.push({ url: req.url ?? "", body }); + if (req.url === "/ping" && ++pingCount === 1) { + held = res; + res.once("close", () => oldClosed.resolve()); + if (phase === "body") { + res.writeHead(200, { "content-type": "application/json" }); + res.write(HIT.slice(0, 9)); + } + received.resolve(); + return; + } + res.writeHead(200, { "content-type": "application/json" }); + res.end(req.url === "/ping" ? HIT : JSON.stringify({ normal: true })); + }); + }); + await new Promise(resolve => server.listen(0, "127.0.0.1", resolve)); + const address = server.address(); + assert.ok(address && typeof address !== "string"); + const origin = `http://127.0.0.1:${address.port}`; + const dispatcher = new Agent({ connections: 1, pipelining: 1 }); + const realFetch = globalThis.fetch; + let pingSignal: AbortSignal | null | undefined; + globalThis.fetch = async (input, init) => { + if (input === `${origin}/ping`) pingSignal = init?.signal; + const response = await realFetch(input, init); + if (input === `${origin}/ping`) { + const readText = response.text.bind(response); + Object.defineProperty(response, "text", { + value: () => { consumingBody.resolve(); return readText(); }, + }); + } + return response; + }; + const sessionId = `keepalive-7142-${phase}-${t.name}`; + getSession(sessionId, { protocol: "anthropic" }); + const input: KeepaliveArmInput = { + sessionId, protocol: "anthropic", reqMethod: "POST", sidePassthrough: false, resignLane: false, + wireBody: BODY, upstreamUrl: `${origin}/ping`, headers: { "content-type": "application/json", "x-api-key": "old-test-key" }, + dispatcher, routes: undefined, + }; + t.after(async () => { + _resetKeepaliveForTest(); + globalThis.fetch = realFetch; + await dispatcher.destroy(); + server.closeAllConnections(); + await new Promise(resolve => server.close(() => resolve())); + _resetSessionsForTest(); + for (const key of ENV_KEYS) { + if (env[key] === undefined) delete process.env[key]; + else process.env[key] = env[key]; + } + }); + const fire = () => { + assert.equal(timers.size, 1); + const [id, callback] = timers.entries().next().value!; + timers.delete(id); + callback(); + }; + noteSuccessfulForward(input); + fire(); + await within(phase === "headers" ? received.promise : consumingBody.promise, "ping stage"); + return { + input, timers, fire, requests, oldClosed, getSignal: () => pingSignal, + finishOld() { + if (phase === "headers") held.writeHead(200, { "content-type": "application/json" }); + held.end(phase === "body" ? HIT.slice(9) : HIT); + }, + async normalRequest() { + const init: Omit & { dispatcher: object } = { + dispatcher, signal: AbortSignal.timeout(1000), + }; + const response = await realFetch(`${origin}/normal`, init as RequestInit); + assert.equal(response.status, 200); + assert.deepEqual(await response.json(), { normal: true }); + }, + }; +} + +for (const phase of ["headers", "body"] as const) { + for (const change of ["replace", "disable", "rotate", "evict"] as const) { + test(`${change} releases an in-flight keepalive waiting for ${phase}`, async t => { + const f = await setup(t, phase); + let currentSession: string | undefined; + if (change === "replace") { + noteSuccessfulForward({ ...f.input, wireBody: BODY.replace("hello", "new turn") }); + currentSession = f.input.sessionId; + } else if (change === "disable") { + process.env.BILI_CACHE_KEEPALIVE = "0"; + noteSuccessfulForward(f.input); + } else if (change === "rotate") { + noteSuccessfulForward({ ...f.input, wireBody: NO_BREAKPOINT, headers: { ...f.input.headers, "x-api-key": "new-test-key" } }); + } else { + currentSession = `${f.input.sessionId}-other`; + getSession(currentSession, { protocol: "anthropic" }); + noteSuccessfulForward({ ...f.input, sessionId: currentSession }); + } + await f.normalRequest(); + await within(f.oldClosed.promise, "invalidated ping close"); + assert.equal(f.getSignal()?.aborted, true); + assert.equal(f.requests.filter(request => request.url === "/normal").length, 1); + assert.equal(f.requests[0]!.body, JSON.stringify({ ...JSON.parse(BODY), max_tokens: 1, stream: false })); + if (currentSession) { + assert.equal(keepaliveStatusFor(currentSession)?.failStreak, 0); + assert.equal(keepaliveStatusFor(currentSession)?.pingCount, 0); + f.fire(); + await f.normalRequest(); + assert.equal(keepaliveStatusFor(currentSession)?.pingCount, 1); + assert.equal(keepaliveStatusFor(currentSession)?.failStreak, 0); + assert.equal(f.timers.size, 1); + } else { + assert.equal(keepaliveStatusFor(f.input.sessionId), undefined); + assert.equal(f.timers.size, 0); + } + }); + } + + test(`an unmodified snapshot keeps its live ${phase} request and records the hit`, async t => { + const f = await setup(t, phase); + noteSuccessfulForward({ ...f.input, wireBody: NO_BREAKPOINT }); + assert.equal(f.getSignal()?.aborted, false); + assert.ok(keepaliveStatusFor(f.input.sessionId)); + f.finishOld(); + await f.normalRequest(); + assert.equal(keepaliveStatusFor(f.input.sessionId)?.pingCount, 1); + assert.equal(keepaliveStatusFor(f.input.sessionId)?.failStreak, 0); + assert.equal(f.timers.size, 1); + }); +} + +test("keepalive cancellation works without AbortSignal.any on early Node 20", async t => { + const original = Object.getOwnPropertyDescriptor(AbortSignal, "any"); + Object.defineProperty(AbortSignal, "any", { value: undefined, configurable: true, writable: true }); + t.after(() => { + if (original) Object.defineProperty(AbortSignal, "any", original); + else Reflect.deleteProperty(AbortSignal, "any"); + }); + const f = await setup(t, "body"); + process.env.BILI_CACHE_KEEPALIVE = "0"; + noteSuccessfulForward(f.input); + await f.normalRequest(); + await within(f.oldClosed.promise, "invalidated ping close"); + assert.equal(f.getSignal()?.aborted, true); + assert.equal(keepaliveStatusFor(f.input.sessionId), undefined); +});