Skip to content

Commit ebee101

Browse files
committed
fix(qa): cancel Docker health probe bodies
1 parent 5697ab8 commit ebee101

2 files changed

Lines changed: 81 additions & 4 deletions

File tree

‎src/plugin-sdk/qa-runtime.test.ts‎

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { createServer } from "node:net";
12
/**
23
* Tests QA runtime command loading and private CLI gating.
34
*/
45
import { Command } from "commander";
5-
import { createServer } from "node:net";
66
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
77
import {
88
cleanupTempDirs,
@@ -60,6 +60,21 @@ describe("plugin-sdk qa-runtime", () => {
6060
};
6161
}
6262

63+
function cancelTrackedFetchResponse(ok = true) {
64+
let canceled = false;
65+
return {
66+
response: {
67+
ok,
68+
body: {
69+
cancel: vi.fn(async () => {
70+
canceled = true;
71+
}),
72+
},
73+
},
74+
wasCanceled: () => canceled,
75+
};
76+
}
77+
6378
it("stays cold until the runtime seam is used", async () => {
6479
const module = await import("./qa-runtime.js");
6580

@@ -341,6 +356,52 @@ describe("plugin-sdk qa-runtime", () => {
341356
expect(fetchImpl).toHaveBeenCalledWith("http://172.18.0.4:18789/healthz");
342357
});
343358

359+
it("cancels compose service health probe response bodies", async () => {
360+
const module = await import("./qa-runtime.js");
361+
const runtime = module.createQaDockerRuntime({ auditContext: "qa-test" });
362+
const runCommand = vi.fn(async (_command: string, args: string[]) => {
363+
if (args.includes("ps")) {
364+
return { stdout: "qa-gateway-one\n", stderr: "" };
365+
}
366+
return { stdout: "172.18.0.4\n", stderr: "" };
367+
});
368+
const probe = cancelTrackedFetchResponse(true);
369+
const fetchImpl = vi.fn(async () => probe.response);
370+
371+
await expect(
372+
runtime.resolveComposeServiceUrl(
373+
"gateway",
374+
18789,
375+
"/tmp/docker-compose.yml",
376+
"/repo",
377+
runCommand,
378+
fetchImpl,
379+
),
380+
).resolves.toBe("http://172.18.0.4:18789/");
381+
expect(probe.wasCanceled()).toBe(true);
382+
});
383+
384+
it("cancels waitForHealth response bodies after each probe", async () => {
385+
const module = await import("./qa-runtime.js");
386+
const runtime = module.createQaDockerRuntime({ auditContext: "qa-test" });
387+
const first = cancelTrackedFetchResponse(false);
388+
const second = cancelTrackedFetchResponse(true);
389+
const responses = [first.response, second.response];
390+
const fetchImpl = vi.fn(async () => responses.shift() ?? second.response);
391+
const sleepImpl = vi.fn(async () => {});
392+
393+
await runtime.waitForHealth("http://127.0.0.1:18789/healthz", {
394+
fetchImpl,
395+
sleepImpl,
396+
timeoutMs: 1000,
397+
pollMs: 1,
398+
});
399+
400+
expect(fetchImpl).toHaveBeenCalledTimes(2);
401+
expect(first.wasCanceled()).toBe(true);
402+
expect(second.wasCanceled()).toBe(true);
403+
});
404+
344405
it("resolves an unpinned QA Docker host port away from an occupied loopback default", async () => {
345406
const module = await import("./qa-runtime.js");
346407
const reservation = await occupyLoopbackPort();

‎src/plugin-sdk/qa-runtime.ts‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,11 @@ export type QaDockerRunCommand = (
249249
) => Promise<{ stdout: string; stderr: string }>;
250250

251251
/** Minimal fetch-like health probe used by QA Docker runtime helpers. */
252-
export type QaDockerFetchLike = (input: string) => Promise<{ ok: boolean }>;
252+
export type QaDockerFetchResponse = {
253+
ok: boolean;
254+
body?: { cancel?: () => unknown } | null;
255+
};
256+
export type QaDockerFetchLike = (input: string) => Promise<QaDockerFetchResponse>;
253257

254258
const DEFAULT_QA_DOCKER_COMMAND_TIMEOUT_MS = 120_000;
255259

@@ -484,14 +488,23 @@ function parseDockerComposePsRows(stdout: string) {
484488
}
485489

486490
async function isQaDockerHealthy(url: string, fetchImpl: QaDockerFetchLike) {
491+
let response: QaDockerFetchResponse | undefined;
487492
try {
488-
const response = await fetchImpl(url);
493+
response = await fetchImpl(url);
489494
return response.ok;
490495
} catch {
491496
return false;
497+
} finally {
498+
await releaseQaDockerFetchResponse(response);
492499
}
493500
}
494501

502+
async function releaseQaDockerFetchResponse(response: QaDockerFetchResponse | undefined) {
503+
try {
504+
await response?.body?.cancel?.();
505+
} catch {}
506+
}
507+
495508
/** Create Docker command, health-check, and compose helpers for QA harnesses. */
496509
export function createQaDockerRuntime(params: {
497510
auditContext: string;
@@ -548,14 +561,17 @@ export function createQaDockerRuntime(params: {
548561
let lastError: unknown = null;
549562

550563
while (Date.now() < deadline) {
564+
let response: QaDockerFetchResponse | undefined;
551565
try {
552-
const response = await deps.fetchImpl(url);
566+
response = await deps.fetchImpl(url);
553567
if (response.ok) {
554568
return;
555569
}
556570
lastError = new Error(`Health check returned non-OK for ${url}`);
557571
} catch (error) {
558572
lastError = error;
573+
} finally {
574+
await releaseQaDockerFetchResponse(response);
559575
}
560576
await deps.sleepImpl(pollMs);
561577
}

0 commit comments

Comments
 (0)