From 76189b11ef297ab1244c268f20813ebb3d839fb7 Mon Sep 17 00:00:00 2001 From: Afloat Date: Mon, 9 Nov 2026 21:48:37 +0800 Subject: [PATCH] fix: preserve busy liveness when health response bodies time out --- src/liveness.ts | 3 +- tests/health-body-timeout.test.ts | 46 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 tests/health-body-timeout.test.ts diff --git a/src/liveness.ts b/src/liveness.ts index 67daf110c8..eeed7c39b0 100644 --- a/src/liveness.ts +++ b/src/liveness.ts @@ -112,7 +112,8 @@ export async function probeLiveness(origin: string, budgetMs: number): Promise Promise): Promise { + const server = http.createServer(handler); + await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); + const address = server.address(); + assert.ok(address && typeof address !== "string"); + try { + await run(`http://127.0.0.1:${address.port}`); + } finally { + server.closeAllConnections(); + await new Promise((resolve) => server.close(() => resolve())); + } +} + +test("a health body stalled after successful headers is busy", { timeout: 5_000 }, async () => { + let requested = false; + await withServer((_req, res) => { + requested = true; + res.writeHead(200, { "content-type": "application/json" }); + res.flushHeaders(); + res.write('{"ok":'); + }, async (origin) => { + const verdict = await probeLiveness(origin, 500); + assert.equal(requested, true); + assert.equal(verdict.state, "busy"); + assert.match(verdict.detail!, /after TCP connect/); + }); +}); + +test("a complete healthy payload remains alive", async () => { + await withServer((_req, res) => res.end('{"ok":true}'), async (origin) => { + assert.equal((await probeLiveness(origin, 1_000)).state, "alive"); + }); +}); + +test("a complete malformed payload remains dead", async () => { + await withServer((_req, res) => res.end("not JSON"), async (origin) => { + const verdict = await probeLiveness(origin, 1_000); + assert.equal(verdict.state, "dead"); + assert.match(verdict.detail!, /non-JSON/); + }); +});