From 544070b1a45eef02155227c1c6ba8bcd03d23415 Mon Sep 17 00:00:00 2001 From: Tyler Skelton Date: Wed, 7 Oct 2026 15:13:50 -0700 Subject: [PATCH] Keep the loop alive in the local-ask timeout test The mock stream in 'ends at the timeout while the stream is still open' holds no handle, and the ask's deadline is AbortSignal.timeout, whose timer is unref'd. Under Node 22 (CI) the loop drained before the deadline fired, cancelling that test and every one after it in the file. Node 26's test runner keeps the loop alive while a test is pending, which hid it locally. The mock now holds a timer for as long as the request is open, the way a real socket would. --- test/local-ask.test.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/test/local-ask.test.ts b/test/local-ask.test.ts index 84b1529..75e3ace 100644 --- a/test/local-ask.test.ts +++ b/test/local-ask.test.ts @@ -78,17 +78,31 @@ describe("an ask to a self-hosted server", () => { it("ends at the timeout while the stream is still open, closing the request", async () => { let aborted = false; + let socket: ReturnType | undefined; const fetchFn = (async (_u: string, init: { signal?: AbortSignal }) => { init.signal?.addEventListener("abort", () => (aborted = true)); + // An open connection is a socket, and a socket keeps the event loop + // alive. This in-memory stream is not one, and the ask's deadline is + // AbortSignal.timeout, whose timer is unref'd: with nothing else + // holding the loop it drains before the deadline fires. Node 26's test + // runner happens to hold the loop open while a test is pending; Node + // 22's (CI) does not, so this test and every one after it in the file + // was cancelled with "Promise resolution is still pending but the + // event loop has already resolved". Stand in for the socket until the + // request is closed. + socket = setInterval(() => {}, 1_000); const body = new ReadableStream({ start(c) { c.enqueue(new TextEncoder().encode(`data: ${JSON.stringify(delta({ reasoning_content: "x" }))}\n\n`)); - init.signal?.addEventListener("abort", () => c.error(init.signal!.reason)); + init.signal?.addEventListener("abort", () => { + clearInterval(socket); + c.error(init.signal!.reason); + }); }, }); return new Response(body, { headers: { "content-type": "text/event-stream" } }); }) as unknown as typeof fetch; - const r = await askModel({ baseUrl: LOCAL, model: "m", system: "s", prompt: "p", fetchFn, timeoutMs: 30 }); + const r = await askModel({ baseUrl: LOCAL, model: "m", system: "s", prompt: "p", fetchFn, timeoutMs: 30 }).finally(() => clearInterval(socket)); assert.equal(aborted, true); assert.match(!r.ok ? r.error : "", /did not answer within/); });