mirror of
https://github.com/openclaw/openclaw.git
synced 2026-07-10 00:11:19 +00:00
fix(test-live): force cleanup shard child groups on parent signal
This commit is contained in:
parent
c33f8c20ef
commit
24b6e6ba96
2 changed files with 130 additions and 6 deletions
|
|
@ -6,6 +6,7 @@ import path from "node:path";
|
|||
import { fileURLToPath } from "node:url";
|
||||
import { spawnPnpmRunner } from "./pnpm-runner.mjs";
|
||||
import {
|
||||
forwardSignalToVitestProcessGroup,
|
||||
installVitestProcessGroupCleanup,
|
||||
shouldUseDetachedVitestProcessGroup,
|
||||
} from "./vitest-process-group.mjs";
|
||||
|
|
@ -661,18 +662,25 @@ if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.me
|
|||
let forwardedSignal = null;
|
||||
const teardown = installVitestProcessGroupCleanup({
|
||||
child,
|
||||
forceSignal: "SIGKILL",
|
||||
forceSignalDelayMs: 100,
|
||||
onSignal: (signal) => {
|
||||
forwardedSignal ??= signal;
|
||||
},
|
||||
});
|
||||
child.on("exit", (code, signal) => {
|
||||
teardown();
|
||||
if (signal) {
|
||||
process.kill(process.pid, signal);
|
||||
if (forwardedSignal) {
|
||||
forwardSignalToVitestProcessGroup({
|
||||
child,
|
||||
kill: process.kill.bind(process),
|
||||
signal: "SIGKILL",
|
||||
});
|
||||
process.kill(process.pid, forwardedSignal);
|
||||
return;
|
||||
}
|
||||
if (forwardedSignal) {
|
||||
process.kill(process.pid, forwardedSignal);
|
||||
if (signal) {
|
||||
process.kill(process.pid, signal);
|
||||
return;
|
||||
}
|
||||
if ((code ?? 1) === 0) {
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
// Test Live Shard tests cover test live shard script behavior.
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { spawn, spawnSync } from "node:child_process";
|
||||
import { chmodSync, existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import path from "node:path";
|
||||
import { setTimeout as delay } from "node:timers/promises";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
LIVE_TEST_SHARDS,
|
||||
|
|
@ -421,4 +422,119 @@ describe("scripts/test-live-shard", () => {
|
|||
stdio: "inherit",
|
||||
});
|
||||
});
|
||||
|
||||
it.skipIf(process.platform === "win32")(
|
||||
"cleans live shard descendants before forwarding parent SIGTERM",
|
||||
async () => {
|
||||
const root = mkdtempSync(path.join(tmpdir(), "openclaw-live-shard-signal-"));
|
||||
const fakePnpmPath = path.join(root, "pnpm");
|
||||
const childPidPath = path.join(root, "child.pid");
|
||||
const descendantPidPath = path.join(root, "descendant.pid");
|
||||
const signaledPath = path.join(root, "signaled");
|
||||
let childPid = 0;
|
||||
let descendantPid = 0;
|
||||
let runner: ReturnType<typeof spawn> | undefined;
|
||||
|
||||
try {
|
||||
writeFakePnpm(fakePnpmPath);
|
||||
runner = spawn(
|
||||
process.execPath,
|
||||
["scripts/test-live-shard.mjs", "native-live-src-agents"],
|
||||
{
|
||||
env: {
|
||||
...process.env,
|
||||
OPENCLAW_FAKE_PNPM_DESCENDANT_PID_PATH: descendantPidPath,
|
||||
OPENCLAW_FAKE_PNPM_PID_PATH: childPidPath,
|
||||
OPENCLAW_FAKE_PNPM_SIGNALED_PATH: signaledPath,
|
||||
npm_execpath: fakePnpmPath,
|
||||
},
|
||||
stdio: "ignore",
|
||||
},
|
||||
);
|
||||
|
||||
await waitFor(() => existsSync(childPidPath), 5_000);
|
||||
await waitFor(() => existsSync(descendantPidPath), 5_000);
|
||||
childPid = Number(readFileSync(childPidPath, "utf8"));
|
||||
descendantPid = Number(readFileSync(descendantPidPath, "utf8"));
|
||||
expect(Number.isInteger(childPid)).toBe(true);
|
||||
expect(Number.isInteger(descendantPid)).toBe(true);
|
||||
|
||||
runner.kill("SIGTERM");
|
||||
|
||||
await expect(waitForClose(runner)).resolves.toEqual({ code: null, signal: "SIGTERM" });
|
||||
await waitFor(() => existsSync(signaledPath), 5_000);
|
||||
expect(readFileSync(signaledPath, "utf8")).toBe("SIGTERM");
|
||||
await waitFor(() => !isProcessAlive(childPid), 5_000);
|
||||
await waitFor(() => !isProcessAlive(descendantPid), 5_000);
|
||||
} finally {
|
||||
if (runner?.pid && isProcessAlive(runner.pid)) {
|
||||
process.kill(runner.pid, "SIGKILL");
|
||||
}
|
||||
if (childPid && isProcessAlive(childPid)) {
|
||||
process.kill(childPid, "SIGKILL");
|
||||
}
|
||||
if (descendantPid && isProcessAlive(descendantPid)) {
|
||||
process.kill(descendantPid, "SIGKILL");
|
||||
}
|
||||
rmSync(root, { force: true, recursive: true });
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
function writeFakePnpm(filePath: string): void {
|
||||
writeFileSync(
|
||||
filePath,
|
||||
[
|
||||
"#!/usr/bin/env node",
|
||||
'const { spawn } = require("node:child_process");',
|
||||
'const fs = require("node:fs");',
|
||||
"const child = spawn(process.execPath, [",
|
||||
' "-e",',
|
||||
" \"process.on('SIGTERM', () => {}); setInterval(() => {}, 1000);\",",
|
||||
"], { stdio: 'ignore' });",
|
||||
"fs.writeFileSync(process.env.OPENCLAW_FAKE_PNPM_DESCENDANT_PID_PATH, String(child.pid));",
|
||||
"fs.writeFileSync(process.env.OPENCLAW_FAKE_PNPM_PID_PATH, String(process.pid));",
|
||||
'process.on("SIGTERM", () => {',
|
||||
' fs.writeFileSync(process.env.OPENCLAW_FAKE_PNPM_SIGNALED_PATH, "SIGTERM");',
|
||||
" process.exit(0);",
|
||||
"});",
|
||||
"setInterval(() => {}, 1000);",
|
||||
"",
|
||||
].join("\n"),
|
||||
);
|
||||
chmodSync(filePath, 0o755);
|
||||
}
|
||||
|
||||
async function waitFor(condition: () => boolean, timeoutMs: number): Promise<void> {
|
||||
const startedAt = Date.now();
|
||||
while (!condition()) {
|
||||
if (Date.now() - startedAt > timeoutMs) {
|
||||
throw new Error("timed out waiting for condition");
|
||||
}
|
||||
await delay(25);
|
||||
}
|
||||
}
|
||||
|
||||
async function waitForClose(
|
||||
child: ReturnType<typeof spawn>,
|
||||
timeoutMs = 5_000,
|
||||
): Promise<{ code: number | null; signal: NodeJS.Signals | null }> {
|
||||
return await Promise.race([
|
||||
new Promise<{ code: number | null; signal: NodeJS.Signals | null }>((resolve) => {
|
||||
child.once("close", (code, signal) => resolve({ code, signal }));
|
||||
}),
|
||||
delay(timeoutMs).then(() => {
|
||||
throw new Error("timed out waiting for child close");
|
||||
}),
|
||||
]);
|
||||
}
|
||||
|
||||
function isProcessAlive(pid: number): boolean {
|
||||
try {
|
||||
process.kill(pid, 0);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue