Challenge - 5 Problems
Graceful Shutdown Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when an uncaught exception occurs in this Node.js server?
Consider this Node.js server code snippet. What will be the output behavior when an uncaught exception is thrown inside the request handler?
Node.js
import http from 'http'; const server = http.createServer((req, res) => { if (req.url === '/error') { throw new Error('Unexpected error'); } res.end('Hello World'); }); server.listen(3000); process.on('uncaughtException', (err) => { console.error('Caught exception:', err.message); // No server.close() called here });
Attempts:
2 left
💡 Hint
Think about what happens if you catch an uncaughtException but do not close the server.
✗ Incorrect
The 'uncaughtException' handler logs the error but since server.close() is not called, the server keeps running and accepts new requests. This is risky because the server state may be unstable.
❓ state_output
intermediate2:00remaining
What is the state of the server after calling server.close() inside an error handler?
Given this code snippet, what is the state of the server after an error triggers the 'uncaughtException' handler that calls server.close()?
Node.js
import http from 'http'; const server = http.createServer((req, res) => { if (req.url === '/fail') { throw new Error('Fail'); } res.end('OK'); }); server.listen(3000); process.on('uncaughtException', (err) => { console.error('Error:', err.message); server.close(() => { console.log('Server closed'); process.exit(1); }); });
Attempts:
2 left
💡 Hint
Remember what server.close() does in Node.js.
✗ Incorrect
Calling server.close() stops the server from accepting new connections but allows existing connections to complete before the callback runs.
🔧 Debug
advanced2:00remaining
Why does this graceful shutdown code not exit the process?
This code aims to gracefully shut down the server on an unhandled rejection but the process never exits. What is the cause?
Node.js
import http from 'http'; const server = http.createServer((req, res) => { if (req.url === '/reject') { Promise.reject(new Error('Rejected promise')); } res.end('OK'); }); server.listen(3000); process.on('unhandledRejection', (reason) => { console.error('Unhandled rejection:', reason.message); server.close(() => { console.log('Server closed'); }); // Missing process.exit() });
Attempts:
2 left
💡 Hint
Think about what happens after server.close() finishes.
✗ Incorrect
Without calling process.exit(), the Node.js process keeps running even after the server closes, because there may be other open handles or timers.
📝 Syntax
advanced2:00remaining
Which option correctly handles graceful shutdown on SIGINT signal?
Choose the code snippet that correctly listens for SIGINT and gracefully shuts down the server before exiting.
Attempts:
2 left
💡 Hint
Remember server.close() is asynchronous and process.exit() should be called after it finishes.
✗ Incorrect
Option A waits for server.close() to finish before calling process.exit(0), ensuring graceful shutdown. Other options call process.exit() too early or log before closing.
🧠 Conceptual
expert2:00remaining
Why is it risky to continue running a Node.js server after catching an uncaught exception without shutting down?
Select the best explanation for why continuing to run a Node.js server after an uncaught exception without shutting down can cause problems.
Attempts:
2 left
💡 Hint
Think about what happens to program state after an unexpected error.
✗ Incorrect
Uncaught exceptions can leave the server in an unstable state. Continuing to run risks corrupted data, memory leaks, or security vulnerabilities. It's best to shut down and restart cleanly.