0
0
Node.jsframework~20 mins

Graceful shutdown handling 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 the server receives SIGINT?

Consider this Node.js server code snippet that listens for the SIGINT signal to perform a graceful shutdown. What will be the output when you press Ctrl+C in the terminal?

Node.js
import http from 'http';

const server = http.createServer((req, res) => {
  res.end('Hello World');
});

server.listen(3000, () => {
  console.log('Server running on port 3000');
});

process.on('SIGINT', () => {
  console.log('SIGINT received, closing server...');
  server.close(() => {
    console.log('Server closed');
    process.exit(0);
  });
});
AServer running on port 3000\nServer closed\nSIGINT received, closing server...
BServer running on port 3000\nSIGINT received, closing server...\n(process hangs, no further output)
CServer running on port 3000\nSIGINT received, closing server...\nServer closed
DServer running on port 3000\n(process exits immediately without logs)
Attempts:
2 left
💡 Hint

Think about the order of console logs and when server.close() callback runs.

📝 Syntax
intermediate
2:00remaining
Which option correctly handles server close with async/await?

You want to convert the server close logic to use async/await with a promise. Which code snippet correctly wraps server.close() in a promise?

Node.js
import http from 'http';

const server = http.createServer((req, res) => {
  res.end('Hi');
});

async function shutdown() {
  // Fill in the correct code here
}

process.on('SIGTERM', async () => {
  console.log('SIGTERM received');
  await shutdown();
  console.log('Shutdown complete');
  process.exit(0);
});
Aawait new Promise((resolve, reject) => server.close(err => err ? reject(err) : resolve()));
Bawait server.close();
Cawait new Promise(resolve => server.close(resolve));
Dawait new Promise((resolve, reject) => server.close(() => reject()));
Attempts:
2 left
💡 Hint

Remember server.close() uses a callback with an error argument.

🔧 Debug
advanced
2:00remaining
Why does the server not close on SIGINT?

Given this code, why does the server not close when pressing Ctrl+C?

Node.js
import http from 'http';

const server = http.createServer((req, res) => {
  res.end('Hello');
});

server.listen(4000);

process.on('SIGINT', () => {
  console.log('SIGINT received');
  server.close();
  console.log('Server closed');
  process.exit(0);
});
ABecause the SIGINT event is not properly registered.
BBecause <code>server.close()</code> is asynchronous and the process exits before it finishes closing.
CBecause <code>server.close()</code> requires a callback to close the server.
DBecause the server never started listening.
Attempts:
2 left
💡 Hint

Think about what happens when process.exit(0) runs immediately after server.close().

🧠 Conceptual
advanced
1:30remaining
What is the main benefit of graceful shutdown in Node.js servers?

Why is implementing graceful shutdown important for Node.js servers?

AIt improves server startup speed by caching modules.
BIt automatically restarts the server after it crashes.
CIt disables all incoming connections immediately to save resources.
DIt allows the server to finish handling ongoing requests before closing, preventing data loss or errors.
Attempts:
2 left
💡 Hint

Think about what happens to active connections when the server closes abruptly.

state_output
expert
2:30remaining
What is the output order when multiple shutdown signals are received?

Consider this code snippet. What will be the console output if the process receives SIGINT, then SIGTERM shortly after?

Node.js
import http from 'http';

const server = http.createServer((req, res) => {
  res.end('OK');
});

server.listen(5000);

let shuttingDown = false;

async function shutdown(signal) {
  if (shuttingDown) {
    console.log(`Already shutting down on ${signal}`);
    return;
  }
  shuttingDown = true;
  console.log(`Shutdown started by ${signal}`);
  await new Promise(resolve => server.close(resolve));
  console.log('Server closed');
  process.exit(0);
}

process.on('SIGINT', () => shutdown('SIGINT'));
process.on('SIGTERM', () => shutdown('SIGTERM'));
AShutdown started by SIGINT\nAlready shutting down on SIGTERM\nServer closed
BShutdown started by SIGTERM\nAlready shutting down on SIGINT\nServer closed
CShutdown started by SIGINT\nServer closed
DShutdown started by SIGINT\nServer closed\nShutdown started by SIGTERM
Attempts:
2 left
💡 Hint

Consider the shuttingDown flag and the order signals are received.