Skip to content

Commit 6b5c435

Browse files
committed
fix: declaration steps succeed with lingering subprocesses
Fixes #132053. Verify POSIX process completion at each declaration owner before accepting success. Preserve Windows behavior without changing timeouts or shared defaults.
1 parent 6a961bd commit 6b5c435

5 files changed

Lines changed: 128 additions & 28 deletions

File tree

‎docs/reference/test.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ do not restrict the [SSH backend's Gateway host](/gateway/sandboxing#ssh-backend
125125

126126
## Shared test state and process helpers
127127

128+
Plugin SDK declaration preparation and `scripts/run-tsgo.mjs` require child work
129+
to finish before reporting success. On POSIX, each verifies its own managed
130+
process group: leftover children are terminated and the command fails instead of
131+
allowing artifact stamps or downstream checks to proceed. Windows retains normal
132+
joined-launcher completion because strict group verification is unsupported there.
133+
This does not detect descendants that deliberately leave the managed groups.
134+
128135
On POSIX hosts, the Vitest wrapper gives each invocation an owned temporary
129136
namespace through `TMPDIR`, `TMP`, and `TEMP`. Fallback SQLite state stays available
130137
across shared-worker files and module resets, then the wrapper removes the namespace

‎scripts/prepare-extension-package-boundary-artifacts.mts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,8 @@ export async function runNodeStep(
714714
env: params.env ? { ...process.env, ...params.env } : process.env,
715715
shell: false,
716716
stdio: ["ignore", "pipe", "pipe"],
717+
// Artifact writers must finish before stamps, dependent readers, or lock release.
718+
requireProcessTreeExit: process.platform !== "win32",
717719
timeoutMs: resolvedTimeoutMs,
718720
signal: params.abortController?.signal,
719721
abortKillGraceMs: Math.max(

‎scripts/run-tsgo.mts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ async function main(): Promise<void> {
8181
bin: tsgoPath,
8282
args: finalArgs,
8383
env,
84+
// The compiler owns a nested group that outer preparation cannot verify.
85+
requireProcessTreeExit: process.platform !== "win32",
8486
timeoutMs,
8587
});
8688
} catch (error) {

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

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@ require('node:child_process').spawn(process.execPath, ['-e', ${JSON.stringify(le
934934
expect(isProcessAlive(childPid)).toBe(false);
935935
});
936936

937-
it("allows bounded retry output and normal long-running work to complete", async () => {
937+
it("allows bounded retry output to complete", async () => {
938938
await expect(
939939
runManagedCommand({
940940
bin: process.execPath,
@@ -947,6 +947,9 @@ require('node:child_process').spawn(process.execPath, ['-e', ${JSON.stringify(le
947947
timeoutMs: 1_000,
948948
}),
949949
).resolves.toBe(0);
950+
});
951+
952+
posixIt("allows strict normal long-running work to complete", async () => {
950953
await expect(
951954
runManagedCommand({
952955
bin: process.execPath,
@@ -976,17 +979,14 @@ require('node:child_process').spawn(process.execPath, ['-e', ${JSON.stringify(le
976979
expect(isProcessAlive(childPid)).toBe(false);
977980
});
978981

979-
posixIt("rejects and drains descendants left after a successful leader exit", async () => {
980-
const dir = createTempDir("openclaw-managed-lingering-");
981-
const descendantPidPath = path.join(dir, "descendant.pid");
982-
let descendantPid = 0;
983-
try {
984-
await expect(
985-
runManagedCommand({
986-
bin: process.execPath,
987-
args: [
988-
"-e",
989-
`
982+
posixIt.each(["managed", "preparation"])(
983+
"rejects and drains descendants left after a successful leader exit through %s",
984+
async (runner) => {
985+
const dir = createTempDir("openclaw-managed-lingering-");
986+
const descendantPidPath = path.join(dir, "descendant.pid");
987+
const args = [
988+
"-e",
989+
`
990990
const { spawn } = require("node:child_process");
991991
const child = spawn(process.execPath, [
992992
"-e",
@@ -995,22 +995,41 @@ const child = spawn(process.execPath, [
995995
], { stdio: ["ignore", "ignore", "ignore", "ipc"] });
996996
child.once("message", () => process.exit(0));
997997
`,
998-
descendantPidPath,
999-
],
1000-
requireProcessTreeExit: true,
1001-
shell: false,
1002-
stdio: "ignore",
1003-
timeoutMs: 1_000,
1004-
}),
1005-
).rejects.toMatchObject({ code: "EPROCESSGROUP_CLEANUP_FAILED" });
1006-
descendantPid = Number(fs.readFileSync(descendantPidPath, "utf8"));
1007-
expect(isProcessAlive(descendantPid)).toBe(false);
1008-
} finally {
1009-
if (descendantPid && isProcessAlive(descendantPid)) {
1010-
process.kill(descendantPid, "SIGKILL");
998+
descendantPidPath,
999+
];
1000+
try {
1001+
const command =
1002+
runner === "preparation"
1003+
? runNodeStep("lingering-prep", args, 1_000)
1004+
: runManagedCommand({
1005+
bin: process.execPath,
1006+
args,
1007+
requireProcessTreeExit: true,
1008+
shell: false,
1009+
stdio: "ignore",
1010+
timeoutMs: 1_000,
1011+
});
1012+
const failure = await command.catch((error: unknown) => error);
1013+
const descendantPid = Number(fs.readFileSync(descendantPidPath, "utf8"));
1014+
expect.soft(failure).toMatchObject({ code: "EPROCESSGROUP_CLEANUP_FAILED" });
1015+
expect
1016+
.soft(
1017+
isProcessAlive(descendantPid),
1018+
`descendant ${descendantPid} must be absent at settlement`,
1019+
)
1020+
.toBe(false);
1021+
} finally {
1022+
// Recover the recorded PID even when an unexpected success fails the assertion.
1023+
if (fs.existsSync(descendantPidPath)) {
1024+
const descendantPid = Number(fs.readFileSync(descendantPidPath, "utf8"));
1025+
if (Number.isSafeInteger(descendantPid) && descendantPid > 1) {
1026+
if (isProcessAlive(descendantPid)) process.kill(descendantPid, "SIGKILL");
1027+
await waitForDead(descendantPid, 2_000);
1028+
}
1029+
}
10111030
}
1012-
}
1013-
});
1031+
},
1032+
);
10141033

10151034
posixIt(
10161035
"kills managed child process group descendants when the runner is terminated",

‎test/scripts/run-tsgo.test.ts‎

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,28 @@ import {
1010
shouldSkipSparseTsgoGuardError,
1111
} from "../../scripts/lib/tsgo-sparse-guard.mts";
1212
import { resolveTsgoTimeoutMs } from "../../scripts/run-tsgo.mts";
13-
import { waitForChildClose, waitForDead, waitForPidFile } from "../helpers/process-wait.js";
13+
import {
14+
isProcessAlive,
15+
waitForChildClose,
16+
waitForDead,
17+
waitForPidFile,
18+
} from "../helpers/process-wait.js";
1419
import { createScriptTestHarness } from "./test-helpers.js";
1520

1621
const { createTempDir } = createScriptTestHarness();
1722

23+
it("runs the installed compiler version through the real tsgo wrapper", () => {
24+
const result = spawnSync(process.execPath, [path.resolve("scripts/run-tsgo.mjs"), "--version"], {
25+
encoding: "utf8",
26+
timeout: 25_000,
27+
killSignal: "SIGKILL",
28+
});
29+
30+
expect(result.error).toBeUndefined();
31+
expect(result.status).toBe(0);
32+
expect(result.stdout).toMatch(/^Version \d/m);
33+
}, 30_000);
34+
1835
describe("run-tsgo sparse guard", () => {
1936
it("ends sparse-checkout failures with the stable failure trailer", () => {
2037
const cwd = createTempDir("openclaw-run-tsgo-");
@@ -304,6 +321,59 @@ describe.skipIf(process.platform === "win32")("run-tsgo watchdog", () => {
304321
}
305322
}
306323

324+
it("rejects and drains compiler descendants left after a successful leader exit", async () => {
325+
const cwd = createTempDir("openclaw-run-tsgo-lingering-");
326+
const descendantPidPath = path.join(cwd, "descendant.pid");
327+
writeFakeTsgo(
328+
cwd,
329+
`#!/usr/bin/env node
330+
const fs = require("node:fs");
331+
const { spawn } = require("node:child_process");
332+
fs.writeFileSync("fake-tsgo.pid", String(process.pid));
333+
const child = spawn(process.execPath, ["-e", ${JSON.stringify(`
334+
const fs = require("node:fs");
335+
setInterval(() => {}, 1000);
336+
fs.writeFileSync(process.argv[1], String(process.pid));
337+
process.send("ready");
338+
process.disconnect();
339+
`)}, ${JSON.stringify(descendantPidPath)}], { stdio: ["ignore", "ignore", "ignore", "ipc"] });
340+
child.once("message", () => process.exit(0));
341+
`,
342+
);
343+
let observedPids: Array<number | undefined> = [];
344+
let liveBeforeTeardown: number[] = [];
345+
try {
346+
const result = runFakeTsgo(cwd, undefined, (pid) => {
347+
observedPids = [
348+
pid,
349+
fs.existsSync(descendantPidPath)
350+
? Number(fs.readFileSync(descendantPidPath, "utf8"))
351+
: undefined,
352+
];
353+
liveBeforeTeardown = observedPids.filter(
354+
(owned): owned is number => owned !== undefined && isProcessAlive(owned),
355+
);
356+
});
357+
expect(result.error).toBeUndefined();
358+
expect(
359+
observedPids.every((pid) => pid !== undefined && Number.isSafeInteger(pid) && pid > 1),
360+
).toBe(true);
361+
expect.soft(result.status).toBe(1);
362+
expect.soft(result.stderr).toContain("EPROCESSGROUP_CLEANUP_FAILED");
363+
expect
364+
.soft(liveBeforeTeardown, "compiler descendants must be absent before fixture teardown")
365+
.toEqual([]);
366+
} finally {
367+
for (const pidFile of [descendantPidPath, path.join(cwd, "fake-tsgo.pid")]) {
368+
if (!fs.existsSync(pidFile)) continue;
369+
const pid = Number(fs.readFileSync(pidFile, "utf8"));
370+
if (!Number.isSafeInteger(pid) || pid <= 1) continue;
371+
if (isProcessAlive(pid)) process.kill(pid, "SIGKILL");
372+
await waitForDead(pid, 2_000);
373+
}
374+
}
375+
}, 30_000);
376+
307377
it.each([{ bound: "0" }, { bound: "abc" }])(
308378
"explains a rejected OPENCLAW_TSGO_TIMEOUT_MS of $bound instead of crashing",
309379
({ bound }) => {

0 commit comments

Comments
 (0)