Node.js traps · Chapter 4 of 16

A listener leak

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: The event loop
  • 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.

  1. Task 1

    repro.js: pile up listeners on purpose

    Write reproduce() in repro.js. Make the booking service's emitter with new EventEmitter() from node:events, and a feed with createSeatFeed(seatEvents) from seat-feed.js. Then, 15 times, one after another: make a page with createClient() from live-client.js, subscribe it to show 'T0023' with feed.subscribe, and close it with its close(). Resolve to seatEvents.listenerCount('seat-sold').

    The check runs your reproduce() twice: beside the original seat-feed.js it must resolve to 15 (every closed page left its listener behind), and beside a fixed seat-feed.js to 0. Beside the original, Node also prints a MaxListenersExceededWarning: that is part of the bug. No real waiting: reproduce() must finish within 2 seconds.

  2. Task 2

    seat-feed.js: forget a page once it is closed

    Fix subscribe(showId, client) in seat-feed.js. An open page must get the line seat <seat> sold for every 'seat-sold' event of its own show, and nothing for other shows. When the page emits 'close', its listener must be removed from seatEvents (only that page's), and the page is never written to again. Once every page has closed, seatEvents has no 'seat-sold' listeners left.

    Keep the export, the event name and the line as they are. The check uses its own emitter, shows and pages: it counts listeners as pages close, sells several seats to a page that stays open (one update only is not enough), and opens and closes 50 pages one after another, expecting no listeners left and no MaxListenersExceededWarning. Turning the warning off does not pass.

  3. Task 3

    seat-feed.test.js: a test that would have caught it

    Write a vitest test file, seat-feed.test.js, that would have caught the leak before the opening week. The check runs your file three times: beside the original seat-feed.js, where at least one test must fail; beside the fixed seat-feed.js, where every test must pass; and beside a wrong fix that turns the warning off with setMaxListeners(0) and still never removes listeners, where at least one test must fail.

    So open pages with createClient() from live-client.js and check both sides: an open page gets the updates for its own show, and after the pages close, seatEvents.listenerCount('seat-sold') is 0 and the closed pages had nothing written after close (writesAfterClose() is 0). Each test must finish within 3 seconds.

What you will learn

  • repro.js: pile up listeners on purpose
  • seat-feed.js: forget a page once it is closed
  • seat-feed.test.js: a test that would have caught it

More in The event loop

Build "A listener leak" 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.