Skip to content

Commit f46f9b5

Browse files
PollyBot13obviyus
andauthored
fix(release): bound ClawHub publish without GNU timeout (#125296)
Allow ClawHub release validation and publish retries to stay bounded on stock macOS by using the existing managed-process owner when GNU timeout is unavailable. Preserve the GNU timeout path and status 124 retry contract. Verify TERM-before-KILL cleanup, descendant exit, and release-plan ownership for the portable fallback. Closes #125295 Co-authored-by: Ayaan Zaidi <[email protected]>
1 parent 55b5317 commit f46f9b5

7 files changed

Lines changed: 241 additions & 16 deletions

‎scripts/lib/bounded-command.mjs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { runTsxCliShim } from "./tsx-cli-shim.mjs";
2+
3+
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,
7+
implementation: "./bounded-command.mts",
8+
});

‎scripts/lib/bounded-command.mts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { runManagedCommand } from "./managed-child-process.mjs";
2+
3+
const NODE_TIMEOUT_MAX_MS = 2 ** 31 - 1;
4+
5+
function usage(message?: string): never {
6+
if (message) {
7+
console.error(message);
8+
}
9+
console.error(
10+
"usage: node --import tsx scripts/lib/bounded-command.mts <timeout-ms> -- <command> [args...]",
11+
);
12+
process.exit(2);
13+
}
14+
15+
const [timeoutMsRaw, separator, command, ...args] = process.argv.slice(2);
16+
if (separator !== "--" || !command) {
17+
usage();
18+
}
19+
if (!timeoutMsRaw || !/^[1-9][0-9]*$/u.test(timeoutMsRaw)) {
20+
usage("timeout-ms must be a positive integer");
21+
}
22+
const timeoutMs = Number(timeoutMsRaw);
23+
if (!Number.isSafeInteger(timeoutMs)) {
24+
usage("timeout-ms must be a safe integer");
25+
}
26+
if (timeoutMs > NODE_TIMEOUT_MAX_MS) {
27+
usage(`timeout-ms must be at most ${NODE_TIMEOUT_MAX_MS}`);
28+
}
29+
30+
try {
31+
process.exitCode = await runManagedCommand({
32+
args,
33+
bin: command,
34+
requireProcessTreeExit: true,
35+
timeoutForceKillOnLeaderExit: true,
36+
timeoutKillGraceMs: 10_000,
37+
timeoutMs,
38+
});
39+
} catch (error) {
40+
if (error instanceof Error && "code" in error && error.code === "ETIMEDOUT") {
41+
process.exitCode = 124;
42+
} else {
43+
throw error;
44+
}
45+
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ type ManagedCommandOptions = {
5555

5656
type RunManagedCommandOptions = ManagedCommandOptions & {
5757
timeoutMs?: number;
58+
timeoutKillGraceMs?: number;
59+
timeoutForceKillOnLeaderExit?: boolean;
5860
requireProcessTreeExit?: boolean;
5961
runTaskkill?: TaskkillRunner;
6062
onReady?: (child: ChildProcess) => void;
@@ -278,6 +280,8 @@ export async function runManagedCommand({
278280
windowsVerbatimArguments,
279281
comSpec,
280282
timeoutMs,
283+
timeoutKillGraceMs,
284+
timeoutForceKillOnLeaderExit = false,
281285
requireProcessTreeExit = false,
282286
runTaskkill = spawnSync,
283287
onReady,
@@ -340,6 +344,7 @@ export async function runManagedCommand({
340344
outcome: ManagedCommandOutcome,
341345
stopSignal: NodeJS.Signals,
342346
forceKillDelayMs?: number,
347+
forceKillOnLeaderExit = false,
343348
) => {
344349
if (cancellation) {
345350
return cancellation;
@@ -349,6 +354,7 @@ export async function runManagedCommand({
349354
platform,
350355
runTaskkill,
351356
forceKillDelayMs,
357+
forceKillOnLeaderExit,
352358
});
353359
cancellation = finalization.then(
354360
() => outcome,
@@ -384,7 +390,7 @@ export async function runManagedCommand({
384390
});
385391
if (timeoutMs !== undefined) {
386392
timeoutTimer = setTimeout(() => {
387-
void stop({ type: "timeout" }, "SIGTERM");
393+
void stop({ type: "timeout" }, "SIGTERM", timeoutKillGraceMs, timeoutForceKillOnLeaderExit);
388394
}, timeoutMs);
389395
}
390396
signal?.addEventListener("abort", abort, { once: true });
@@ -454,10 +460,12 @@ async function finalizeManagedChild(
454460
platform,
455461
runTaskkill,
456462
forceKillDelayMs = FORCE_KILL_DELAY_MS,
463+
forceKillOnLeaderExit = false,
457464
}: {
458465
platform: NodeJS.Platform;
459466
runTaskkill: TaskkillRunner;
460467
forceKillDelayMs?: number;
468+
forceKillOnLeaderExit?: boolean;
461469
},
462470
) {
463471
// Nested wrappers own detached groups. Let them forward the signal before
@@ -500,7 +508,9 @@ async function finalizeManagedChild(
500508
return;
501509
}
502510
const now = Date.now();
503-
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))) {
504514
forced = true;
505515
if (groupState !== "dead") {
506516
terminateManagedChild(child, "SIGKILL", { platform, runTaskkill });

‎scripts/lib/plugin-clawhub-release.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ const OPENCLAW_PLUGIN_CLAWHUB_WORKFLOW_FILENAME = "plugin-clawhub-release.yml";
8282
const CLAWHUB_RELEASE_AUTHORITY_PATHS = [
8383
".github/workflows/plugin-clawhub-release.yml",
8484
".github/actions/setup-node-env",
85+
"scripts/lib/bounded-command.mjs",
86+
"scripts/lib/bounded-command.mts",
87+
"scripts/lib/managed-child-process.mts",
88+
"scripts/lib/tsx-cli-shim.mjs",
8589
"scripts/lib/bounded-response.mjs",
8690
"scripts/lib/plugin-npm-release.ts",
8791
"scripts/lib/plugin-clawhub-release.ts",

‎scripts/plugin-clawhub-publish.sh‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -261,16 +261,21 @@ for timeout_candidate in timeout gtimeout; do
261261
break
262262
fi
263263
done
264-
if [[ -z "${timeout_bin}" ]]; then
265-
echo "GNU timeout or gtimeout with --signal and --kill-after support is required for bounded ClawHub CLI calls." >&2
266-
exit 1
264+
if [[ -n "${timeout_bin}" ]]; then
265+
clawhub_timeout=(
266+
"${timeout_bin}"
267+
--signal=TERM
268+
--kill-after=10s
269+
"${clawhub_timeout_seconds}s"
270+
)
271+
else
272+
clawhub_timeout=(
273+
node
274+
"${repo_root}/scripts/lib/bounded-command.mjs"
275+
"$((clawhub_timeout_seconds * 1000))"
276+
--
277+
)
267278
fi
268-
clawhub_timeout=(
269-
"${timeout_bin}"
270-
--signal=TERM
271-
--kill-after=10s
272-
"${clawhub_timeout_seconds}s"
273-
)
274279

275280
validate_packed_publish() {
276281
local dry_run_json

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,10 @@ describe("resolveSelectedClawHubPublishablePluginPackages", () => {
426426
it.each([
427427
"packages/normalization-core/src/record-coerce.ts",
428428
"packages/plugin-package-contract/src/schema.ts",
429+
"scripts/lib/bounded-command.mjs",
430+
"scripts/lib/bounded-command.mts",
431+
"scripts/lib/managed-child-process.mts",
432+
"scripts/lib/tsx-cli-shim.mjs",
429433
"scripts/lib/plugin-publication-candidates.ts",
430434
"scripts/lib/plugin-publication-collector.ts",
431435
])("selects all publishable plugins when %s changes", (changedPath) => {
@@ -1698,14 +1702,20 @@ describe("plugin-clawhub-publish.sh", () => {
16981702
expect(localIdentityIndex).toBeLessThan(clawHubDryRunIndex);
16991703
});
17001704

1701-
it("probes GNU timeout capabilities and leaves pack-only mode portable", () => {
1705+
it("prefers GNU timeout and keeps a portable bounded fallback", () => {
17021706
const source = readFileSync("scripts/plugin-clawhub-publish.sh", "utf8");
17031707
const packExitIndex = source.indexOf('if [[ "${mode}" == "--pack" ]]');
17041708
const timeoutProbeIndex = source.indexOf("for timeout_candidate in timeout gtimeout");
17051709

17061710
expect(timeoutProbeIndex).toBeGreaterThan(packExitIndex);
17071711
expect(source).toContain("--signal=TERM --kill-after=1s 1s true");
1708-
expect(source).toContain("with --signal and --kill-after support is required");
1712+
expect(source).toContain('"${repo_root}/scripts/lib/bounded-command.mjs"');
1713+
expect(readFileSync("scripts/lib/bounded-command.mts", "utf8")).toContain(
1714+
"timeoutKillGraceMs: 10_000",
1715+
);
1716+
expect(readFileSync("scripts/lib/bounded-command.mjs", "utf8")).toContain(
1717+
"forceKillDelayMs: 15_000",
1718+
);
17091719
});
17101720

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

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

Lines changed: 146 additions & 3 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);
@@ -679,13 +738,14 @@ ${publish(2)}
679738
const childPath = path.join(dir, "child.mjs");
680739
const childPidPath = path.join(dir, "child.pid");
681740
const descendantPidPath = path.join(dir, "descendant.pid");
741+
const signalPath = path.join(dir, "signal.txt");
682742
fs.writeFileSync(
683743
childPath,
684744
`
685745
import { spawn } from "node:child_process";
686746
import fs from "node:fs";
687747
688-
process.on("SIGTERM", () => {});
748+
process.on("SIGTERM", () => fs.writeFileSync(process.argv[4], "SIGTERM\\n"));
689749
setInterval(() => {}, 1_000);
690750
spawn(process.execPath, [
691751
"-e",
@@ -706,9 +766,10 @@ ${publishReadyPidScript(2)}
706766
expect(
707767
runManagedCommand({
708768
bin: process.execPath,
709-
args: [childPath, childPidPath, descendantPidPath],
769+
args: [childPath, childPidPath, descendantPidPath, signalPath],
710770
shell: false,
711771
stdio: "ignore",
772+
timeoutKillGraceMs: 100,
712773
timeoutMs: 500,
713774
}),
714775
).rejects.toMatchObject({ code: "ETIMEDOUT" }),
@@ -723,6 +784,7 @@ ${publishReadyPidScript(2)}
723784
expect(isProcessAlive(descendantPid)).toBe(true);
724785
await releaseAndWait();
725786
if (process.platform !== "win32") {
787+
expect(fs.readFileSync(signalPath, "utf8")).toBe("SIGTERM\n");
726788
expect(killSpy).toHaveBeenCalledWith(-childPid, "SIGKILL");
727789
}
728790
expect(isProcessAlive(childPid)).toBe(false);
@@ -747,6 +809,87 @@ ${publishReadyPidScript(2)}
747809
}
748810
});
749811

812+
posixIt("lets a timed-out command handle SIGTERM before forced cleanup", async () => {
813+
const dir = createTempDir("openclaw-managed-timeout-grace-");
814+
const childPath = path.join(dir, "child.mjs");
815+
const signalPath = path.join(dir, "signal.txt");
816+
fs.writeFileSync(
817+
childPath,
818+
`
819+
import fs from "node:fs";
820+
process.on("SIGTERM", () => {
821+
fs.writeFileSync(process.argv[2], "SIGTERM\\n");
822+
process.exit(0);
823+
});
824+
setInterval(() => {}, 1_000);
825+
`,
826+
"utf8",
827+
);
828+
829+
await expect(
830+
runManagedCommand({
831+
args: [childPath, signalPath],
832+
bin: process.execPath,
833+
shell: false,
834+
stdio: "ignore",
835+
timeoutKillGraceMs: 10_000,
836+
timeoutMs: 500,
837+
}),
838+
).rejects.toMatchObject({ code: "ETIMEDOUT" });
839+
expect(fs.readFileSync(signalPath, "utf8")).toBe("SIGTERM\n");
840+
});
841+
842+
posixIt("force-kills descendants after the timed-out leader exits during grace", async () => {
843+
const dir = createTempDir("openclaw-managed-timeout-leader-exit-");
844+
const childPath = path.join(dir, "child.mjs");
845+
const descendantPidPath = path.join(dir, "descendant.pid");
846+
const signalPath = path.join(dir, "signal.txt");
847+
fs.writeFileSync(
848+
childPath,
849+
`
850+
import { spawn } from "node:child_process";
851+
import fs from "node:fs";
852+
853+
const descendant = spawn(process.execPath, [
854+
"-e",
855+
"process.on('SIGTERM', () => {}); setInterval(() => {}, 1_000);",
856+
], { stdio: "ignore" });
857+
fs.writeFileSync(process.argv[2], String(descendant.pid));
858+
process.on("SIGTERM", () => {
859+
fs.writeFileSync(process.argv[3], "SIGTERM\\n");
860+
process.exit(0);
861+
});
862+
setInterval(() => {}, 1_000);
863+
`,
864+
"utf8",
865+
);
866+
867+
let descendantPid = 0;
868+
try {
869+
const startedAt = Date.now();
870+
await expect(
871+
runManagedCommand({
872+
args: [childPath, descendantPidPath, signalPath],
873+
bin: process.execPath,
874+
shell: false,
875+
stdio: "ignore",
876+
timeoutForceKillOnLeaderExit: true,
877+
timeoutKillGraceMs: 10_000,
878+
timeoutMs: 500,
879+
}),
880+
).rejects.toMatchObject({ code: "ETIMEDOUT" });
881+
882+
descendantPid = Number(fs.readFileSync(descendantPidPath, "utf8"));
883+
expect(fs.readFileSync(signalPath, "utf8")).toBe("SIGTERM\n");
884+
expect(isProcessAlive(descendantPid)).toBe(false);
885+
expect(Date.now() - startedAt).toBeLessThan(3_000);
886+
} finally {
887+
if (descendantPid && isProcessAlive(descendantPid)) {
888+
process.kill(descendantPid, "SIGKILL");
889+
}
890+
}
891+
});
892+
750893
it("uses a wall timeout even while the child emits progress", async () => {
751894
const startedAt = Date.now();
752895
await expect(

0 commit comments

Comments
 (0)