A typical sample code for handling SIGINT
ends with a process.exit
, like this:
const closeMyServer = async () => {
logger.info(`Closing myServer...`);
await myServer.close();
logger.info('myServer closed');
process.exit(0);
};
process.on('SIGINT', () => {
void closeMyServer();
});
Without the proces.exit
, the signal is suppressed, so, like, a CTRL-C
will no longer stop the binary. But calling process.exit
is very “greedy” — if there are, like, multiple process.on('SIGINT'
, only one of them will actually fully execute (whichever one gets to process.exit
first).
E.g. if I also have a deleteTempFiles
that I want to run when I get a CTRL-C
I could try to add:
process.on('SIGINT', () => {
void deleteTempFiles('SIGINT');
});
But if, say, .close
takes 30ms and the deleting all files takes 500ms, as soon as the .close
completes, the process is forcefully terminated with process.exit
and the deletion of the temp files gets halted.
Is there a proper way to do this, without requiring there to be a single process.on('SIGINT'
?