Skip to content

Commit 730c726

Browse files
committed
fix(test): clean Vitest runner child groups on signal
1 parent d72f7ed commit 730c726

6 files changed

Lines changed: 97 additions & 12 deletions

File tree

‎scripts/lib/vitest-batch-runner.mjs‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from "node:path";
33
import { fileURLToPath, pathToFileURL } from "node:url";
44
import { spawnPnpmRunner } from "../pnpm-runner.mjs";
55
import {
6+
forceKillVitestProcessGroup,
67
installVitestProcessGroupCleanup,
78
shouldUseDetachedVitestProcessGroup,
89
} from "../vitest-process-group.mjs";
@@ -26,8 +27,10 @@ export async function runVitestBatch(params) {
2627
});
2728
const teardownChildCleanup = installVitestProcessGroupCleanup({
2829
child,
30+
forceSignal: "SIGKILL",
31+
forceSignalDelayMs: 100,
2932
onSignal(signal) {
30-
forwardedSignal = signal;
33+
forwardedSignal ??= signal;
3134
},
3235
});
3336

@@ -37,14 +40,15 @@ export async function runVitestBatch(params) {
3740
});
3841
child.on("exit", (code, signal) => {
3942
teardownChildCleanup();
40-
if (signal) {
41-
process.kill(process.pid, signal);
42-
return;
43-
}
4443
if (forwardedSignal) {
44+
forceKillVitestProcessGroup(child);
4545
process.kill(process.pid, forwardedSignal);
4646
return;
4747
}
48+
if (signal) {
49+
process.kill(process.pid, signal);
50+
return;
51+
}
4852
resolve(code ?? 1);
4953
});
5054
});

‎scripts/run-vitest.mjs‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { boundaryTestFiles } from "../test/vitest/vitest.unit-paths.mjs";
1010
import { resolveLocalVitestEnv } from "./lib/vitest-local-scheduling.mjs";
1111
import { spawnPnpmRunner } from "./pnpm-runner.mjs";
1212
import {
13-
forwardSignalToVitestProcessGroup,
13+
forceKillVitestProcessGroup,
1414
installVitestProcessGroupCleanup,
1515
shouldUseDetachedVitestProcessGroup,
1616
} from "./vitest-process-group.mjs";
@@ -994,11 +994,19 @@ export function spawnWatchedVitestProcess({
994994
label,
995995
onNoOutputTimeout,
996996
}) {
997+
let forwardedSignal = null;
997998
const child = spawnVitestProcess({
998999
pnpmArgs,
9991000
spawnParams,
10001001
});
1001-
const teardownChildCleanup = installVitestProcessGroupCleanup({ child });
1002+
const teardownChildCleanup = installVitestProcessGroupCleanup({
1003+
child,
1004+
forceSignal: "SIGKILL",
1005+
forceSignalDelayMs: 100,
1006+
onSignal: (signal) => {
1007+
forwardedSignal ??= signal;
1008+
},
1009+
});
10021010
const teardownNoOutputWatchdog = installVitestNoOutputWatchdog({
10031011
streams: [child.stdout, child.stderr],
10041012
timeoutMs: resolveVitestNoOutputTimeoutMs(env),
@@ -1028,6 +1036,7 @@ export function spawnWatchedVitestProcess({
10281036

10291037
return {
10301038
child,
1039+
getForwardedSignal: () => forwardedSignal,
10311040
teardown: () => {
10321041
teardownChildCleanup();
10331042
teardownNoOutputWatchdog();
@@ -1054,13 +1063,19 @@ export function resolveTestProjectsRunnerSpawnParams(env, platform = process.pla
10541063
}
10551064

10561065
function spawnTestProjectsRunner(argv, env) {
1066+
let forwardedSignal = null;
10571067
const child = spawn(process.execPath, [testProjectsRunnerPath, ...argv], {
10581068
...resolveTestProjectsRunnerSpawnParams(env),
10591069
});
10601070
const teardown = installVitestProcessGroupCleanup({
10611071
child,
1072+
forceSignal: "SIGKILL",
1073+
forceSignalDelayMs: 100,
1074+
onSignal: (signal) => {
1075+
forwardedSignal ??= signal;
1076+
},
10621077
});
1063-
return { child, teardown };
1078+
return { child, getForwardedSignal: () => forwardedSignal, teardown };
10641079
}
10651080

10661081
function main(argv = process.argv.slice(2), env = process.env) {
@@ -1082,9 +1097,15 @@ function main(argv = process.argv.slice(2), env = process.env) {
10821097

10831098
const delegatedArgs = resolveTestProjectsDelegationArgs(argv);
10841099
if (delegatedArgs) {
1085-
const { child, teardown } = spawnTestProjectsRunner(delegatedArgs, env);
1100+
const { child, getForwardedSignal, teardown } = spawnTestProjectsRunner(delegatedArgs, env);
10861101
child.on("exit", (code, signal) => {
10871102
teardown();
1103+
const forwardedSignal = getForwardedSignal();
1104+
if (forwardedSignal) {
1105+
forceKillVitestProcessGroup(child);
1106+
process.kill(process.pid, forwardedSignal);
1107+
return;
1108+
}
10881109
if (signal) {
10891110
process.kill(process.pid, signal);
10901111
return;
@@ -1113,7 +1134,7 @@ function main(argv = process.argv.slice(2), env = process.env) {
11131134
throw error;
11141135
}
11151136

1116-
const { child, teardown } = spawnWatchedVitestProcess({
1137+
const { child, getForwardedSignal, teardown } = spawnWatchedVitestProcess({
11171138
pnpmArgs: ["exec", "node", ...resolveVitestNodeArgs(env), vitestCliEntry, ...guardedVitestArgs],
11181139
spawnParams: resolveVitestSpawnParams(spawnEnv),
11191140
env: spawnEnv,
@@ -1122,6 +1143,12 @@ function main(argv = process.argv.slice(2), env = process.env) {
11221143

11231144
child.on("exit", (code, signal) => {
11241145
teardown();
1146+
const forwardedSignal = getForwardedSignal();
1147+
if (forwardedSignal) {
1148+
forceKillVitestProcessGroup(child);
1149+
process.kill(process.pid, forwardedSignal);
1150+
return;
1151+
}
11251152
if (signal) {
11261153
process.kill(process.pid, signal);
11271154
return;

‎scripts/test-projects.mjs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
shouldRetryVitestNoOutputTimeout,
3939
writeVitestIncludeFile,
4040
} from "./test-projects.test-support.mjs";
41+
import { forceKillVitestProcessGroup } from "./vitest-process-group.mjs";
4142

4243
// Keep this shim so `pnpm test -- src/foo.test.ts` still forwards filters
4344
// cleanly instead of leaking pnpm's passthrough sentinel to Vitest.
@@ -88,7 +89,7 @@ function runVitestSpec(spec) {
8889
}
8990
let noOutputTimedOut = false;
9091
return new Promise((resolve, reject) => {
91-
const { child, teardown } = spawnWatchedVitestProcess({
92+
const { child, getForwardedSignal, teardown } = spawnWatchedVitestProcess({
9293
pnpmArgs: spec.pnpmArgs,
9394
env: spec.env,
9495
label: spec.config,
@@ -104,6 +105,12 @@ function runVitestSpec(spec) {
104105
child.on("exit", (code, signal) => {
105106
teardown();
106107
cleanupVitestRunSpec(spec);
108+
const forwardedSignal = getForwardedSignal();
109+
if (forwardedSignal) {
110+
forceKillVitestProcessGroup(child);
111+
resolve({ code: 143, noOutputTimedOut, signal: forwardedSignal });
112+
return;
113+
}
107114
resolve({ code: code ?? (signal ? 143 : 1), noOutputTimedOut, signal });
108115
});
109116

‎scripts/vitest-process-group.mjs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ export function forwardSignalToVitestProcessGroup(params) {
4141
}
4242
}
4343

44+
/**
45+
* Force-cleans any remaining processes in a Vitest child process group.
46+
*/
47+
export function forceKillVitestProcessGroup(child, kill = process.kill.bind(process)) {
48+
return forwardSignalToVitestProcessGroup({
49+
child,
50+
kill,
51+
signal: "SIGKILL",
52+
});
53+
}
54+
4455
function ensureProcessListenerCapacity(processObject, eventName, additionalListeners = 1) {
4556
if (
4657
typeof processObject.getMaxListeners !== "function" ||

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,14 +716,21 @@ describe("scripts/run-vitest", () => {
716716
os.tmpdir(),
717717
`openclaw-run-vitest-delegated-child-${process.pid}-${Date.now()}.pid`,
718718
);
719+
const descendantPidPath = nodePath.join(
720+
os.tmpdir(),
721+
`openclaw-run-vitest-delegated-descendant-${process.pid}-${Date.now()}.pid`,
722+
);
719723

720724
fs.writeFileSync(
721725
fixturePath,
722726
[
727+
'import { spawn } from "node:child_process";',
723728
'import fs from "node:fs";',
724729
'import { it } from "vitest";',
725730
'it("waits for wrapper termination", async () => {',
731+
' const child = spawn(process.execPath, ["-e", "process.on(\\\'SIGTERM\\\', () => {}); setInterval(() => {}, 1000);"], { stdio: "ignore" });',
726732
" fs.writeFileSync(process.env.OPENCLAW_DELEGATED_SIGNAL_CHILD_PID!, String(process.pid));",
733+
" fs.writeFileSync(process.env.OPENCLAW_DELEGATED_SIGNAL_DESCENDANT_PID!, String(child.pid));",
727734
" await new Promise(() => {});",
728735
"});",
729736
"",
@@ -737,33 +744,44 @@ describe("scripts/run-vitest", () => {
737744
env: {
738745
...process.env,
739746
OPENCLAW_DELEGATED_SIGNAL_CHILD_PID: childPidPath,
747+
OPENCLAW_DELEGATED_SIGNAL_DESCENDANT_PID: descendantPidPath,
740748
},
741749
stdio: "ignore",
742750
},
743751
);
744752
let childPid = 0;
753+
let descendantPid = 0;
745754

746755
try {
747756
await waitFor(() => fs.existsSync(childPidPath), 10_000);
757+
await waitFor(() => fs.existsSync(descendantPidPath), 10_000);
748758
childPid = Number(fs.readFileSync(childPidPath, "utf8"));
759+
descendantPid = Number(fs.readFileSync(descendantPidPath, "utf8"));
749760
expect(Number.isInteger(childPid)).toBe(true);
761+
expect(Number.isInteger(descendantPid)).toBe(true);
750762
expect(isProcessAlive(childPid)).toBe(true);
763+
expect(isProcessAlive(descendantPid)).toBe(true);
751764

752765
expect(runner.pid).toBeGreaterThan(0);
753766
process.kill(runner.pid!, "SIGTERM");
754767
const result = await waitForClose(runner);
755768

756-
expect(result).toEqual({ code: 143, signal: null });
769+
expect(result).toEqual({ code: null, signal: "SIGTERM" });
757770
await waitFor(() => !isProcessAlive(childPid), 5_000);
771+
await waitFor(() => !isProcessAlive(descendantPid), 5_000);
758772
} finally {
759773
if (runner.pid && isProcessAlive(runner.pid)) {
760774
process.kill(runner.pid, "SIGKILL");
761775
}
762776
if (childPid && isProcessAlive(childPid)) {
763777
process.kill(childPid, "SIGKILL");
764778
}
779+
if (descendantPid && isProcessAlive(descendantPid)) {
780+
process.kill(descendantPid, "SIGKILL");
781+
}
765782
fs.rmSync(fixturePath, { force: true });
766783
fs.rmSync(childPidPath, { force: true });
784+
fs.rmSync(descendantPidPath, { force: true });
767785
}
768786
});
769787

‎test/scripts/test-extension.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,25 +748,31 @@ describe("scripts/test-extension.mjs", () => {
748748
const root = mkdtempSync(path.join(tmpdir(), "openclaw-test-extension-signal-"));
749749
const fakePnpmPath = path.join(root, "pnpm");
750750
const childPidPath = path.join(root, "child.pid");
751+
const descendantPidPath = path.join(root, "descendant.pid");
751752
const signaledPath = path.join(root, "signaled");
752753

753754
writeFakePnpm(fakePnpmPath);
754755
const runner = spawn(process.execPath, [scriptPath, "firecrawl"], {
755756
cwd: process.cwd(),
756757
env: {
757758
...process.env,
759+
OPENCLAW_FAKE_PNPM_DESCENDANT_PID_PATH: descendantPidPath,
758760
OPENCLAW_FAKE_PNPM_PID_PATH: childPidPath,
759761
OPENCLAW_FAKE_PNPM_SIGNALED_PATH: signaledPath,
760762
npm_execpath: fakePnpmPath,
761763
},
762764
stdio: "ignore",
763765
});
764766
let childPid = 0;
767+
let descendantPid = 0;
765768

766769
try {
767770
await waitFor(() => fileExists(childPidPath), 5_000);
771+
await waitFor(() => fileExists(descendantPidPath), 5_000);
768772
childPid = Number(readFileSync(childPidPath, "utf8"));
773+
descendantPid = Number(readFileSync(descendantPidPath, "utf8"));
769774
expect(Number.isInteger(childPid)).toBe(true);
775+
expect(Number.isInteger(descendantPid)).toBe(true);
770776

771777
expect(runner.pid).toBeGreaterThan(0);
772778
process.kill(runner.pid!, "SIGTERM");
@@ -776,13 +782,17 @@ describe("scripts/test-extension.mjs", () => {
776782
await waitFor(() => fileExists(signaledPath), 5_000);
777783
expect(readFileSync(signaledPath, "utf8")).toBe("SIGTERM");
778784
await waitFor(() => !isProcessAlive(childPid), 5_000);
785+
await waitFor(() => !isProcessAlive(descendantPid), 5_000);
779786
} finally {
780787
if (runner.pid && isProcessAlive(runner.pid)) {
781788
process.kill(runner.pid, "SIGKILL");
782789
}
783790
if (childPid && isProcessAlive(childPid)) {
784791
process.kill(childPid, "SIGKILL");
785792
}
793+
if (descendantPid && isProcessAlive(descendantPid)) {
794+
process.kill(descendantPid, "SIGKILL");
795+
}
786796
rmSync(root, { force: true, recursive: true });
787797
}
788798
},
@@ -901,11 +911,19 @@ function writeFakePnpm(filePath: string): void {
901911
filePath,
902912
[
903913
"#!/usr/bin/env node",
914+
'const { spawn } = require("node:child_process");',
904915
'const fs = require("node:fs");',
905916
"if (process.env.OPENCLAW_FAKE_PNPM_ARGS_PATH) {",
906917
" fs.writeFileSync(process.env.OPENCLAW_FAKE_PNPM_ARGS_PATH, JSON.stringify(process.argv.slice(2)));",
907918
" process.exit(0);",
908919
"}",
920+
"if (process.env.OPENCLAW_FAKE_PNPM_DESCENDANT_PID_PATH) {",
921+
" const child = spawn(process.execPath, [",
922+
' "-e",',
923+
" \"process.on('SIGTERM', () => {}); setInterval(() => {}, 1000);\",",
924+
" ], { stdio: 'ignore' });",
925+
" fs.writeFileSync(process.env.OPENCLAW_FAKE_PNPM_DESCENDANT_PID_PATH, String(child.pid));",
926+
"}",
909927
"fs.writeFileSync(process.env.OPENCLAW_FAKE_PNPM_PID_PATH, String(process.pid));",
910928
'process.on("SIGTERM", () => {',
911929
' fs.writeFileSync(process.env.OPENCLAW_FAKE_PNPM_SIGNALED_PATH, "SIGTERM");',

0 commit comments

Comments
 (0)