// the find
mcollina/close-with-grace
Exit your process, gracefully (if possible) - for Node.js
A small utility from Matteo Collina (Fastify core team) that registers handlers for every signal and error event Node.js can emit, then calls your async cleanup function before exiting. If the cleanup hangs past a configurable timeout, it kills the process anyway. Aimed at Fastify users but works with anything.
Uses process.once so a second signal during shutdown triggers an immediate hard exit — avoiding the common trap of a graceful-shutdown handler that itself gets stuck. The skip option lets you opt out of specific events when you already handle them, which is the right escape hatch rather than all-or-nothing. Callback and Promise interfaces are both supported so it fits old and new codebases. Test coverage is thorough for a 300-line utility — individual test files for timeout, second-signal, second-error, uninstall, and callback paths.
Handles SIGILL, SIGBUS, SIGFPE, and SIGSEGV — hardware and OS-level fault signals that indicate memory corruption or illegal instructions. Trying to run async cleanup after these is not reliable; the process state is undefined and you may never reach your close handler. Listing them alongside SIGTERM gives false confidence. The delay default of 10 seconds is documented but there is no way to see the current timeout in flight or cancel the forced exit if your close function resolves just before the timer fires. No way to register multiple independent handlers for different subsystems — you get one callback, so callers must compose their own cleanup sequence inside it.