Bird
Raised Fist0
Expressframework~20 mins

Graceful shutdown handling in Express - Practice Problems & Coding Challenges

Choose your learning style10 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 in this Express app?
Consider this Express server code that listens for SIGINT to shut down gracefully. What will be the console output if you press Ctrl+C to stop the server?
Express
const express = require('express');
const app = express();
const server = app.listen(3000, () => console.log('Server started'));

process.on('SIGINT', () => {
  console.log('SIGINT received');
  server.close(() => {
    console.log('Server closed');
    process.exit(0);
  });
});
ASIGINT received\nServer started\nServer closed
BServer started\nServer closed\nSIGINT received
CServer started\nSIGINT received
DServer started\nSIGINT received\nServer closed
Attempts:
2 left
💡 Hint
Think about the order of events when SIGINT is caught and the server closes.
📝 Syntax
intermediate
2:00remaining
Which option correctly attaches a listener for graceful shutdown in Express?
You want to listen for the SIGTERM signal to gracefully close your Express server. Which code snippet is syntactically correct and will work as intended?
Aprocess.on('SIGTERM', () => server.close(() => process.exit(0)));
Bprocess.addListener('SIGTERM', server.close(() => process.exit(0)));
Cprocess.on('SIGTERM', server.close(() => process.exit(0)));
Dprocess.on('SIGTERM', () => { server.close; process.exit(0); });
Attempts:
2 left
💡 Hint
Remember that the listener callback should be a function, not the result of calling a function.
🔧 Debug
advanced
2:00remaining
Why does this Express server not exit on SIGINT?
This Express server code listens for SIGINT but never exits. What is the cause?
Express
const express = require('express');
const app = express();
const server = app.listen(3000);

process.on('SIGINT', () => {
  console.log('SIGINT caught');
  server.close();
  console.log('Server closed');
});
Aconsole.log blocks the event loop preventing exit
BSIGINT event is not emitted on Ctrl+C
Cserver.close() is asynchronous; process.exit() is missing so process stays alive
Dapp.listen() must be called inside SIGINT handler
Attempts:
2 left
💡 Hint
Think about what happens after server.close() and how the process knows to exit.
❓ state_output
advanced
2:00remaining
What is the value of 'connections' after this code runs?
This Express server tracks open connections to close them on shutdown. What is the value of the 'connections' Set after the code below runs and a client connects then disconnects?
Express
const express = require('express');
const app = express();
const server = app.listen(3000);
const connections = new Set();

server.on('connection', (conn) => {
  connections.add(conn);
  conn.on('close', () => connections.delete(conn));
});

// Simulate client connect and disconnect
const net = require('net');
const client = net.createConnection({ port: 3000 }, () => {
  client.end();
});
Aundefined
BSet(0) {} (empty set)
CSet(1) { <Socket> } (one connection still present)
Dnull
Attempts:
2 left
💡 Hint
Think about when the 'close' event fires and what happens to the Set.
🧠 Conceptual
expert
2:00remaining
Why is it important to track open connections during graceful shutdown in Express?
In Express, when shutting down gracefully, why should you track and close open connections before exiting the process?
ATo prevent clients from hanging by ensuring all requests finish before exit
BTo speed up server startup on next launch
CTo automatically restart the server without manual intervention
DTo free up memory used by the Express app immediately
Attempts:
2 left
💡 Hint
Think about user experience and network connections during shutdown.

Practice

(1/5)
1. What is the main purpose of implementing graceful shutdown in an Express server?
easy
A. To allow the server to finish handling current requests before stopping
B. To restart the server automatically after a crash
C. To speed up server startup time
D. To block all incoming requests permanently

Solution

  1. Step 1: Understand graceful shutdown concept

    Graceful shutdown means the server stops accepting new requests but finishes processing ongoing ones.
  2. Step 2: Identify the purpose in Express context

    In Express, this prevents dropping requests abruptly when stopping the server.
  3. Final Answer:

    To allow the server to finish handling current requests before stopping -> Option A
  4. Quick Check:

    Graceful shutdown = finish requests before stop [OK]
Hint: Graceful shutdown means finishing requests before exit [OK]
Common Mistakes:
  • Thinking it restarts the server automatically
  • Confusing it with startup speed
  • Believing it blocks all requests permanently
2. Which of the following is the correct way to start listening for a shutdown signal in an Express app?
easy
A. express.shutdown('SIGINT', () => { /* shutdown code */ });
B. server.listen('SIGINT', () => { /* shutdown code */ });
C. app.shutdown('SIGINT', () => { /* shutdown code */ });
D. process.on('SIGINT', () => { /* shutdown code */ });

Solution

  1. Step 1: Identify how Node.js listens for signals

    Node.js uses process.on('signal', callback) to listen for OS signals like SIGINT.
  2. Step 2: Confirm Express does not have shutdown method

    Express app or server does not have shutdown methods; signal handling is done on process.
  3. Final Answer:

    process.on('SIGINT', () => { /* shutdown code */ }); -> Option D
  4. Quick Check:

    Signal listening uses process.on [OK]
Hint: Use process.on to catch shutdown signals [OK]
Common Mistakes:
  • Using server.listen for signals
  • Calling shutdown on app or express object
  • Confusing signal handling with server methods
3. Consider this Express server code snippet:
const express = require('express');
const app = express();
const server = app.listen(3000);

process.on('SIGTERM', () => {
  server.close(() => {
    console.log('Server closed');
  });
});

console.log('Server running');

What will happen when the process receives a SIGTERM signal?
medium
A. The server stops accepting new requests and logs 'Server closed' after finishing current requests
B. The server immediately crashes without logging anything
C. The server ignores the SIGTERM signal and keeps running
D. The server restarts automatically

Solution

  1. Step 1: Analyze signal handler code

    On SIGTERM, server.close() is called with a callback that logs 'Server closed'.
  2. Step 2: Understand server.close behavior

    server.close stops new connections but waits for ongoing requests to finish before calling the callback.
  3. Final Answer:

    The server stops accepting new requests and logs 'Server closed' after finishing current requests -> Option A
  4. Quick Check:

    SIGTERM triggers server.close with log after finish [OK]
Hint: server.close waits for requests before callback [OK]
Common Mistakes:
  • Assuming immediate crash on SIGTERM
  • Thinking server restarts automatically
  • Believing server ignores SIGTERM
4. This code attempts graceful shutdown but has a bug:
const express = require('express');
const app = express();
const server = app.listen(3000);

process.on('SIGINT', () => {
  server.close();
  console.log('Server closed');
  process.exit(0);
});

What is the problem with this shutdown code?
medium
A. It should call app.close() instead of server.close()
B. It calls process.exit(0) immediately, not waiting for server.close to finish
C. It does not listen on the correct port
D. It uses SIGINT instead of SIGTERM, which is invalid

Solution

  1. Step 1: Understand server.close asynchronous behavior

    server.close is asynchronous and takes a callback to run after closing.
  2. Step 2: Identify premature exit call

    process.exit(0) is called immediately after server.close without waiting, so server may not close properly.
  3. Final Answer:

    It calls process.exit(0) immediately, not waiting for server.close to finish -> Option B
  4. Quick Check:

    Must wait for server.close callback before exit [OK]
Hint: Wait for server.close callback before exiting process [OK]
Common Mistakes:
  • Thinking SIGINT is invalid signal
  • Confusing app.close with server.close
  • Ignoring asynchronous nature of server.close
5. You want to implement graceful shutdown in an Express app that also uses a database connection. Which approach correctly ensures both server and database close properly before exiting?
hard
A. Call process.exit() immediately on SIGINT, then close server and database asynchronously
B. Close database connection first, then call server.close(), ignoring process signals
C. Listen for SIGINT, call server.close(), then close database in server.close callback, finally call process.exit()
D. Restart the server automatically on SIGINT without closing connections

Solution

  1. Step 1: Handle SIGINT signal properly

    Listen for SIGINT to start graceful shutdown process.
  2. Step 2: Close server and then database sequentially

    Call server.close() to finish requests, then in its callback close the database connection.
  3. Step 3: Exit process after all resources are closed

    Call process.exit() only after both server and database are closed to avoid abrupt termination.
  4. Final Answer:

    Listen for SIGINT, call server.close(), then close database in server.close callback, finally call process.exit() -> Option C
  5. Quick Check:

    Shutdown sequence: server.close -> db.close -> exit [OK]
Hint: Close server then DB before exiting on signal [OK]
Common Mistakes:
  • Exiting process before closing resources
  • Ignoring database closure
  • Restarting server instead of shutting down
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard