event-loopAnswer last reviewed July 2026

Your script has finished all its work, but the process just sits there and never exits. What actually keeps a Node process alive, and how does it decide it's done?

Treats process liveness as bookkeeping over refed handles, and can name the API that shows what's holding a hung process open

What an AI-prepared candidate might say

So a Node process stays alive as long as there's still pending work registered with the event loop. That means active timers, open sockets or servers, file operations that are in progress, basically anything the loop is tracking. When the loop has nothing left to do, the process exits on its own. If it's hanging, something is still registered somewhere. The common causes are a setInterval that never got cleared, an open database connection, a server that's still listening, or a stream that was never closed. The usual fix is just cleaning up when you're done. Call clearInterval, close your connections, call server.close(). You can also call unref() on a timer or a socket so it stops keeping the process alive. And if you need to force it, process.exit() ends the process immediately, plus there are the beforeExit and exit events where you can run cleanup code as it shuts down.

Senior
Locked

The refed-handle accounting that actually decides exit, why promises don't count toward it, and the exit-code-13 case hiding behind that.

Unlock the depth
Staff
Locked

A shutdown sequence that drains without hanging, a debugging workflow built on `getActiveResourcesInfo`, and the SIGTERM-in-Kubernetes coordination most services get wrong.

Unlock the depth
Follow-up chain
Your script has finished all its work, but the process just sits there and never exits. What actually keeps a Node process alive, and how does it decide it's done? | NodeBook