Skip to content

Commit 41f57ea

Browse files
committed
fix(ci): resolve reaped groups and publish test PIDs atomically
1 parent b02bed7 commit 41f57ea

3 files changed

Lines changed: 93 additions & 23 deletions

File tree

‎scripts/lib/managed-child-process.mts‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,13 @@ export function inspectManagedProcessGroup(
187187
}
188188
try {
189189
process.kill(-pid, 0);
190-
if (
191-
platform === "linux" &&
192-
(child.exitCode != null || child.signalCode != null) &&
193-
isLinuxZombieProcessGroup(pid)
194-
) {
195-
return "dead";
190+
if (platform === "linux" && (child.exitCode != null || child.signalCode != null)) {
191+
if (isLinuxZombieProcessGroup(pid)) {
192+
return "dead";
193+
}
194+
// The group may be reaped while ps runs. Recheck kernel existence without
195+
// treating an empty or failed snapshot as proof of completion.
196+
process.kill(-pid, 0);
196197
}
197198
return "live";
198199
} catch (error) {

‎test/scripts/managed-child-process.test.ts‎

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Managed Child Process tests cover managed child process script behavior.
2-
import { spawn, spawnSync } from "node:child_process";
2+
import childProcess, { spawn, spawnSync } from "node:child_process";
33
import fs from "node:fs";
4+
import { syncBuiltinESMExports } from "node:module";
45
import os from "node:os";
56
import path from "node:path";
67
import { setTimeout as delay } from "node:timers/promises";
@@ -589,6 +590,83 @@ setInterval(() => {}, 1_000);
589590
).toBe("dead");
590591
});
591592

593+
it.each<{
594+
snapshot: "empty" | "live" | "failed" | "zombie";
595+
afterSnapshot: string | null;
596+
expected: ReturnType<typeof inspectManagedProcessGroup>;
597+
policy?: Parameters<typeof inspectManagedProcessGroup>[1]["errorPolicy"];
598+
platform?: NodeJS.Platform;
599+
running?: boolean;
600+
}>([
601+
{ snapshot: "empty", afterSnapshot: "ESRCH", expected: "dead" },
602+
{ snapshot: "live", afterSnapshot: "ESRCH", expected: "dead" },
603+
{ snapshot: "failed", afterSnapshot: "ESRCH", expected: "dead" },
604+
{ snapshot: "empty", afterSnapshot: null, expected: "live" },
605+
{ snapshot: "live", afterSnapshot: null, expected: "live" },
606+
{ snapshot: "failed", afterSnapshot: null, expected: "live" },
607+
{ snapshot: "zombie", afterSnapshot: null, expected: "dead" },
608+
{ snapshot: "empty", afterSnapshot: "EPERM", expected: "live" },
609+
{
610+
snapshot: "empty",
611+
afterSnapshot: "EPERM",
612+
policy: "indeterminate",
613+
expected: "indeterminate",
614+
},
615+
{ snapshot: "empty", afterSnapshot: "EPERM", policy: "verify-leader", expected: "dead" },
616+
{
617+
snapshot: "empty",
618+
afterSnapshot: "EIO",
619+
policy: "indeterminate",
620+
expected: "indeterminate",
621+
},
622+
{ snapshot: "empty", afterSnapshot: "EIO", expected: "dead" },
623+
{ snapshot: "zombie", afterSnapshot: null, platform: "darwin", expected: "live" },
624+
{ snapshot: "zombie", afterSnapshot: null, running: true, expected: "live" },
625+
])(
626+
"checks current group state after $snapshot snapshot ($afterSnapshot, $policy, $platform, $running)",
627+
({
628+
snapshot,
629+
afterSnapshot,
630+
expected,
631+
policy = "alive-on-eperm",
632+
platform = "linux",
633+
running,
634+
}) => {
635+
let inspected = false;
636+
const kill = vi.spyOn(process, "kill").mockImplementation(() => {
637+
if (inspected && afterSnapshot) {
638+
throw Object.assign(new Error("group lookup failed"), { code: afterSnapshot });
639+
}
640+
return true;
641+
});
642+
const ps = vi.spyOn(childProcess, "spawnSync").mockImplementation(() => {
643+
inspected = true;
644+
return {
645+
pid: 12346,
646+
output: [],
647+
signal: null,
648+
status: snapshot === "empty" ? 1 : 0,
649+
stdout: snapshot === "zombie" ? "12345 Z\n" : snapshot === "live" ? "12345 S\n" : "",
650+
stderr: "",
651+
...(snapshot === "failed" ? { error: new Error("ps unavailable") } : {}),
652+
};
653+
});
654+
syncBuiltinESMExports();
655+
try {
656+
expect(
657+
inspectManagedProcessGroup(
658+
{ pid: 12345, exitCode: running ? null : 0, signalCode: null },
659+
{ errorPolicy: policy, platform },
660+
),
661+
).toBe(expected);
662+
} finally {
663+
ps.mockRestore();
664+
kill.mockRestore();
665+
syncBuiltinESMExports();
666+
}
667+
},
668+
);
669+
592670
it("bounds process-group waiting when the group remains live", async () => {
593671
const kill = vi.spyOn(process, "kill").mockReturnValue(true);
594672

‎test/scripts/run-additional-boundary-checks.test.ts‎

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
runSingleCheck,
1818
selectChecksForShard,
1919
} from "../../scripts/run-additional-boundary-checks.mts";
20+
import { waitForFile, waitForPidFile } from "../helpers/process-wait.js";
2021

2122
function createOutputBuffer() {
2223
const chunks: string[] = [];
@@ -64,17 +65,6 @@ async function sleep(ms: number): Promise<void> {
6465
});
6566
}
6667

67-
async function waitForFile(filePath: string, timeoutMs: number): Promise<void> {
68-
const deadlineAt = Date.now() + timeoutMs;
69-
while (Date.now() < deadlineAt) {
70-
if (fs.existsSync(filePath)) {
71-
return;
72-
}
73-
await sleep(5);
74-
}
75-
throw new Error(`timeout waiting for ${filePath}`);
76-
}
77-
7868
async function waitForDead(pid: number, timeoutMs: number): Promise<void> {
7969
const deadlineAt = Date.now() + timeoutMs;
8070
while (Date.now() < deadlineAt) {
@@ -393,7 +383,8 @@ describe("run-additional-boundary-checks", () => {
393383
"const { spawn } = require('node:child_process');",
394384
"const fs = require('node:fs');",
395385
`const child = spawn(process.execPath, ['-e', ${JSON.stringify(childScript)}], { stdio: 'ignore' });`,
396-
"fs.writeFileSync(process.env.OPENCLAW_TEST_CHILD_PID, String(child.pid));",
386+
"fs.writeFileSync(process.env.OPENCLAW_TEST_CHILD_PID + '.tmp', String(child.pid));",
387+
"fs.renameSync(process.env.OPENCLAW_TEST_CHILD_PID + '.tmp', process.env.OPENCLAW_TEST_CHILD_PID);",
397388
"setInterval(() => {}, 1000);",
398389
].join("");
399390

@@ -411,8 +402,7 @@ describe("run-additional-boundary-checks", () => {
411402
},
412403
);
413404

414-
await waitForFile(childPidPath, 2000);
415-
childPid = Number(fs.readFileSync(childPidPath, "utf8"));
405+
childPid = await waitForPidFile(childPidPath, 2000);
416406
const result = await resultPromise;
417407

418408
expect(result.code).toBe(1);
@@ -444,7 +434,8 @@ describe("run-additional-boundary-checks", () => {
444434
"const { spawn } = require('node:child_process');",
445435
"const fs = require('node:fs');",
446436
`const child = spawn(process.execPath, ['-e', ${JSON.stringify(childScript)}], { stdio: 'ignore' });`,
447-
"fs.writeFileSync(process.env.OPENCLAW_TEST_CHILD_PID, String(child.pid));",
437+
"fs.writeFileSync(process.env.OPENCLAW_TEST_CHILD_PID + '.tmp', String(child.pid));",
438+
"fs.renameSync(process.env.OPENCLAW_TEST_CHILD_PID + '.tmp', process.env.OPENCLAW_TEST_CHILD_PID);",
448439
"fs.writeFileSync(process.env.OPENCLAW_TEST_READY, 'ready');",
449440
"process.on('SIGTERM', () => process.exit(0));",
450441
"setInterval(() => {}, 1000);",
@@ -482,7 +473,7 @@ await runChecks(
482473
});
483474

484475
await waitForFile(readyPath, 2000);
485-
childPid = Number(fs.readFileSync(childPidPath, "utf8"));
476+
childPid = await waitForPidFile(childPidPath, 2000);
486477
expect(Number.isInteger(childPid)).toBe(true);
487478
expect(isProcessAlive(childPid)).toBe(true);
488479

0 commit comments

Comments
 (0)