0
0
Node.jsframework~10 mins

Graceful shutdown handling in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Graceful shutdown handling
Start Server
Server Running
Receive Shutdown Signal
Stop Accepting New Requests
Finish Ongoing Requests
Close Server & Cleanup
Exit Process
The server starts and runs normally until it receives a shutdown signal, then it stops new requests, finishes ongoing ones, cleans up, and exits.
Execution Sample
Node.js
const http = require('http');
const app = (req, res) => {
  res.end('Hello World');
};
const server = http.createServer(app);
server.listen(3000);

process.on('SIGINT', () => {
  server.close(() => process.exit(0));
});
This code starts a server and listens for a shutdown signal to close the server gracefully before exiting.
Execution Table
StepEventServer StateAction TakenProcess State
1Start serverListening on port 3000Server starts accepting requestsRunning
2Server runningListening on port 3000Handles incoming requests normallyRunning
3Receive SIGINT (Ctrl+C)Listening on port 3000Stop accepting new requestsRunning
4Finish ongoing requestsClosing serverWaits for all requests to finishRunning
5Server closedClosedCleanup doneReady to exit
6Process exitN/AProcess exits with code 0Exited
💡 Process exits after server closes and cleanup completes
Variable Tracker
VariableStartAfter SIGINTAfter Close CallbackFinal
server.listeningtruefalsefalsefalse
process.runningtruetruetruefalse
Key Moments - 2 Insights
Why does the server stop accepting new requests after receiving SIGINT?
Because in step 3 of the execution_table, the server.close() method is called, which stops new connections but allows ongoing requests to finish.
What happens if we call process.exit() immediately without waiting for server.close()?
The server might terminate abruptly, dropping ongoing requests. The execution_table shows the importance of waiting for the close callback before exiting (step 4 and 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the server state right after receiving SIGINT?
AClosing server
BListening on port 3000
CClosed
DNot started
💡 Hint
Check row 3 under 'Server State' in the execution_table
At which step does the process exit in the execution_table?
AStep 6
BStep 4
CStep 5
DStep 3
💡 Hint
Look at the 'Process State' column for the exit event
If the server never finishes ongoing requests, what will happen to the process exit?
AProcess exits immediately
BProcess exits with error
CProcess waits indefinitely
DProcess restarts automatically
💡 Hint
Refer to step 4 where server waits for ongoing requests before closing
Concept Snapshot
Graceful shutdown in Node.js:
- Listen for shutdown signals (e.g., SIGINT)
- Call server.close() to stop new requests
- Wait for ongoing requests to finish
- Cleanup resources
- Exit process after server closes
This prevents dropping active connections.
Full Transcript
This visual execution shows how a Node.js server handles graceful shutdown. The server starts and listens for requests. When it receives a shutdown signal like SIGINT, it stops accepting new requests but keeps handling ongoing ones. Once all requests finish, the server closes and cleans up resources. Finally, the process exits cleanly. This prevents abrupt termination and dropped connections.