diff --git a/tests/system/ci-runner/provider-process-group.test.ts b/tests/system/ci-runner/provider-process-group.test.ts index f1bc7304..90f33072 100644 --- a/tests/system/ci-runner/provider-process-group.test.ts +++ b/tests/system/ci-runner/provider-process-group.test.ts @@ -1,4 +1,5 @@ -import { mkdtemp, readFile, rm } from "node:fs/promises"; +import { readFileSync } from "node:fs"; +import { mkdtemp, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; @@ -21,22 +22,79 @@ describe("CI-runner provider process-group lifecycle", { timeout: childProcessTi "setInterval(() => {}, 1000);", ].join("\n"); - const running = runProviderProcess({ - executable: process.execPath, - arguments: ["-e", source], - environment: { - PATH: process.env.PATH, - DESCENDANT_PID_PATH: descendantPidPath, + // 2026-11-13. A real 250 ms timer started the kill before a loaded runner + // had started the descendant, so its pid file did not exist (ENOENT, + // runner run 309). The subject is the group kill, not how long a + // provider may run: the runner's timer fires once the descendant exists, + // or after the watchdog, and then the missing pid fails the test. + const startedAt = Date.now(); + const running = runProviderProcess( + { + executable: process.execPath, + arguments: ["-e", source], + environment: { + PATH: process.env.PATH, + DESCENDANT_PID_PATH: descendantPidPath, + }, + timeoutMs: 250, }, - timeoutMs: 250, - }); + { + setTimer: (fire) => { + const poll = setInterval(() => { + const waiting = readPid(descendantPidPath) === undefined; + if (waiting && Date.now() - startedAt <= PROCESS_WATCHDOG_MS) return; + clearInterval(poll); + fire(); + }, POLL_MS); + return poll; + }, + clearTimer: (timer) => clearInterval(timer), + }, + ); await expect(running).rejects.toThrow(/timed out.*process close/u); - const descendantPid = Number(await readFile(descendantPidPath, "utf8")); - expect(Number.isSafeInteger(descendantPid) && descendantPid > 0).toBe(true); - expect(() => process.kill(descendantPid, 0)).toThrow(/ESRCH|no such process/u); + const descendantPid = readPid(descendantPidPath); + expect(descendantPid).toBeDefined(); + await expect(waitForProcessGone(descendantPid!)).resolves.toBe(true); } finally { await rm(root, { recursive: true, force: true }); } }); }); + +const POLL_MS = 10; +/** + * Test watchdog only, not a provider deadline: how long the test waits for the + * descendant to write its pid, and for it to be gone after the group kill. + * SIGKILL and the reaper (the container's init, once the provider is gone) + * finish after the provider's close event, so the descendant is polled until + * it is gone instead of checked once. + */ +const PROCESS_WATCHDOG_MS = 5_000; + +/** The pid the provider wrote, or undefined while the file is missing or partly written. */ +function readPid(file: string): number | undefined { + let text: string; + try { + text = readFileSync(file, "utf8"); + } catch (error) { + if (error && typeof error === "object" && "code" in error && error.code === "ENOENT") return undefined; + throw error; + } + const pid = Number(text); + return /^[1-9][0-9]*$/u.test(text) && Number.isSafeInteger(pid) ? pid : undefined; +} + +async function waitForProcessGone(pid: number): Promise { + const deadline = Date.now() + PROCESS_WATCHDOG_MS; + while (Date.now() <= deadline) { + try { + process.kill(pid, 0); + } catch (error) { + if (error && typeof error === "object" && "code" in error && error.code === "ESRCH") return true; + throw error; + } + await new Promise((resolve) => setTimeout(resolve, POLL_MS)); + } + return false; +}