Skip to content

Commit 9e4abb2

Browse files
committed
fix(release): bound timeout grace safely
1 parent 988d4ff commit 9e4abb2

5 files changed

Lines changed: 86 additions & 4 deletions

File tree

‎scripts/lib/bounded-command.mjs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { runTsxCliShim } from "./tsx-cli-shim.mjs";
22

33
await runTsxCliShim(import.meta.url, {
4+
// The managed child owner may spend 5s on TERM grace and another 5s
5+
// verifying tree exit. Keep the launcher alive beyond that cleanup window.
6+
forceKillDelayMs: 15_000,
47
implementation: "./bounded-command.mts",
58
});

‎scripts/lib/bounded-command.mts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { runManagedCommand } from "./managed-child-process.mjs";
22

3+
const NODE_TIMEOUT_MAX_MS = 2 ** 31 - 1;
4+
35
function usage(message?: string): never {
46
if (message) {
57
console.error(message);
@@ -21,12 +23,16 @@ const timeoutMs = Number(timeoutMsRaw);
2123
if (!Number.isSafeInteger(timeoutMs)) {
2224
usage("timeout-ms must be a safe integer");
2325
}
26+
if (timeoutMs > NODE_TIMEOUT_MAX_MS) {
27+
usage(`timeout-ms must be at most ${NODE_TIMEOUT_MAX_MS}`);
28+
}
2429

2530
try {
2631
process.exitCode = await runManagedCommand({
2732
args,
2833
bin: command,
2934
requireProcessTreeExit: true,
35+
timeoutForceKillOnLeaderExit: true,
3036
timeoutKillGraceMs: 10_000,
3137
timeoutMs,
3238
});

‎scripts/lib/managed-child-process.mts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type ManagedCommandOptions = {
5656
type RunManagedCommandOptions = ManagedCommandOptions & {
5757
timeoutMs?: number;
5858
timeoutKillGraceMs?: number;
59+
timeoutForceKillOnLeaderExit?: boolean;
5960
requireProcessTreeExit?: boolean;
6061
runTaskkill?: TaskkillRunner;
6162
onReady?: (child: ChildProcess) => void;
@@ -280,6 +281,7 @@ export async function runManagedCommand({
280281
comSpec,
281282
timeoutMs,
282283
timeoutKillGraceMs,
284+
timeoutForceKillOnLeaderExit = false,
283285
requireProcessTreeExit = false,
284286
runTaskkill = spawnSync,
285287
onReady,
@@ -342,6 +344,7 @@ export async function runManagedCommand({
342344
outcome: ManagedCommandOutcome,
343345
stopSignal: NodeJS.Signals,
344346
forceKillDelayMs?: number,
347+
forceKillOnLeaderExit = false,
345348
) => {
346349
if (cancellation) {
347350
return cancellation;
@@ -351,6 +354,7 @@ export async function runManagedCommand({
351354
platform,
352355
runTaskkill,
353356
forceKillDelayMs,
357+
forceKillOnLeaderExit,
354358
});
355359
cancellation = finalization.then(
356360
() => outcome,
@@ -386,7 +390,7 @@ export async function runManagedCommand({
386390
});
387391
if (timeoutMs !== undefined) {
388392
timeoutTimer = setTimeout(() => {
389-
void stop({ type: "timeout" }, "SIGTERM", timeoutKillGraceMs);
393+
void stop({ type: "timeout" }, "SIGTERM", timeoutKillGraceMs, timeoutForceKillOnLeaderExit);
390394
}, timeoutMs);
391395
}
392396
signal?.addEventListener("abort", abort, { once: true });
@@ -456,10 +460,12 @@ async function finalizeManagedChild(
456460
platform,
457461
runTaskkill,
458462
forceKillDelayMs = FORCE_KILL_DELAY_MS,
463+
forceKillOnLeaderExit = false,
459464
}: {
460465
platform: NodeJS.Platform;
461466
runTaskkill: TaskkillRunner;
462467
forceKillDelayMs?: number;
468+
forceKillOnLeaderExit?: boolean;
463469
},
464470
) {
465471
// Nested wrappers own detached groups. Let them forward the signal before
@@ -502,7 +508,9 @@ async function finalizeManagedChild(
502508
return;
503509
}
504510
const now = Date.now();
505-
if (!forced && now >= forceAt) {
511+
// Bounded timeout callers can retire remaining descendants as soon as the
512+
// leader exits. Other owners retain their configured graceful-drain window.
513+
if (!forced && (now >= forceAt || (forceKillOnLeaderExit && exited))) {
506514
forced = true;
507515
if (groupState !== "dead") {
508516
terminateManagedChild(child, "SIGKILL", { platform, runTaskkill });

‎test/plugin-clawhub-release.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,6 +1713,9 @@ describe("plugin-clawhub-publish.sh", () => {
17131713
expect(readFileSync("scripts/lib/bounded-command.mts", "utf8")).toContain(
17141714
"timeoutKillGraceMs: 10_000",
17151715
);
1716+
expect(readFileSync("scripts/lib/bounded-command.mjs", "utf8")).toContain(
1717+
"forceKillDelayMs: 15_000",
1718+
);
17161719
});
17171720

17181721
it("prints help before package or ClawHub checks", () => {

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

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Managed Child Process tests cover managed child process script behavior.
2-
import { spawn } from "node:child_process";
2+
import { spawn, spawnSync } from "node:child_process";
33
import fs from "node:fs";
44
import os from "node:os";
55
import path from "node:path";
@@ -254,6 +254,65 @@ ${publish(2)}
254254
20_000,
255255
);
256256

257+
it("rejects timeout values beyond Node's timer range", () => {
258+
const result = spawnSync(
259+
process.execPath,
260+
[
261+
"scripts/lib/bounded-command.mjs",
262+
"2147483648",
263+
"--",
264+
process.execPath,
265+
"-e",
266+
"process.exit(0)",
267+
],
268+
{ encoding: "utf8" },
269+
);
270+
271+
expect(result.status).toBe(2);
272+
expect(result.stderr).toContain("timeout-ms must be at most 2147483647");
273+
expect(result.stderr).not.toContain("TimeoutOverflowWarning");
274+
});
275+
276+
posixIt(
277+
"keeps the bounded launcher alive until nested signal cleanup finishes",
278+
async () => {
279+
const dir = createTempDir("openclaw-bounded-launcher-cleanup-");
280+
const leafPath = path.join(dir, "leaf.mjs");
281+
const leafPidPath = path.join(dir, "leaf.pid");
282+
fs.writeFileSync(
283+
leafPath,
284+
`
285+
import fs from "node:fs";
286+
fs.writeFileSync(process.argv[2], String(process.pid));
287+
process.on("SIGTERM", () => {});
288+
setInterval(() => {}, 1_000);
289+
`,
290+
"utf8",
291+
);
292+
const launcher = spawn(
293+
process.execPath,
294+
["scripts/lib/bounded-command.mjs", "60000", "--", process.execPath, leafPath, leafPidPath],
295+
{ detached: true, stdio: "ignore" },
296+
);
297+
const closed = waitForChildClose(launcher, 12_000);
298+
let leafPid = 0;
299+
try {
300+
leafPid = await waitForPidFile(leafPidPath, 5_000);
301+
process.kill(-launcher.pid!, "SIGTERM");
302+
await expect(closed).resolves.toEqual({ code: 143, signal: null });
303+
await waitForDead(leafPid, 2_000);
304+
} finally {
305+
if (launcher.pid && isProcessAlive(launcher.pid)) {
306+
process.kill(-launcher.pid, "SIGKILL");
307+
}
308+
if (leafPid && isProcessAlive(leafPid)) {
309+
process.kill(leafPid, "SIGKILL");
310+
}
311+
}
312+
},
313+
15_000,
314+
);
315+
257316
it("maps forwarded signals to shell-compatible exit codes", () => {
258317
expect(signalExitCode("SIGHUP")).toBe(129);
259318
expect(signalExitCode("SIGINT")).toBe(130);
@@ -807,20 +866,23 @@ setInterval(() => {}, 1_000);
807866

808867
let descendantPid = 0;
809868
try {
869+
const startedAt = Date.now();
810870
await expect(
811871
runManagedCommand({
812872
args: [childPath, descendantPidPath, signalPath],
813873
bin: process.execPath,
814874
shell: false,
815875
stdio: "ignore",
816-
timeoutKillGraceMs: 100,
876+
timeoutForceKillOnLeaderExit: true,
877+
timeoutKillGraceMs: 10_000,
817878
timeoutMs: 500,
818879
}),
819880
).rejects.toMatchObject({ code: "ETIMEDOUT" });
820881

821882
descendantPid = Number(fs.readFileSync(descendantPidPath, "utf8"));
822883
expect(fs.readFileSync(signalPath, "utf8")).toBe("SIGTERM\n");
823884
expect(isProcessAlive(descendantPid)).toBe(false);
885+
expect(Date.now() - startedAt).toBeLessThan(3_000);
824886
} finally {
825887
if (descendantPid && isProcessAlive(descendantPid)) {
826888
process.kill(descendantPid, "SIGKILL");

0 commit comments

Comments
 (0)