Skip to content

Timers and handles

Node’s event loop stays alive while handles exist: timers, open sockets, fs watchers, etc.

examples/node/04-timer-handle-leak.js — nested intervals without cleanup.

examples/node/04-timer-handle-fixed.js — store interval id, clearInterval on shutdown.

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);
Terminal window
node examples/node/04-timer-handle-leak.js
node examples/node/04-timer-handle-fixed.js