0
0
Node.jsframework~20 mins

Graceful shutdown on errors in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Graceful Shutdown Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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
});
AThe server logs the error message and shuts down immediately without closing connections.
BThe server logs the error message but continues running and accepts new requests.
CThe server crashes without logging anything.
DThe server logs the error and gracefully closes existing connections before exiting.
Attempts:
2 left
💡 Hint
Think about what happens if you catch an uncaughtException but do not close the server.
state_output
intermediate
2: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);
  });
});
AThe server stops accepting new connections but keeps existing connections open until they finish.
BThe server immediately terminates all connections and exits.
CThe server continues accepting new connections during server.close().
DThe server ignores server.close() and keeps running.
Attempts:
2 left
💡 Hint
Remember what server.close() does in Node.js.
🔧 Debug
advanced
2: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()
});
AThe error is swallowed silently and does not trigger the handler.
BThe server.close() callback never runs because the server is not listening.
CUnhandledRejection event does not catch promise rejections inside request handlers.
DThe process does not exit because process.exit() is not called after server.close().
Attempts:
2 left
💡 Hint
Think about what happens after server.close() finishes.
📝 Syntax
advanced
2: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.
A
process.on('SIGINT', () => {
  server.close(() => {
    console.log('Server closed');
    process.exit(0);
  });
});
B
process.on('SIGINT', () => {
  console.log('Server closed');
  server.close(() => process.exit(0));
});
C
process.on('SIGINT', () => {
  server.close();
  console.log('Server closed');
  process.exit(0);
});
D
process.on('SIGINT', () => {
  process.exit(0);
  server.close(() => console.log('Server closed'));
});
Attempts:
2 left
💡 Hint
Remember server.close() is asynchronous and process.exit() should be called after it finishes.
🧠 Conceptual
expert
2: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.
AUncaught exceptions are always harmless and can be ignored safely.
BThe server will automatically restart, so no action is needed.
CThe server may be in an inconsistent state, leading to unpredictable behavior or security issues.
DNode.js prevents any further requests after an uncaught exception automatically.
Attempts:
2 left
💡 Hint
Think about what happens to program state after an unexpected error.