Skip to content

Commit 0abc513

Browse files
authored
fix(tests): deflake qa-lab process-group pid-file waits (#126351)
The model catalog abort test waited for pid-file existence, but the catalog child's writeFileSync exposes an open-truncate window: the test could read the file empty and fail on a NaN pid assertion (CI run 32231699569, checks-node-changed-extensions-config-37). Add a qa-lab-local process-wait test helper that waits for the asserted state (a parseable pid) with generous 10s deadlines and self-diagnosing timeout errors, and consolidate the two hand-rolled helper copies in model-catalog.runtime.test.ts and test-file-scenario-command-lifecycle.test.ts onto it. Also retire the stale 1.7s abort-settle ceiling (calibrated against a removed 1.5s in-plugin kill grace) in favor of a 5s bound with the contract documented.
1 parent 9d4ba33 commit 0abc513

3 files changed

Lines changed: 95 additions & 87 deletions

File tree

‎extensions/qa-lab/src/model-catalog.runtime.test.ts‎

Lines changed: 16 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,18 @@ import fs from "node:fs/promises";
33
import path from "node:path";
44
import { afterEach, describe, expect, it } from "vitest";
55
import { loadQaRunnerModelOptions } from "./model-catalog.runtime.js";
6+
import {
7+
isProcessAlive,
8+
waitForDead,
9+
waitForFile,
10+
waitForPidFile,
11+
} from "./process-wait.test-helper.js";
612
import { createTempDirHarness } from "./temp-dir.test-helper.js";
713

814
const { cleanup, makeTempDir } = createTempDirHarness();
915

1016
afterEach(cleanup);
1117

12-
async function waitForFile(filePath: string, timeoutMs: number): Promise<void> {
13-
const deadlineAt = Date.now() + timeoutMs;
14-
while (Date.now() < deadlineAt) {
15-
try {
16-
await fs.access(filePath);
17-
return;
18-
} catch {
19-
await new Promise((resolvePoll) => {
20-
setTimeout(resolvePoll, 5);
21-
});
22-
}
23-
}
24-
throw new Error(`timed out waiting for ${filePath}`);
25-
}
26-
27-
function isProcessAlive(pid: number): boolean {
28-
try {
29-
process.kill(pid, 0);
30-
return true;
31-
} catch {
32-
return false;
33-
}
34-
}
35-
36-
async function waitForDead(pid: number, timeoutMs: number): Promise<void> {
37-
const deadlineAt = Date.now() + timeoutMs;
38-
while (Date.now() < deadlineAt) {
39-
if (!isProcessAlive(pid)) {
40-
return;
41-
}
42-
await new Promise((resolvePoll) => {
43-
setTimeout(resolvePoll, 5);
44-
});
45-
}
46-
throw new Error(`timed out waiting for pid ${pid} to exit`);
47-
}
48-
4918
describe("qa runner model catalog", () => {
5019
it("filters catalog output and prefers gpt-5.6-luna first", async () => {
5120
const repoRoot = await makeTempDir("openclaw-qa-model-catalog-output-");
@@ -128,14 +97,12 @@ describe("qa runner model catalog", () => {
12897
signal: controller.signal,
12998
});
13099

131-
await waitForFile(pidPath, 2_000);
132-
descendantPid = Number.parseInt(await fs.readFile(pidPath, "utf8"), 10);
133-
expect(Number.isInteger(descendantPid)).toBe(true);
100+
descendantPid = await waitForPidFile(pidPath);
134101
expect(isProcessAlive(descendantPid)).toBe(true);
135102
controller.abort();
136103

137104
await expect(runPromise).rejects.toThrow("qa model catalog aborted");
138-
await waitForDead(descendantPid, 2_000);
105+
await waitForDead(descendantPid);
139106
} finally {
140107
if (descendantPid !== undefined && isProcessAlive(descendantPid)) {
141108
process.kill(descendantPid, "SIGKILL");
@@ -181,16 +148,19 @@ describe("qa runner model catalog", () => {
181148
signal: controller.signal,
182149
});
183150

184-
await waitForFile(readyPath, 2_000);
185-
descendantPid = Number.parseInt(await fs.readFile(pidPath, "utf8"), 10);
186-
expect(Number.isInteger(descendantPid)).toBe(true);
151+
// The ready marker lands after the SIGTERM handler is installed, and the
152+
// pid file is fully written before it, so this read is parse-safe.
153+
await waitForFile(readyPath);
154+
descendantPid = await waitForPidFile(pidPath);
187155
const abortStartedAt = Date.now();
188156
controller.abort();
189157

190158
await expect(runPromise).rejects.toThrow("qa model catalog aborted");
191159
expect(await fs.readFile(cleanupPath, "utf8")).toBe("clean");
192-
expect(Date.now() - abortStartedAt).toBeLessThan(1_700);
193-
await waitForDead(descendantPid, 2_000);
160+
// Abort must settle with the exiting descendants (grace window is 300ms),
161+
// never a long fixed kill ceiling; generous bound for loaded runners.
162+
expect(Date.now() - abortStartedAt).toBeLessThan(5_000);
163+
await waitForDead(descendantPid);
194164
} finally {
195165
if (descendantPid !== undefined && isProcessAlive(descendantPid)) {
196166
process.kill(descendantPid, "SIGKILL");
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Qa Lab plugin module implements process wait helper behavior.
2+
import fs from "node:fs/promises";
3+
import { setTimeout as sleep } from "node:timers/promises";
4+
5+
const POLL_INTERVAL_MS = 10;
6+
// Generous ceiling for loaded CI runners: callers synchronize on the asserted
7+
// state, so a large bound only delays failure reporting, never success.
8+
const DEFAULT_WAIT_TIMEOUT_MS = 10_000;
9+
10+
export function isProcessAlive(pid: number): boolean {
11+
try {
12+
process.kill(pid, 0);
13+
return true;
14+
} catch {
15+
return false;
16+
}
17+
}
18+
19+
export async function waitForFile(
20+
filePath: string,
21+
timeoutMs = DEFAULT_WAIT_TIMEOUT_MS,
22+
): Promise<void> {
23+
const deadlineAt = Date.now() + timeoutMs;
24+
while (Date.now() < deadlineAt) {
25+
try {
26+
await fs.access(filePath);
27+
return;
28+
} catch {
29+
await sleep(POLL_INTERVAL_MS);
30+
}
31+
}
32+
throw new Error(`timed out after ${timeoutMs}ms waiting for ${filePath}`);
33+
}
34+
35+
// writeFileSync exposes an open-truncate window to observers, so wait for a
36+
// parseable pid, never bare existence; an existence wait reads "" into NaN.
37+
export async function waitForPidFile(
38+
filePath: string,
39+
timeoutMs = DEFAULT_WAIT_TIMEOUT_MS,
40+
): Promise<number> {
41+
const deadlineAt = Date.now() + timeoutMs;
42+
let lastContent: string | undefined;
43+
while (Date.now() < deadlineAt) {
44+
lastContent = await fs.readFile(filePath, "utf8").catch(() => undefined);
45+
if (lastContent !== undefined) {
46+
const pid = Number.parseInt(lastContent, 10);
47+
if (Number.isInteger(pid) && pid > 0) {
48+
return pid;
49+
}
50+
}
51+
await sleep(POLL_INTERVAL_MS);
52+
}
53+
const lastState =
54+
lastContent === undefined
55+
? "file missing"
56+
: `unparsable content ${JSON.stringify(lastContent)}`;
57+
throw new Error(`timed out after ${timeoutMs}ms waiting for pid file ${filePath} (${lastState})`);
58+
}
59+
60+
export async function waitForDead(pid: number, timeoutMs = DEFAULT_WAIT_TIMEOUT_MS): Promise<void> {
61+
const deadlineAt = Date.now() + timeoutMs;
62+
while (Date.now() < deadlineAt) {
63+
if (!isProcessAlive(pid)) {
64+
return;
65+
}
66+
await sleep(POLL_INTERVAL_MS);
67+
}
68+
throw new Error(`timed out after ${timeoutMs}ms waiting for pid ${pid} to exit`);
69+
}

‎extensions/qa-lab/src/test-file-scenario-command-lifecycle.test.ts‎

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { ChildProcess } from "node:child_process";
22
import { EventEmitter } from "node:events";
3-
import { mkdtemp, readFile, rm } from "node:fs/promises";
3+
import { mkdtemp, rm } from "node:fs/promises";
44
import os from "node:os";
55
import path from "node:path";
66
import { setTimeout as sleep } from "node:timers/promises";
@@ -29,6 +29,7 @@ vi.mock("node:child_process", async (importOriginal) => {
2929
};
3030
});
3131

32+
import { isProcessAlive, waitForDead, waitForPidFile } from "./process-wait.test-helper.js";
3233
import {
3334
resetQaScenarioCommandCleanupTimings,
3435
runQaScenarioCommandLifecycle,
@@ -62,38 +63,6 @@ function runCommand(timeoutMs?: number) {
6263
});
6364
}
6465

65-
function isProcessRunning(pid: number) {
66-
try {
67-
process.kill(pid, 0);
68-
return true;
69-
} catch {
70-
return false;
71-
}
72-
}
73-
74-
async function waitForPidFile(filePath: string, timeoutMs = 2_000) {
75-
const deadline = Date.now() + timeoutMs;
76-
while (Date.now() < deadline) {
77-
const value = await readFile(filePath, "utf8").catch(() => "");
78-
if (/^[1-9]\d*$/u.test(value.trim())) {
79-
return Number(value.trim());
80-
}
81-
await sleep(10);
82-
}
83-
throw new Error(`timed out waiting for pid file ${filePath}`);
84-
}
85-
86-
async function waitForProcessExit(pid: number, timeoutMs = 2_000) {
87-
const deadline = Date.now() + timeoutMs;
88-
while (Date.now() < deadline) {
89-
if (!isProcessRunning(pid)) {
90-
return;
91-
}
92-
await sleep(10);
93-
}
94-
throw new Error(`timed out waiting for process ${pid} to exit`);
95-
}
96-
9766
describe.skipIf(process.platform === "win32")("qa scenario command real POSIX lifecycle", () => {
9867
afterEach(() => {
9968
resetQaScenarioCommandCleanupTimings();
@@ -153,9 +122,9 @@ describe.skipIf(process.platform === "win32")("qa scenario command real POSIX li
153122
if (descendantPid === undefined) {
154123
throw new Error("scenario command descendant did not expose its pid");
155124
}
156-
await waitForProcessExit(descendantPid);
125+
await waitForDead(descendantPid);
157126
} finally {
158-
if (descendantPid && isProcessRunning(descendantPid)) {
127+
if (descendantPid && isProcessAlive(descendantPid)) {
159128
process.kill(descendantPid, "SIGKILL");
160129
}
161130
await rm(root, { force: true, recursive: true });
@@ -197,12 +166,12 @@ describe.skipIf(process.platform === "win32")("qa scenario command real POSIX li
197166
expect(result.exitCode).toBe(1);
198167
expect(result.failureMessage).toBe("stdio-drain-timeout");
199168
expect(result.stdout).toContain("escaped descendant output");
200-
expect(isProcessRunning(descendantPid)).toBe(true);
169+
expect(isProcessAlive(descendantPid)).toBe(true);
201170
} finally {
202171
// A true setsid descendant is outside the original PGID by design.
203-
if (descendantPid && isProcessRunning(descendantPid)) {
172+
if (descendantPid && isProcessAlive(descendantPid)) {
204173
process.kill(descendantPid, "SIGKILL");
205-
await waitForProcessExit(descendantPid).catch(() => undefined);
174+
await waitForDead(descendantPid).catch(() => undefined);
206175
}
207176
await rm(root, { force: true, recursive: true });
208177
}
@@ -257,7 +226,7 @@ describe.skipIf(process.platform === "win32")("qa scenario command real POSIX li
257226
{ cwd: process.cwd(), env: process.env, stdio: "ignore" },
258227
);
259228
try {
260-
descendantPid = await waitForPidFile(descendantPidPath, 10_000);
229+
descendantPid = await waitForPidFile(descendantPidPath);
261230
controller.kill("SIGTERM");
262231
const [exitCode, signal] = await new Promise<[number | null, NodeJS.Signals | null]>(
263232
(resolve) => {
@@ -267,12 +236,12 @@ describe.skipIf(process.platform === "win32")("qa scenario command real POSIX li
267236

268237
expect(exitCode).toBeNull();
269238
expect(signal).toBe("SIGTERM");
270-
await waitForProcessExit(descendantPid);
239+
await waitForDead(descendantPid);
271240
} finally {
272241
if (controller.exitCode === null && controller.signalCode === null) {
273242
controller.kill("SIGKILL");
274243
}
275-
if (descendantPid && isProcessRunning(descendantPid)) {
244+
if (descendantPid && isProcessAlive(descendantPid)) {
276245
process.kill(descendantPid, "SIGKILL");
277246
}
278247
await rm(root, { force: true, recursive: true });

0 commit comments

Comments
 (0)