Timers and handles
Node’s event loop stays alive while handles exist: timers, open sockets, fs watchers, etc.
Broken code
Section titled “Broken code”examples/node/04-timer-handle-leak.js — nested intervals without cleanup.
Fixed code
Section titled “Fixed code”examples/node/04-timer-handle-fixed.js — store interval id, clearInterval on shutdown.
Graceful shutdown pattern
Section titled “Graceful shutdown pattern”const timers = [];
function track(id) { timers.push(id); return id;}
function shutdown() { for (const id of timers) clearInterval(id); server.close();}
process.on('SIGTERM', shutdown);Run it
Section titled “Run it”node examples/node/04-timer-handle-leak.jsnode examples/node/04-timer-handle-fixed.js