{"success":true,"name":"wire-node-js-graceful-shutdown-handlers-correctly","description":"Writing shutdown/cleanup code for a Node.js server.","category":"ai-brimind","path":"ai-brimind/wire-node-js-graceful-shutdown-handlers-correctly/SKILL.md","file":"SKILL.md","content":"---\nname: wire-node-js-graceful-shutdown-handlers-correctly\ndescription: \"Writing shutdown/cleanup code for a Node.js server.\"\n---\n\n# Wire Node.js Graceful Shutdown Handlers Correctly\n\n## When to use\nProject is a Node.js server, and you are adding SIGTERM/SIGINT handling or cleanup for DB connections, timers, or in-flight requests.\n\n## Example request\nprocess.exit(0) directly in the SIGTERM handler → in-flight requests are cut mid-response; close the server first, exit after — this is connection draining. server.close() called but process never exits → open sockets, DB pools, or intervals keep the loop alive; close each one — every handle needs a shutdown owner. No timeout on shutdown → one stuck request blocks the deploy forever; force-exit after a deadline. Cleanup code duplicated in SIGTERM and SIGINT → one shutdown function, both signals call it.\n\n## Instructions\nAct as a Node.js developer writing shutdown as a precise sequence, not a single exit call. The correct order: (1) on SIGTERM/SIGINT, flip a shutting-down flag and stop accepting new work — server.close() stops new connections while letting in-flight requests finish; (2) close every other open handle that keeps the event loop alive: database pools, Redis clients, queues, clearInterval on timers; (3) exit with code 0 only after those complete; (4) arm a hard deadline (e.g. 10–15s) that force-exits nonzero if draining hangs, because one stuck request must not block a deploy forever. Write ONE shutdown function and register it for both signals — duplicated cleanup drifts. Never call process.exit() as the first line of a handler; that's the symptom-level shortcut that kills live requests. Test it: send SIGTERM during an active request and confirm the response completes and the process actually exits. Only ship once that test passes, and explain which handle each cleanup step releases.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #991\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}