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" ;
33import fs from "node:fs" ;
44import os from "node:os" ;
55import 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 `
685745import { spawn } from "node:child_process";
686746import fs from "node:fs";
687747
688- process.on("SIGTERM", () => {} );
748+ process.on("SIGTERM", () => fs.writeFileSync(process.argv[4], "SIGTERM\\n") );
689749setInterval(() => {}, 1_000);
690750spawn(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