Node.js traps · Chapter 15 of 16
server.close() waits for open connections
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Modules
- 3 tasks
- Node.js
- In the all labs plan
The first 2 chapters of Node.js traps are free. This one comes with the all labs plan.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
repro.js: make the shutdown hang on purpose
Write reproduce() in repro.js. Make a server with createLiveServer() from live-server.js, listen on port 0 on 127.0.0.1 and wait for its 'listening' event (once from node:events). Open two live seat maps with http.get from node:http, on the paths '/live/T0023' and '/live/T0038', and wait until each one has sent its first event (the response's first 'data'). Keep both open, then call shutdown() and resolve to what it resolves to.
The check runs your reproduce() twice, each time in its own node process: beside the original live-server.js it must still be waiting when the check stops it after 3 seconds (the open streams keep close() waiting), and beside a fixed live-server.js it must resolve to 'closed'. No deadline or setTimeout of your own: the hang is the result. Before your fix, node repro.js hangs; press Ctrl+C to stop it.
- Task 2
live-server.js: a shutdown that finishes
Fix shutdown() in live-server.js, so the deploy's shutdown finishes while seat maps are open. Every live stream still open gets a last event, the text event: bye, a new line, data: reconnect and a blank line (so the page reconnects to the new server), and then ends; then the server closes and shutdown() resolves to 'closed'. After shutdown, new connections are refused. A stream whose page closed earlier needs nothing more.
Keep createLiveServer() returning { server, shutdown }, the routes (/live/<showtime> opens a stream that starts with the hello event, /health answers ok), and the log lines live: <showtime> stream opened and live: shutting down. The check opens its own streams on localhost (one closed by its page before the deploy) and gives shutdown() 2 seconds.
- Task 3
live-server.test.js: a test that would have caught it
Write a vitest test file, live-server.test.js, that would have caught the hanging deploys before the live seat map shipped. The check runs your file three times: beside the original live-server.js, where at least one test must fail; beside the fixed live-server.js, where every test must pass; and beside a wrong fix that closes only the idle connections before close(), where at least one test must fail.
So open two live streams on a server listening on port 0, race shutdown() against a deadline of 500 ms (setTimeout from node:timers/promises), and check both sides: shutdown gave 'closed' before the deadline, and each stream's text ended with the bye event. A failing test must not leave the server running: close every connection at the end with server.closeAllConnections(). Each test must finish within 3 seconds.
What you will learn
- repro.js: make the shutdown hang on purpose
- live-server.js: a shutdown that finishes
- live-server.test.js: a test that would have caught it
More in Modules
Build "server.close() waits for open connections" yourself
Parda's booking backend has run on Node for a year, and Nikhil keeps handing you incidents where Node did exactly what the team told it. Each is a runtime trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Node.js traps are free. This one comes with the all labs plan.