SIGINT and SIGTERM handled for graceful shutdown
Why it matters
MCP servers running as child processes receive SIGINT when the user presses Ctrl+C and SIGTERM when the parent process shuts down. Without signal handlers, the Node.js process exits immediately (SIGINT default), potentially mid-write on an in-progress JSON-RPC response. The client receives a truncated or empty message and marks the server as crashed. CWE-404 (improper resource shutdown) covers this: open database connections, file handles, and HTTP keep-alives are abandoned rather than closed, which can leave the remote systems in inconsistent state.
Severity rationale
Medium because abrupt termination corrupts in-progress responses and leaks resources, but the immediate impact is limited to the session in progress at shutdown time.
Remediation
Register SIGINT and SIGTERM handlers that call server.close() before exiting. The SDK's close() drains in-flight messages and closes the transport cleanly.
// src/index.ts
async function shutdown() {
console.error('Shutting down...')
await server.close() // closes transport, drains in-flight messages
process.exit(0)
}
process.on('SIGINT', shutdown)
process.on('SIGTERM', shutdown)
const transport = new StdioServerTransport()
await server.connect(transport)
If your server holds database connections or open file handles, close them inside shutdown() before calling process.exit(0).
DetectionPro
The detection method for this pattern is part of AuditBuffet Pro.
The full method (what to look for, pass and fail conditions, and how results get reported) ships inside the MCP Server Audit. The pattern definition, severity rationale, and remediation remain openly licensed under CC-BY-4.0.
External references
- cwe · CWE-404 — Improper Resource Shutdown or Release
- iso-25010:2011 · reliability
Taxons
History
- 2026-04-18·v1.0.0·Initial import from mcp-server·automated