mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-08 02:52:15 +00:00
fix(bench): kill gateway child trees on windows
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
// Gateway Bench Child script supports OpenClaw repository automation.
|
||||
import type { ChildProcessWithoutNullStreams } from "node:child_process";
|
||||
import { spawnSync, type ChildProcessWithoutNullStreams } from "node:child_process";
|
||||
|
||||
const TEARDOWN_GRACE_MS = 2_000;
|
||||
const TEARDOWN_KILL_GRACE_MS = 1_000;
|
||||
@@ -14,6 +14,13 @@ export type StopChildResult = ChildExit & {
|
||||
exitedBeforeTeardown: boolean;
|
||||
};
|
||||
|
||||
export type StopChildOptions = {
|
||||
killGraceMs?: number;
|
||||
platform?: NodeJS.Platform;
|
||||
runTaskkill?: typeof spawnSync;
|
||||
teardownGraceMs?: number;
|
||||
};
|
||||
|
||||
export function delay(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(resolve, ms);
|
||||
@@ -22,10 +29,14 @@ export function delay(ms: number): Promise<void> {
|
||||
|
||||
export async function stopChild(
|
||||
child: ChildProcessWithoutNullStreams,
|
||||
options: { killGraceMs?: number; teardownGraceMs?: number } = {},
|
||||
options: StopChildOptions = {},
|
||||
): Promise<StopChildResult> {
|
||||
const teardownGraceMs = options.teardownGraceMs ?? TEARDOWN_GRACE_MS;
|
||||
const killGraceMs = options.killGraceMs ?? TEARDOWN_KILL_GRACE_MS;
|
||||
const processTreeOptions = {
|
||||
platform: options.platform ?? process.platform,
|
||||
runTaskkill: options.runTaskkill ?? spawnSync,
|
||||
};
|
||||
let observedExit: ChildExit | null = null;
|
||||
const directExit = (): ChildExit | null =>
|
||||
observedExit ??
|
||||
@@ -34,7 +45,7 @@ export async function stopChild(
|
||||
: null);
|
||||
const currentExit = (): ChildExit | null => {
|
||||
const exit = directExit();
|
||||
if (exit == null || isProcessTreeAlive(child)) {
|
||||
if (exit == null || isProcessTreeAlive(child, processTreeOptions)) {
|
||||
return null;
|
||||
}
|
||||
return exit;
|
||||
@@ -42,26 +53,26 @@ export async function stopChild(
|
||||
const waitForProcessTreeExit = async (ms: number): Promise<boolean> => {
|
||||
const deadlineAt = Date.now() + ms;
|
||||
while (Date.now() < deadlineAt) {
|
||||
if (!isProcessTreeAlive(child)) {
|
||||
if (!isProcessTreeAlive(child, processTreeOptions)) {
|
||||
return true;
|
||||
}
|
||||
await delay(Math.min(EXIT_POLL_MS, deadlineAt - Date.now()));
|
||||
}
|
||||
return !isProcessTreeAlive(child);
|
||||
return !isProcessTreeAlive(child, processTreeOptions);
|
||||
};
|
||||
const cleanupExitedProcessTree = async (
|
||||
exit: ChildExit,
|
||||
exitedBeforeTeardown: boolean,
|
||||
): Promise<StopChildResult> => {
|
||||
if (!isProcessTreeAlive(child)) {
|
||||
if (!isProcessTreeAlive(child, processTreeOptions)) {
|
||||
return { ...exit, exitedBeforeTeardown };
|
||||
}
|
||||
const sentTeardownSignal = killProcessTree(child, "SIGTERM");
|
||||
const sentTeardownSignal = killProcessTree(child, "SIGTERM", processTreeOptions);
|
||||
if (sentTeardownSignal) {
|
||||
await waitForProcessTreeExit(teardownGraceMs);
|
||||
}
|
||||
if (sentTeardownSignal && isProcessTreeAlive(child)) {
|
||||
killProcessTree(child, "SIGKILL");
|
||||
if (sentTeardownSignal && isProcessTreeAlive(child, processTreeOptions)) {
|
||||
killProcessTree(child, "SIGKILL", processTreeOptions);
|
||||
await waitForProcessTreeExit(killGraceMs);
|
||||
}
|
||||
if (!sentTeardownSignal) {
|
||||
@@ -106,7 +117,7 @@ export async function stopChild(
|
||||
return await cleanupExitedProcessTree(queuedExit, true);
|
||||
}
|
||||
|
||||
const sentTeardownSignal = killProcessTree(child, "SIGTERM");
|
||||
const sentTeardownSignal = killProcessTree(child, "SIGTERM", processTreeOptions);
|
||||
const gracefulExit = await waitForExit(teardownGraceMs);
|
||||
if (gracefulExit != null) {
|
||||
return { ...gracefulExit, exitedBeforeTeardown: !sentTeardownSignal };
|
||||
@@ -121,7 +132,7 @@ export async function stopChild(
|
||||
return { exitCode: null, exitedBeforeTeardown: true, signal: null };
|
||||
}
|
||||
|
||||
killProcessTree(child, "SIGKILL");
|
||||
killProcessTree(child, "SIGKILL", processTreeOptions);
|
||||
const killedExit = await waitForExit(killGraceMs);
|
||||
const finalExit = killedExit ?? currentExit();
|
||||
if (finalExit != null) {
|
||||
@@ -139,8 +150,11 @@ function releaseUnsettledChild(child: ChildProcessWithoutNullStreams): void {
|
||||
child.unref();
|
||||
}
|
||||
|
||||
function isProcessTreeAlive(child: ChildProcessWithoutNullStreams): boolean {
|
||||
if (process.platform === "win32" || child.pid === undefined) {
|
||||
function isProcessTreeAlive(
|
||||
child: ChildProcessWithoutNullStreams,
|
||||
{ platform = process.platform }: Pick<StopChildOptions, "platform"> = {},
|
||||
): boolean {
|
||||
if (platform === "win32" || child.pid === undefined) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
@@ -156,8 +170,12 @@ function isProcessStillExistsError(error: unknown): boolean {
|
||||
return code === "EPERM";
|
||||
}
|
||||
|
||||
function killProcessTree(child: ChildProcessWithoutNullStreams, signal: NodeJS.Signals): boolean {
|
||||
if (process.platform !== "win32" && child.pid !== undefined) {
|
||||
function killProcessTree(
|
||||
child: ChildProcessWithoutNullStreams,
|
||||
signal: NodeJS.Signals,
|
||||
{ platform = process.platform, runTaskkill = spawnSync }: StopChildOptions = {},
|
||||
): boolean {
|
||||
if (platform !== "win32" && child.pid !== undefined) {
|
||||
try {
|
||||
process.kill(-child.pid, signal);
|
||||
return true;
|
||||
@@ -165,5 +183,15 @@ function killProcessTree(child: ChildProcessWithoutNullStreams, signal: NodeJS.S
|
||||
// Fall back to the direct child below.
|
||||
}
|
||||
}
|
||||
if (platform === "win32" && child.pid !== undefined) {
|
||||
const args = ["/PID", String(child.pid), "/T"];
|
||||
if (signal === "SIGKILL") {
|
||||
args.push("/F");
|
||||
}
|
||||
const result = runTaskkill("taskkill", args, { stdio: "ignore" });
|
||||
if (!result.error && result.status === 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return child.kill(signal);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user