|
1 | 1 | // 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"; |
3 | 3 | import fs from "node:fs"; |
4 | 4 | import os from "node:os"; |
5 | 5 | import path from "node:path"; |
@@ -254,6 +254,65 @@ ${publish(2)} |
254 | 254 | 20_000, |
255 | 255 | ); |
256 | 256 |
|
| 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 | + |
257 | 316 | it("maps forwarded signals to shell-compatible exit codes", () => { |
258 | 317 | expect(signalExitCode("SIGHUP")).toBe(129); |
259 | 318 | expect(signalExitCode("SIGINT")).toBe(130); |
@@ -807,20 +866,23 @@ setInterval(() => {}, 1_000); |
807 | 866 |
|
808 | 867 | let descendantPid = 0; |
809 | 868 | try { |
| 869 | + const startedAt = Date.now(); |
810 | 870 | await expect( |
811 | 871 | runManagedCommand({ |
812 | 872 | args: [childPath, descendantPidPath, signalPath], |
813 | 873 | bin: process.execPath, |
814 | 874 | shell: false, |
815 | 875 | stdio: "ignore", |
816 | | - timeoutKillGraceMs: 100, |
| 876 | + timeoutForceKillOnLeaderExit: true, |
| 877 | + timeoutKillGraceMs: 10_000, |
817 | 878 | timeoutMs: 500, |
818 | 879 | }), |
819 | 880 | ).rejects.toMatchObject({ code: "ETIMEDOUT" }); |
820 | 881 |
|
821 | 882 | descendantPid = Number(fs.readFileSync(descendantPidPath, "utf8")); |
822 | 883 | expect(fs.readFileSync(signalPath, "utf8")).toBe("SIGTERM\n"); |
823 | 884 | expect(isProcessAlive(descendantPid)).toBe(false); |
| 885 | + expect(Date.now() - startedAt).toBeLessThan(3_000); |
824 | 886 | } finally { |
825 | 887 | if (descendantPid && isProcessAlive(descendantPid)) { |
826 | 888 | process.kill(descendantPid, "SIGKILL"); |
|
0 commit comments