@@ -51,6 +51,7 @@ export type TaskRunProcessOptions = {
5151 machineResources : MachinePresetResources ;
5252 isWarmStart ?: boolean ;
5353 cwd ?: string ;
54+ gracefulTerminationTimeoutInMs ?: number ;
5455} ;
5556
5657export type TaskRunProcessExecuteParams = {
@@ -114,7 +115,7 @@ export class TaskRunProcess {
114115 console . error ( "Error cancelling task run process" , { err } ) ;
115116 }
116117
117- await this . kill ( ) ;
118+ await this . #gracefullyTerminate ( this . options . gracefulTerminationTimeoutInMs ) ;
118119 }
119120
120121 async cleanup ( kill = true ) {
@@ -131,7 +132,7 @@ export class TaskRunProcess {
131132 }
132133
133134 if ( kill ) {
134- await this . kill ( "SIGKILL" ) ;
135+ await this . #gracefullyTerminate ( this . options . gracefulTerminationTimeoutInMs ) ;
135136 }
136137 }
137138
@@ -395,6 +396,18 @@ export class TaskRunProcess {
395396 this . _stderr . push ( errorLine ) ;
396397 }
397398
399+ async #gracefullyTerminate( timeoutInMs : number = 1_000 ) {
400+ logger . debug ( "gracefully terminating task run process" , { pid : this . pid , timeoutInMs } ) ;
401+
402+ await this . kill ( "SIGTERM" , timeoutInMs ) ;
403+
404+ if ( this . _child ?. connected ) {
405+ logger . debug ( "child process is still connected, sending SIGKILL" , { pid : this . pid } ) ;
406+
407+ await this . kill ( "SIGKILL" ) ;
408+ }
409+ }
410+
398411 /** This will never throw. */
399412 async kill ( signal ?: number | NodeJS . Signals , timeoutInMs ?: number ) {
400413 logger . debug ( `killing task run process` , {
@@ -420,7 +433,11 @@ export class TaskRunProcess {
420433 const [ error ] = await tryCatch ( killTimeout ) ;
421434
422435 if ( error ) {
423- logger . debug ( "kill: failed to wait for child process to exit" , { error } ) ;
436+ logger . debug ( "kill: failed to wait for child process to exit" , {
437+ timeoutInMs,
438+ signal,
439+ pid : this . pid ,
440+ } ) ;
424441 }
425442 }
426443
0 commit comments