Skip to content

Commit 912946f

Browse files
committed
fix(test): kill openclaw test process groups
1 parent 5893758 commit 912946f

2 files changed

Lines changed: 49 additions & 5 deletions

File tree

‎test/helpers/openclaw-test-instance.test.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// OpenClaw test instance tests cover spawned test instance lifecycle.
22
import fs from "node:fs/promises";
33
import path from "node:path";
4-
import { describe, expect, it } from "vitest";
4+
import { describe, expect, it, vi } from "vitest";
55
import { createOpenClawTestInstance, testing } from "./openclaw-test-instance.js";
66

77
async function expectPathMissing(targetPath: string): Promise<void> {
@@ -44,6 +44,24 @@ describe("openclaw test instance", () => {
4444
).rejects.toThrow("gateway exited before listening");
4545
});
4646

47+
it("signals test instance process groups on POSIX", () => {
48+
const child = {
49+
pid: 1234,
50+
kill: vi.fn(() => true),
51+
};
52+
const killProcess = vi.fn(() => true);
53+
54+
testing.signalOpenClawTestProcess(child, "SIGKILL", killProcess);
55+
56+
if (process.platform === "win32") {
57+
expect(killProcess).not.toHaveBeenCalled();
58+
expect(child.kill).toHaveBeenCalledWith("SIGKILL");
59+
} else {
60+
expect(killProcess).toHaveBeenCalledWith(-1234, "SIGKILL");
61+
expect(child.kill).not.toHaveBeenCalled();
62+
}
63+
});
64+
4765
it("creates isolated config and spawn env without mutating process env", async () => {
4866
const previousHome = process.env.HOME;
4967
const inst = await createOpenClawTestInstance({

‎test/helpers/openclaw-test-instance.ts‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ type BoundedStringLog = string[] & {
7373
truncated?: boolean;
7474
};
7575

76+
type OpenClawTestChildProcess = Pick<ChildProcessWithoutNullStreams, "kill" | "pid">;
77+
7678
function createBoundedStringLog(): string[] {
7779
const log = [] as BoundedStringLog;
7880
log.byteLength = 0;
@@ -152,6 +154,7 @@ async function prepareGatewayEntrypoint(cwd: string): Promise<string[]> {
152154
cwd,
153155
env: { ...process.env, VITEST: "1" },
154156
stdio: ["ignore", "pipe", "pipe"],
157+
detached: shouldUseOpenClawTestProcessGroup(),
155158
});
156159
child.stdout?.setEncoding("utf8");
157160
child.stderr?.setEncoding("utf8");
@@ -167,7 +170,7 @@ async function prepareGatewayEntrypoint(cwd: string): Promise<string[]> {
167170
]);
168171

169172
if (completed === null) {
170-
child.kill("SIGKILL");
173+
signalOpenClawTestProcess(child, "SIGKILL");
171174
throw new Error(`timeout preparing gateway entrypoint\n${formatLogs(stdout, stderr)}`);
172175
}
173176
if (completed.code !== 0) {
@@ -401,6 +404,7 @@ export async function createOpenClawTestInstance(
401404
cwd,
402405
env,
403406
stdio: ["ignore", "pipe", "pipe"],
407+
detached: shouldUseOpenClawTestProcessGroup(),
404408
},
405409
);
406410

@@ -428,7 +432,7 @@ export async function createOpenClawTestInstance(
428432
}
429433
if (!hasChildExited(child) && !child.killed) {
430434
try {
431-
child.kill("SIGTERM");
435+
signalOpenClawTestProcess(child, "SIGTERM");
432436
} catch {
433437
// ignore
434438
}
@@ -439,7 +443,7 @@ export async function createOpenClawTestInstance(
439443
);
440444
if (!exited && !hasChildExited(child) && !child.killed) {
441445
try {
442-
child.kill("SIGKILL");
446+
signalOpenClawTestProcess(child, "SIGKILL");
443447
} catch {
444448
// ignore
445449
}
@@ -479,6 +483,7 @@ async function runCommand(params: {
479483
cwd: params.cwd,
480484
env: params.env,
481485
stdio: ["ignore", "pipe", "pipe"],
486+
detached: shouldUseOpenClawTestProcessGroup(),
482487
});
483488
child.stdout?.setEncoding("utf8");
484489
child.stderr?.setEncoding("utf8");
@@ -493,7 +498,7 @@ async function runCommand(params: {
493498
sleep(params.timeoutMs).then(() => null),
494499
]);
495500
if (completed === null) {
496-
child.kill("SIGKILL");
501+
signalOpenClawTestProcess(child, "SIGKILL");
497502
await waitForGatewayExit(child, GATEWAY_STOP_TIMEOUT_MS);
498503
throw new Error(
499504
`command timed out after ${params.timeoutMs}ms: ${params.args.join(" ")}\n${formatLogs(stdout, stderr)}`,
@@ -506,10 +511,31 @@ async function runCommand(params: {
506511
};
507512
}
508513

514+
function shouldUseOpenClawTestProcessGroup(): boolean {
515+
return process.platform !== "win32";
516+
}
517+
518+
function signalOpenClawTestProcess(
519+
child: OpenClawTestChildProcess,
520+
signal: NodeJS.Signals,
521+
killProcess: (pid: number, signal: NodeJS.Signals) => boolean = process.kill,
522+
): void {
523+
if (shouldUseOpenClawTestProcessGroup() && typeof child.pid === "number") {
524+
try {
525+
killProcess(-child.pid, signal);
526+
return;
527+
} catch {
528+
// Fall back to the direct child if the process group already exited.
529+
}
530+
}
531+
child.kill(signal);
532+
}
533+
509534
export const testing = {
510535
appendLogChunk,
511536
createBoundedStringLog,
512537
formatLogs,
513538
hasChildExited,
539+
signalOpenClawTestProcess,
514540
waitForPortOpen,
515541
};

0 commit comments

Comments
 (0)