I have a Node app doing some asynchronous things, and I can't take the chance that one of the many modules does something bad and gets stuck, so the app will never exit. (It just happened when sending log messages to a service.)
I can't use process.exit
directly, because it will terminate no matter how many asynchronous operations are pending. Still I'd like to exit as early as possible, so this won't do:
function exit() {
setTimeout(function() {
process.exit(1);
}, 10000);
}
Because that will wait 10 seconds, even if everything went ok and all async events finished after 1 second.
I'm considering checking if the event loop is empty except for this timer, then exit. That could possibly be done through some undocumented process methods, but I prefer avoiding shady stuff like that. Any ideas about a better way to solve this?