0
0
Node.jsframework~10 mins

Graceful shutdown on errors in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Graceful shutdown on errors
Start Node.js app
Listen for errors
Error occurs?
NoContinue running
Yes
Log error
Close server connections
Cleanup resources
Exit process
The app runs and listens for errors. When an error happens, it logs it, closes connections, cleans up, then exits safely.
Execution Sample
Node.js
const server = app.listen(3000);
process.on('uncaughtException', (err) => {
  console.error('Error:', err);
  server.close(() => process.exit(1));
});
This code listens for uncaught errors, logs them, closes the server, then exits the app.
Execution Table
StepEventActionServer StateProcess State
1App startServer starts listeningOpenRunning
2No errorApp runs normallyOpenRunning
3Uncaught error occursError caught by handlerOpenRunning
4Error loggedConsole outputs error messageOpenRunning
5Server closingserver.close() calledClosingRunning
6Server closedCallback triggers process.exit(1)ClosedExiting
7Process exitApp stopsClosedStopped
💡 Process exits after server closes to ensure no new requests are accepted.
Variable Tracker
VariableStartAfter ErrorAfter server.close()Final
serverListeningListeningClosingClosed
process.stateRunningRunningExitingStopped
Key Moments - 3 Insights
Why do we call server.close() before process.exit()?
Calling server.close() stops new connections and finishes existing ones before exiting, preventing abrupt termination. See execution_table steps 5 and 6.
What happens if we call process.exit() immediately on error?
The app stops instantly, possibly dropping active requests or leaving resources open. The table shows graceful shutdown waits for server to close first.
How does the error handler catch uncaught exceptions?
The process.on('uncaughtException') listens globally for errors not caught elsewhere, shown in execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the server state at step 5?
AClosed
BOpen
CClosing
DStopped
💡 Hint
Check the 'Server State' column at step 5 in the execution_table.
At which step does the process start exiting?
AStep 4
BStep 6
CStep 5
DStep 7
💡 Hint
Look for when process.exit(1) is called in the 'Action' column.
If server.close() was not called, what would happen to the process state after error?
AProcess would exit immediately
BServer would close automatically
CProcess would keep running
DError would not be logged
💡 Hint
Refer to key_moments about calling process.exit() immediately without server.close().
Concept Snapshot
Graceful shutdown on errors:
- Listen for uncaught errors with process.on('uncaughtException')
- Log the error for debugging
- Call server.close() to stop new requests and finish ongoing ones
- After server closes, call process.exit() to stop the app
- This prevents abrupt termination and resource leaks
Full Transcript
This visual execution shows how a Node.js app handles errors gracefully. The app starts and listens for requests. If an uncaught error happens, the error handler logs it, then calls server.close() to stop accepting new connections and finish current ones. Once the server closes, the app calls process.exit() to stop running. This sequence avoids sudden crashes and cleans up resources properly. Variables like server state and process state change step by step, showing the shutdown flow clearly.