diff --git a/src/agent/pi-inline.ts b/src/agent/pi-inline.ts index 238d3c73d1..9e23fbeef0 100644 --- a/src/agent/pi-inline.ts +++ b/src/agent/pi-inline.ts @@ -42,7 +42,12 @@ export function installInlineTransport(): boolean { } catch (err) { // Caller-side cancel is not an engine failure — propagate it: // degrading would consume the one-shot warning and re-send a - // request the caller already abandoned. + // request the caller already abandoned. The engine rejects with + // the signal's REASON, which for a custom abort + // (controller.abort(new Error(...))) is not an AbortError — so + // classify by the signal's state, not the reason's name. + const cancelSignal = init?.signal !== undefined ? init.signal : input instanceof Request ? input.signal : undefined; + if (cancelSignal?.aborted) throw err; if (err instanceof Error && err.name === "AbortError") throw err; const msg = err instanceof Error ? err.message : String(err); if (url.startsWith(MODEL_PREFIX)) { diff --git a/src/inline-engine.ts b/src/inline-engine.ts index c156b0fa36..904e1b76e0 100644 --- a/src/inline-engine.ts +++ b/src/inline-engine.ts @@ -269,7 +269,10 @@ function responseFrom(res: InlineRes): Response { function makeDispatcher(ctx: EngineContext): InlineEngine["dispatch"] { return async (input, init, rawFetch) => { + const signal = init?.signal !== undefined ? init.signal : input instanceof Request ? input.signal : undefined; + signal?.throwIfAborted(); const req = await toInlineReq(input, init); + signal?.throwIfAborted(); const res = new InlineRes(); // Spawn-mode cancel parity (#3278 review): honor the caller's // AbortSignal. Destroying res drives the pipeline's own close plumbing @@ -277,7 +280,6 @@ function makeDispatcher(ctx: EngineContext): InlineEngine["dispatch"] { // socket close does over HTTP, stopping the upstream round trip; the // raced promise makes the caller reject with the abort reason like // undici would instead of running to completion and burning tokens. - const signal = init?.signal; let onAbort: (() => void) | undefined; let abortPromise: Promise | undefined; if (signal != null) { diff --git a/tests/e2e/e2e-update-state.test.ts b/tests/e2e/e2e-update-state.test.ts index 03f2b1db22..e31baf7659 100644 --- a/tests/e2e/e2e-update-state.test.ts +++ b/tests/e2e/e2e-update-state.test.ts @@ -299,6 +299,11 @@ test("self-update keeps live compression state: folds survive the OLD→NEW hop envBase.BILI_UPDATE_POLICY_URL = "http://127.0.0.1:1/no-policy.json"; envBase.BILI_PERSIST_DEBOUNCE_MS = "200"; envBase.BILI_SESSIONS_DIR = sessionsDir; + // Pure-sqlite default (5a80e2b8e, #6480) turns the JSON envelope + // writer OFF, but this lane asserts on the on-disk .json session file + // (state survival across the update). Pin the writer ON so the + // envelope this test was written for is still produced. + envBase.BILI_STORAGE_JSON_WRITER = "on"; const port = await freePort(); proxy = spawnProxy(installDir, port, envBase); diff --git a/tests/inline-request-signal.test.ts b/tests/inline-request-signal.test.ts new file mode 100644 index 0000000000..f98086b2af --- /dev/null +++ b/tests/inline-request-signal.test.ts @@ -0,0 +1,130 @@ +import { after, test } from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import type { AddressInfo } from "node:net"; + +function deferred() { + let resolve!: () => void; + const promise = new Promise((done) => { resolve = done; }); + return { promise, resolve }; +} + +interface Fixture { + started: ReturnType; + closed: ReturnType; + response?: http.ServerResponse; + stream?: boolean; +} +const pending = new Map(); +let upstreamHits = 0; +const upstream = http.createServer((req, res) => { + req.resume(); + req.on("end", () => { + upstreamHits++; + const fixture = pending.get(req.url ?? ""); + if (!fixture) { res.end("complete"); return; } + fixture.response = res; + res.once("close", fixture.closed.resolve); + if (fixture.stream) { + res.writeHead(200, { "content-type": "application/octet-stream" }); + res.write("first"); + } + fixture.started.resolve(); + }); +}); +upstream.listen(0, "127.0.0.1"); +await once(upstream, "listening"); +const origin = `http://127.0.0.1:${(upstream.address() as AddressInfo).port}`; +const ie = await import("../src/inline-engine.ts"); + +after(async () => { + upstream.closeAllConnections(); + await new Promise((resolve) => upstream.close(() => resolve())); + const { flushAllSessions } = await import("../src/session.ts"); + await flushAllSessions(); + ie._resetInlineEngineForTest(); +}); + +async function bounded(promise: Promise): Promise { + let timer: NodeJS.Timeout | undefined; + try { + return await Promise.race([ + promise, + new Promise((_, reject) => { + timer = setTimeout(() => reject(new Error("inline cancellation did not settle")), 3000); + }), + ]); + } finally { clearTimeout(timer); } +} + +for (const explicitUndefined of [false, true]) { + test(`Request.signal cancels before headers (${explicitUndefined ? "undefined signal override" : "omitted init"})`, { timeout: 30_000 }, async () => { + const engine = await ie.getInlineEngine(); + const endpoint = `/pending-${explicitUndefined}`; + const fixture: Fixture = { started: deferred(), closed: deferred() }; + pending.set(endpoint, fixture); + const controller = new AbortController(); + const reason = new Error("request caller cancelled"); + const request = new Request(`${ie.INLINE_ORIGIN}/bili/${origin}${endpoint}`, { signal: controller.signal }); + const call = engine.dispatch(request, explicitUndefined ? { signal: undefined } : undefined, globalThis.fetch); + try { + await bounded(fixture.started.promise); + controller.abort(reason); + await assert.rejects(bounded(call), (error: unknown) => error === reason); + await bounded(fixture.closed.promise); + } finally { + fixture.response?.end("released"); + await call.then((response) => response.text(), () => {}).catch(() => {}); + pending.delete(endpoint); + } + }); +} + +test("Request.signal aborts a committed body and closes the live upstream", { timeout: 30_000 }, async () => { + const engine = await ie.getInlineEngine(); + const endpoint = "/stream"; + const fixture: Fixture = { started: deferred(), closed: deferred(), stream: true }; + pending.set(endpoint, fixture); + const controller = new AbortController(); + const request = new Request(`${ie.INLINE_ORIGIN}/bili/${origin}${endpoint}`, { signal: controller.signal }); + const response = await bounded(engine.dispatch(request, {}, globalThis.fetch)); + const reader = response.body!.getReader(); + let read: ReturnType | undefined; + try { + assert.equal(new TextDecoder().decode((await bounded(reader.read())).value), "first"); + controller.abort(); + read = reader.read(); + await assert.rejects(bounded(read), /abort/i); + await bounded(fixture.closed.promise); + } finally { + fixture.response?.end(); + await read?.catch(() => {}); + reader.releaseLock(); + pending.delete(endpoint); + } +}); + +test("a pre-aborted Request never reaches the upstream", { timeout: 30_000 }, async () => { + const engine = await ie.getInlineEngine(); + const controller = new AbortController(); + const reason = new Error("already cancelled"); + controller.abort(reason); + const request = new Request(`${ie.INLINE_ORIGIN}/bili/${origin}/ok`, { signal: controller.signal }); + const before = upstreamHits; + await assert.rejects(engine.dispatch(request, undefined, globalThis.fetch), (error: unknown) => error === reason); + assert.equal(upstreamHits, before); +}); + +for (const override of ["signal", "null"] as const) { + test(`init.signal=${override} overrides an aborted Request signal`, { timeout: 30_000 }, async () => { + const engine = await ie.getInlineEngine(); + const controller = new AbortController(); + controller.abort(); + const request = new Request(`${ie.INLINE_ORIGIN}/bili/${origin}/ok`, { signal: controller.signal }); + const init = { signal: override === "null" ? null : new AbortController().signal }; + const response = await engine.dispatch(request, init, globalThis.fetch); + assert.equal(response.status, 200); + assert.equal(await response.text(), "complete"); + }); +} diff --git a/tests/pi-inline.test.ts b/tests/pi-inline.test.ts index 00b175a4ff..3421c69495 100644 --- a/tests/pi-inline.test.ts +++ b/tests/pi-inline.test.ts @@ -372,3 +372,56 @@ test("#3278 engine: caller abort cancels the in-process round trip (spawn-mode p await new Promise((r) => setTimeout(r, 300)); assert.equal(slowCompleted, false, "upstream round trip must not complete after the caller aborted"); }); + +test("#6504 transport: a custom-reason Request.signal cancel propagates, never degrades to a direct resend", async () => { + // The engine rejects with the signal's REASON. For a custom abort + // (controller.abort(new Error(...))) that reason is NOT an AbortError, so + // the transport must classify the cancel by the signal's state — not by + // the reason's name — or it would degrade: consume the one-shot warning + // and re-send an already-abandoned request direct, uncompressed. + const ie = await import("../src/inline-engine.ts"); + ie._resetInlineEngineForTest(); + const reason = new Error("caller cancelled"); + ie._setEngineFactoryForTest(async () => ({ + origin: ie.INLINE_ORIGIN, + dispatch: async (input: string | URL | Request, init?: RequestInit): Promise => { + // Mirror the engine's signal resolution + pre-header abort reject. + const signal = init?.signal !== undefined ? init.signal : input instanceof Request ? input.signal : undefined; + await new Promise((_, reject) => { + if (signal?.aborted) reject(signal.reason); + else signal?.addEventListener("abort", () => reject(signal.reason), { once: true }); + }); + return new Response("unreachable"); + }, + dispatchHttp: async () => {}, + wsOrigin: Promise.resolve(undefined), + webOrigin: Promise.resolve(undefined), + })); + try { + await freshChain(); + const ac = new AbortController(); + const req = new Request(`${ie.INLINE_ORIGIN}/bili/${upstreamOrigin}/v1/messages`, { + method: "POST", + body: "{}", + signal: ac.signal, + }); + const before = hits.length; + const settled = fetch(req).then( + (r) => { throw new Error(`expected cancellation, got HTTP ${r.status}`); }, + (e: unknown) => e, + ); + setTimeout(() => ac.abort(reason), 50); + const err = await withTimeout(settled, 10_000, "cancel propagation") as Error; + // The caller's custom reason must propagate verbatim. + assert.equal(err, reason, "the caller's abort reason must propagate verbatim"); + // No direct resend: the real upstream must stay untouched. + assert.equal(hits.length, before, "a cancelled request must not be re-sent direct"); + } finally { + const piInline = await import("../src/agent/pi-inline.ts"); + const fc = await import("../src/agent/fetch-chain/index.ts"); + piInline._resetInlineTransportForTest(); + fc.resetFetchChainForTest(); + ie._setEngineFactoryForTest(undefined); + ie._resetInlineEngineForTest(); + } +});