@@ -466,19 +466,32 @@ export class CodeQLCliServer implements Disposable {
466466 void this . logger . log ( `${ description } using CodeQL CLI: ${ argsString } ...` ) ;
467467 }
468468
469- const process = spawnChildProcess ( codeqlPath , args ) ;
469+ const abortController = new AbortController ( ) ;
470+
471+ const process = spawnChildProcess ( codeqlPath , args , {
472+ signal : abortController . signal ,
473+ } ) ;
470474 if ( ! process || ! process . pid ) {
471475 throw new Error (
472476 `Failed to start ${ description } using command ${ codeqlPath } ${ argsString } .` ,
473477 ) ;
474478 }
475479
476- let cancellationRegistration : Disposable | undefined = undefined ;
477- try {
478- cancellationRegistration = token ?. onCancellationRequested ( ( _e ) => {
479- tk ( process . pid || 0 ) ;
480- } ) ;
480+ // We need to ensure that we're not killing the same process twice (since this may kill
481+ // another process with the same PID), so keep track of whether we've already exited.
482+ let exited = false ;
483+ process . on ( "exit" , ( ) => {
484+ exited = true ;
485+ } ) ;
481486
487+ const cancellationRegistration = token ?. onCancellationRequested ( ( _e ) => {
488+ abortController . abort ( "Token was cancelled." ) ;
489+ if ( process . pid && ! exited ) {
490+ tk ( process . pid ) ;
491+ }
492+ } ) ;
493+
494+ try {
482495 return await this . handleProcessOutput ( process , {
483496 handleNullTerminator : false ,
484497 description,
@@ -501,7 +514,9 @@ export class CodeQLCliServer implements Disposable {
501514 throw e ;
502515 } finally {
503516 process . stdin . end ( ) ;
504- process . kill ( ) ;
517+ if ( ! exited ) {
518+ tk ( process . pid ) ;
519+ }
505520 process . stdout . destroy ( ) ;
506521 process . stderr . destroy ( ) ;
507522
0 commit comments