99 BUILD_STAMP_FILE ,
1010 RUNTIME_POSTBUILD_STAMP_FILE ,
1111} from "../../scripts/lib/local-build-metadata-paths.mjs" ;
12+ import { terminateManagedChild } from "../../scripts/lib/managed-child-process.mjs" ;
1213import {
1314 createOpenClawTestState ,
1415 type OpenClawTestState ,
@@ -87,6 +88,9 @@ type OpenClawTestProcessReadiness = Pick<OpenClawTestProcess, "exitCode" | "sign
8788 once : ( event : "exit" , listener : ( ) => void ) => unknown ;
8889 off : ( event : "exit" , listener : ( ) => void ) => unknown ;
8990} ;
91+ type GatewayProcessStopOptions = NonNullable < Parameters < typeof terminateManagedChild > [ 2 ] > & {
92+ forceWindowsTree ?: boolean ;
93+ } ;
9094
9195function createBoundedStringLog ( ) : string [ ] {
9296 const log = [ ] as BoundedStringLog ;
@@ -335,30 +339,56 @@ async function stopGatewayProcess(
335339 child : OpenClawTestProcess ,
336340 deadline : number ,
337341 stopTimeoutMs : number ,
342+ options : GatewayProcessStopOptions = { } ,
338343) : Promise < boolean > {
339- const waitForClose = ( deadlineShare : number ) =>
344+ const platform = options . platform ?? process . platform ;
345+ const waitForClose = ( remainingSteps : number ) =>
340346 waitForGatewayClose (
341347 child ,
342- Math . min ( stopTimeoutMs , Math . max ( 0 , Math . floor ( ( deadline - Date . now ( ) ) / deadlineShare ) ) ) ,
348+ Math . min (
349+ stopTimeoutMs ,
350+ Math . max ( 0 , Math . floor ( ( deadline - Date . now ( ) ) / Math . max ( 1 , remainingSteps ) ) ) ,
351+ ) ,
352+ ) ;
353+ const terminate = ( signal : NodeJS . Signals ) => {
354+ terminateManagedChild (
355+ child ,
356+ signal ,
357+ options . runTaskkill
358+ ? { platform, runTaskkill : options . runTaskkill }
359+ : {
360+ platform,
361+ } ,
343362 ) ;
344- // Reserve enough of the shared deadline for TERM and KILL after natural stdio drain.
345- if ( hasChildExited ( child ) && ( await waitForClose ( 3 ) ) ) {
363+ } ;
364+ const forceWindowsTree = options . forceWindowsTree === true && platform === "win32" ;
365+ const signals = forceWindowsTree ? ( [ "SIGKILL" ] as const ) : ( [ "SIGTERM" , "SIGKILL" ] as const ) ;
366+
367+ if ( hasGatewayProcessClosed ( child ) ) {
368+ return true ;
369+ }
370+ // An exited leader can leave inherited stdio open in descendants. Let it
371+ // settle briefly, then terminate the owned tree before releasing the slot.
372+ if ( ! forceWindowsTree && hasChildExited ( child ) && ( await waitForClose ( signals . length + 1 ) ) ) {
346373 return true ;
347374 }
348- for ( const signal of [ "SIGTERM" , "SIGKILL" ] as const ) {
375+ for ( const [ index , signal ] of signals . entries ( ) ) {
349376 if ( hasGatewayProcessClosed ( child ) ) {
350377 return true ;
351378 }
379+ if ( Date . now ( ) >= deadline ) {
380+ break ;
381+ }
352382 try {
353- signalOpenClawTestProcess ( child , signal ) ;
383+ terminate ( signal ) ;
354384 } catch {
355385 // ignore
356386 }
357- if ( await waitForClose ( signal === "SIGTERM" ? 2 : 1 ) ) {
387+ if ( await waitForClose ( signals . length - index ) ) {
358388 return true ;
359389 }
360390 }
361- return false ;
391+ return hasGatewayProcessClosed ( child ) ;
362392}
363393
364394function hasChildExited ( child : Pick < OpenClawTestProcess , "exitCode" | "signalCode" > ) {
@@ -471,8 +501,9 @@ export async function createOpenClawTestInstance(
471501 const releaseGatewayChild = async (
472502 target : OpenClawTestProcess ,
473503 deadline : number ,
504+ options : GatewayProcessStopOptions = { } ,
474505 ) : Promise < boolean > => {
475- const closed = await stopGatewayProcess ( target , deadline , stopTimeoutMs ) ;
506+ const closed = await stopGatewayProcess ( target , deadline , stopTimeoutMs , options ) ;
476507 if ( closed && child === target ) {
477508 child = undefined ;
478509 }
@@ -506,7 +537,7 @@ export async function createOpenClawTestInstance(
506537 } ) ;
507538 } ,
508539 startGateway : async ( ) => {
509- if ( child && ! hasGatewayProcessClosed ( child ) ) {
540+ if ( child && ! hasChildExited ( child ) ) {
510541 return ;
511542 }
512543 const entrypoint = await resolveGatewayEntrypoint ( cwd ) ;
@@ -523,6 +554,18 @@ export async function createOpenClawTestInstance(
523554 const deadline = Date . now ( ) + ( options . startTimeoutMs ?? GATEWAY_START_TIMEOUT_MS ) ;
524555 let restarts = 0 ;
525556
557+ if ( child ) {
558+ const staleChild = child ;
559+ const closed = await releaseGatewayChild ( staleChild , deadline , {
560+ forceWindowsTree : true ,
561+ } ) ;
562+ if ( ! closed ) {
563+ throw new Error (
564+ `gateway process did not close before restart deadline\n${ formatLogs ( stdout , stderr ) } ` ,
565+ ) ;
566+ }
567+ }
568+
526569 while ( true ) {
527570 const remainingMs = deadline - Date . now ( ) ;
528571 if ( remainingMs <= 0 ) {
@@ -537,19 +580,24 @@ export async function createOpenClawTestInstance(
537580 await waitForGatewayReady ( attempt , stdout , stderr , port , remainingMs ) ;
538581 return ;
539582 } catch ( err ) {
540- const closed = await releaseGatewayChild ( attempt , deadline ) ;
583+ const exitCode = attempt . exitCode ;
584+ const signalCode = attempt . signalCode ;
585+ const closed = await releaseGatewayChild ( attempt , deadline , {
586+ forceWindowsTree : true ,
587+ } ) ;
541588 const shouldRestart =
542- closed &&
543589 restarts < GATEWAY_MIGRATION_CONVERGENCE_MAX_RESTARTS &&
544590 isGatewayMigrationConvergenceRefusal (
545- attempt . exitCode ,
546- attempt . signalCode ,
591+ exitCode ,
592+ signalCode ,
547593 readLogBuffer ( attemptStderr ) ,
548594 ) ;
549595 if ( shouldRestart && Date . now ( ) < deadline ) {
550- restarts += 1 ;
551- appendLogChunk ( stderr , GATEWAY_MIGRATION_CONVERGENCE_RESTART_MARKER ) ;
552- continue ;
596+ if ( closed ) {
597+ restarts += 1 ;
598+ appendLogChunk ( stderr , GATEWAY_MIGRATION_CONVERGENCE_RESTART_MARKER ) ;
599+ continue ;
600+ }
553601 }
554602 throw err ;
555603 }
@@ -651,5 +699,6 @@ export const testing = {
651699 hasChildExited,
652700 isGatewayMigrationConvergenceRefusal,
653701 signalOpenClawTestProcess,
702+ stopGatewayProcess,
654703 waitForGatewayReady,
655704} ;
0 commit comments