0
0
Expressframework~10 mins

Graceful shutdown handling in Express - 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
Exit Process
The server starts and runs normally. When a shutdown signal arrives, it stops new requests, finishes current ones, closes, and exits.
Execution Sample
Express
const server = app.listen(3000);
process.on('SIGINT', () => {
  server.close(() => {
    process.exit(0);
  });
});
This code starts an Express server and listens for a shutdown signal to close the server gracefully.
Execution Table
StepEventServer StateAction TakenResult
1Start serverListeningServer starts listening on port 3000Server ready for requests
2Server runningListeningHandling incoming requests normallyRequests processed
3Receive SIGINTListeningSignal received to shutdownPrepare to close server
4Stop accepting new requestsClosingserver.close() calledNo new requests accepted
5Finish ongoing requestsClosingWait for current requests to finishOngoing requests complete
6Close serverClosedCallback from server.close() triggeredServer fully closed
7Exit processClosedprocess.exit(0) calledNode process exits cleanly
💡 Process exits after server closes and all requests finish
Variable Tracker
VariableStartAfter SIGINTAfter server.close()Final
server.listeningtruetruefalsefalse
process.exitCalledfalsefalsefalsetrue
Key Moments - 2 Insights
Why does the server stop accepting new requests but still finish ongoing ones?
When server.close() is called (see step 4 in execution_table), the server stops new connections but keeps existing ones until they finish, ensuring no requests are cut off.
What happens if process.exit() is called before ongoing requests finish?
Calling process.exit() too early would terminate the server immediately, dropping active requests. The code waits for server.close() callback (step 6) before exiting.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the server stop accepting new requests?
AStep 4
BStep 3
CStep 5
DStep 6
💡 Hint
Check the 'Action Taken' column for when server.close() is called.
According to variable_tracker, what is the value of server.listening after server.close()?
Atrue
Bfalse
Cundefined
Dnull
💡 Hint
Look at the 'After server.close()' column for server.listening.
If process.exit(0) was called immediately after receiving SIGINT (step 3), what would happen?
AServer would close gracefully
BServer would keep accepting requests
COngoing requests might be dropped
DNothing would change
💡 Hint
Refer to key_moments about process.exit() timing.
Concept Snapshot
Graceful shutdown in Express:
- Listen for shutdown signals (e.g., SIGINT)
- Call server.close() to stop new requests
- Wait for ongoing requests to finish
- Exit process after server closes
This prevents dropping active requests during shutdown.
Full Transcript
This visual execution shows how an Express server handles graceful shutdown. The server starts and listens for requests. When a shutdown signal like SIGINT is received, the server stops accepting new requests by calling server.close(). It waits for ongoing requests to finish before closing completely. Finally, the process exits cleanly. Variables like server.listening change from true to false after closing. This ensures no requests are dropped during shutdown.