diff --git a/src/inline-engine.ts b/src/inline-engine.ts index c156b0fa36..be5ad5b52c 100644 --- a/src/inline-engine.ts +++ b/src/inline-engine.ts @@ -155,6 +155,9 @@ class InlineRes extends EventEmitter { super(); this.bodyStream = new ReadableStream({ start: (c) => { this.controller = c; }, + // Cancelling a response body is a client disconnect even when + // the caller did not supply or abort an AbortSignal. + cancel: () => { this.destroy(); }, }); } 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-body-cancel.test.ts b/tests/inline-body-cancel.test.ts new file mode 100644 index 0000000000..7f333b3533 --- /dev/null +++ b/tests/inline-body-cancel.test.ts @@ -0,0 +1,115 @@ +import { after, test } from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import type { AddressInfo } from "node:net"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; + +const root = fs.mkdtempSync(path.join(os.tmpdir(), "bili-inline-body-cancel-")); +const previous = new Map(); +for (const name of ["XDG_DATA_HOME", "XDG_STATE_HOME", "XDG_CONFIG_HOME"]) { + previous.set(name, process.env[name]); + process.env[name] = path.join(root, name); +} +const { getInlineEngine, INLINE_ORIGIN, _resetInlineEngineForTest } = await import("../src/inline-engine.ts"); +const engine = await getInlineEngine(); +const streams: http.ServerResponse[] = []; +const upstream = http.createServer((req, res) => { + req.resume(); + req.on("end", () => { + streams.push(res); + if (req.url === "/complete") { + res.writeHead(200, { "content-type": "text/plain" }); + res.end("complete response"); + } else if (req.url === "/v1/messages") { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.write('event: message_start\ndata: {"type":"message_start","message":{"id":"msg_1","type":"message","role":"assistant","model":"test-model","content":[],"usage":{"input_tokens":10,"output_tokens":1}}}\n\n'); + res.write('event: content_block_delta\ndata: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"first token"}}\n\n'); + } else { + res.writeHead(200, { "content-type": "application/octet-stream" }); + res.write("first chunk\n"); + } + // All non-complete responses stay open and idle. Only cancellation + // should end them; another upstream chunk cannot mask a missing hook. + }); +}); +await new Promise(resolve => upstream.listen(0, "127.0.0.1", resolve)); +const target = `${INLINE_ORIGIN}/bili/http://127.0.0.1:${(upstream.address() as AddressInfo).port}`; + +after(async () => { + upstream.closeAllConnections(); + await new Promise(resolve => upstream.close(() => resolve())); + _resetInlineEngineForTest(); + for (const [name, value] of previous) { + if (value === undefined) delete process.env[name]; + else process.env[name] = value; + } + fs.rmSync(root, { recursive: true, force: true }); +}); + +async function closesPromptly(res: http.ServerResponse): Promise { + if (res.destroyed) return; + let timer: ReturnType | undefined; + try { + await Promise.race([ + new Promise(resolve => res.once("close", resolve)), + new Promise((_, reject) => { timer = setTimeout(() => reject(new Error("upstream remained open after response body cancellation")), 1000); }), + ]); + } finally { + clearTimeout(timer); + } +} + +for (const mode of ["unread body", "active reader", "reader with caller signal"]) { + test(`cancelling the ${mode} closes an idle raw upstream`, { timeout: 10_000 }, async () => { + const controller = new AbortController(); + const response = await engine.dispatch(`${target}/raw`, mode === "reader with caller signal" ? { signal: controller.signal } : undefined, globalThis.fetch); + assert.equal(response.status, 200); + const upstreamResponse = streams.at(-1)!; + try { + if (mode === "unread body") { + await response.body!.cancel("caller no longer needs the response"); + } else { + const reader = response.body!.getReader(); + assert.equal((await reader.read()).done, false); + await reader.cancel(new Error("caller stopped reading")); + assert.equal((await reader.read()).done, true); + reader.releaseLock(); + } + await closesPromptly(upstreamResponse); + assert.equal(controller.signal.aborted, false, "stream cancellation does not abort the caller-owned controller"); + } finally { + upstreamResponse.destroy(); + } + }); +} + +test("cancelling the model response reader stops an idle SSE generation", { timeout: 10_000 }, async () => { + const response = await engine.dispatch(`${target}/v1/messages`, { + method: "POST", + headers: { "content-type": "application/json", "x-bili-plugin": "pi", "x-bili-plugin-conversation": "inline-body-cancel-model" }, + body: JSON.stringify({ model: "test-model", max_tokens: 1024, stream: true, messages: [{ role: "user", content: "hi" }] }), + }, globalThis.fetch); + assert.equal(response.status, 200); + const upstreamResponse = streams.at(-1)!; + const reader = response.body!.getReader(); + try { + let text = ""; + while (!text.includes("first token")) { + const chunk = await reader.read(); + assert.equal(chunk.done, false); + text += Buffer.from(chunk.value!).toString(); + } + await reader.cancel(); + await closesPromptly(upstreamResponse); + } finally { + reader.releaseLock(); + upstreamResponse.destroy(); + } +}); + +test("normal response completion remains readable", async () => { + const response = await engine.dispatch(`${target}/complete`, undefined, globalThis.fetch); + assert.equal(await response.text(), "complete response"); +});