0
0
ExpressDebug / FixBeginner · 4 min read

How to Handle Graceful Shutdown in Express

To handle graceful shutdown in Express, listen for termination signals like SIGINT or SIGTERM, then close the server with server.close() to stop accepting new requests and finish ongoing ones before exiting. This ensures your app shuts down cleanly without dropping connections.
🔍

Why This Happens

When you stop an Express server abruptly (like pressing Ctrl+C), it immediately kills the process without finishing current requests or cleaning up resources. This can cause errors, lost data, or corrupted connections.

javascript
import express from 'express';

const app = express();

app.get('/', (req, res) => {
  res.send('Hello World');
});

app.listen(3000);

// No shutdown handling here
Output
Server stops immediately on Ctrl+C, possibly dropping active requests.
🔧

The Fix

To fix this, save the server instance returned by app.listen(). Then listen for system signals like SIGINT (Ctrl+C) and SIGTERM. Inside the handler, call server.close() to stop accepting new requests and wait for ongoing requests to finish. After that, exit the process.

javascript
import express from 'express';

const app = express();

app.get('/', (req, res) => {
  res.send('Hello World');
});

const server = app.listen(3000, () => {
  console.log('Server running on port 3000');
});

const shutdown = () => {
  console.log('Received shutdown signal, closing server...');
  server.close(() => {
    console.log('Server closed, exiting process');
    process.exit(0);
  });
};

process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
Output
Server running on port 3000 Received shutdown signal, closing server... Server closed, exiting process
🛡️

Prevention

Always implement graceful shutdown in your Express apps to avoid dropped requests and resource leaks. Use server.close() and listen for termination signals. Also, handle cleanup of other resources like database connections inside the shutdown handler.

Use linting rules or code reviews to ensure shutdown logic is present. Testing shutdown behavior in development helps catch issues early.

⚠️

Related Errors

Common related errors include:

  • ECONNRESET: Happens when connections are dropped abruptly.
  • Unhandled Promise Rejections: If async cleanup fails during shutdown.
  • Timeouts: If server.close() is not awaited properly.

Fix these by properly awaiting cleanup, handling errors, and using graceful shutdown patterns.

Key Takeaways

Always listen for termination signals like SIGINT and SIGTERM to trigger graceful shutdown.
Use server.close() to stop accepting new requests and finish ongoing ones before exiting.
Clean up other resources like database connections inside the shutdown handler.
Test shutdown behavior to ensure no requests are dropped or resources leaked.
Implement graceful shutdown as a standard pattern in all Express apps.