@@ -14,13 +14,23 @@ import packageJson from "../package.json";
1414
1515const jobs : Job [ ] = [ ] ;
1616
17- process . on ( "SIGINT" , async ( _ : string , code : number ) => {
18- await cleanupJobResources ( jobs ) ;
19- process . exit ( code ) ;
20- } ) ;
17+ let cleanupAndExitPromise : Promise < void > | null = null ;
18+
19+ async function cleanupAndExit ( code : number ) {
20+ // First caller's exit code wins — subsequent callers join the in-flight cleanup.
21+ if ( cleanupAndExitPromise ) return cleanupAndExitPromise ;
22+ cleanupAndExitPromise = cleanupJobResources ( jobs ) . finally ( ( ) => process . exit ( code ) ) ;
23+ return cleanupAndExitPromise ;
24+ }
25+
26+ process . on ( "SIGINT" , ( ) => cleanupAndExit ( 130 ) ) ;
27+ process . on ( "SIGTERM" , ( ) => cleanupAndExit ( 143 ) ) ;
28+ process . on ( "SIGHUP" , ( ) => cleanupAndExit ( 129 ) ) ;
2129
2230// Graceful shutdown for nodemon
23- process . on ( "SIGUSR2" , async ( ) => await cleanupJobResources ( jobs ) ) ;
31+ process . on ( "SIGUSR2" , async ( ) => {
32+ if ( ! cleanupAndExitPromise ) await cleanupJobResources ( jobs ) ;
33+ } ) ;
2434
2535( ( ) => {
2636 const yparser = yargs ( process . argv . slice ( 2 ) ) ;
@@ -42,8 +52,7 @@ process.on("SIGUSR2", async () => await cleanupJobResources(jobs));
4252 } else {
4353 process . stderr . write ( chalk `{red ${ e . stack ?? e } }\n` ) ;
4454 }
45- await cleanupJobResources ( jobs ) ;
46- process . exit ( 1 ) ;
55+ await cleanupAndExit ( 1 ) ;
4756 }
4857 } ,
4958 builder : ( y : any ) => {
0 commit comments