@@ -73,6 +73,8 @@ type BoundedStringLog = string[] & {
7373 truncated ?: boolean ;
7474} ;
7575
76+ type OpenClawTestChildProcess = Pick < ChildProcessWithoutNullStreams , "kill" | "pid" > ;
77+
7678function createBoundedStringLog ( ) : string [ ] {
7779 const log = [ ] as BoundedStringLog ;
7880 log . byteLength = 0 ;
@@ -152,6 +154,7 @@ async function prepareGatewayEntrypoint(cwd: string): Promise<string[]> {
152154 cwd,
153155 env : { ...process . env , VITEST : "1" } ,
154156 stdio : [ "ignore" , "pipe" , "pipe" ] ,
157+ detached : shouldUseOpenClawTestProcessGroup ( ) ,
155158 } ) ;
156159 child . stdout ?. setEncoding ( "utf8" ) ;
157160 child . stderr ?. setEncoding ( "utf8" ) ;
@@ -167,7 +170,7 @@ async function prepareGatewayEntrypoint(cwd: string): Promise<string[]> {
167170 ] ) ;
168171
169172 if ( completed === null ) {
170- child . kill ( "SIGKILL" ) ;
173+ signalOpenClawTestProcess ( child , "SIGKILL" ) ;
171174 throw new Error ( `timeout preparing gateway entrypoint\n${ formatLogs ( stdout , stderr ) } ` ) ;
172175 }
173176 if ( completed . code !== 0 ) {
@@ -401,6 +404,7 @@ export async function createOpenClawTestInstance(
401404 cwd,
402405 env,
403406 stdio : [ "ignore" , "pipe" , "pipe" ] ,
407+ detached : shouldUseOpenClawTestProcessGroup ( ) ,
404408 } ,
405409 ) ;
406410
@@ -428,7 +432,7 @@ export async function createOpenClawTestInstance(
428432 }
429433 if ( ! hasChildExited ( child ) && ! child . killed ) {
430434 try {
431- child . kill ( "SIGTERM" ) ;
435+ signalOpenClawTestProcess ( child , "SIGTERM" ) ;
432436 } catch {
433437 // ignore
434438 }
@@ -439,7 +443,7 @@ export async function createOpenClawTestInstance(
439443 ) ;
440444 if ( ! exited && ! hasChildExited ( child ) && ! child . killed ) {
441445 try {
442- child . kill ( "SIGKILL" ) ;
446+ signalOpenClawTestProcess ( child , "SIGKILL" ) ;
443447 } catch {
444448 // ignore
445449 }
@@ -479,6 +483,7 @@ async function runCommand(params: {
479483 cwd : params . cwd ,
480484 env : params . env ,
481485 stdio : [ "ignore" , "pipe" , "pipe" ] ,
486+ detached : shouldUseOpenClawTestProcessGroup ( ) ,
482487 } ) ;
483488 child . stdout ?. setEncoding ( "utf8" ) ;
484489 child . stderr ?. setEncoding ( "utf8" ) ;
@@ -493,7 +498,7 @@ async function runCommand(params: {
493498 sleep ( params . timeoutMs ) . then ( ( ) => null ) ,
494499 ] ) ;
495500 if ( completed === null ) {
496- child . kill ( "SIGKILL" ) ;
501+ signalOpenClawTestProcess ( child , "SIGKILL" ) ;
497502 await waitForGatewayExit ( child , GATEWAY_STOP_TIMEOUT_MS ) ;
498503 throw new Error (
499504 `command timed out after ${ params . timeoutMs } ms: ${ params . args . join ( " " ) } \n${ formatLogs ( stdout , stderr ) } ` ,
@@ -506,10 +511,31 @@ async function runCommand(params: {
506511 } ;
507512}
508513
514+ function shouldUseOpenClawTestProcessGroup ( ) : boolean {
515+ return process . platform !== "win32" ;
516+ }
517+
518+ function signalOpenClawTestProcess (
519+ child : OpenClawTestChildProcess ,
520+ signal : NodeJS . Signals ,
521+ killProcess : ( pid : number , signal : NodeJS . Signals ) => boolean = process . kill ,
522+ ) : void {
523+ if ( shouldUseOpenClawTestProcessGroup ( ) && typeof child . pid === "number" ) {
524+ try {
525+ killProcess ( - child . pid , signal ) ;
526+ return ;
527+ } catch {
528+ // Fall back to the direct child if the process group already exited.
529+ }
530+ }
531+ child . kill ( signal ) ;
532+ }
533+
509534export const testing = {
510535 appendLogChunk,
511536 createBoundedStringLog,
512537 formatLogs,
513538 hasChildExited,
539+ signalOpenClawTestProcess,
514540 waitForPortOpen,
515541} ;
0 commit comments