JavaScript traps · Chapter 15 of 16

Memory leaks from closures

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

  • Part: Time
  • 3 tasks
  • JavaScript
  • In the all labs plan

The first 2 chapters of JavaScript traps are free. This one comes with the all labs plan.

Why you need this

A flag that says "closed" makes a page look closed: it stops sending, and every test of what the page receives passes. The listener and everything it holds are still there. Node's only hint is this warning, printed once, when an emitter gets its eleventh listener for one event (trimmed):

for (let screen = 1; screen <= 11; screen += 1) {
  menu.on('price', (item) => {});
}
(node:6423) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 price listeners added to [EventEmitter]. MaxListeners is 10. Use emitter.setMaxListeners() to increase limit

setMaxListeners raises the limit and hides the warning. It does not remove a single listener.

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: count the listeners nobody removed

    Write reproduce() in repro.js. Make a bus with new EventEmitter() (imported from node:events), then 100 times open a tracking page with openTracking(bus, orderId, send) from tracking.js, with any order id and a send that does nothing, and close it straight away with the close() it returns. Resolve to bus.listenerCount('status'), how many 'status' listeners the bus still has.

    The check runs your reproduce() twice: beside the original tracking.js it must resolve to 100 (every closed page is still on the bus), and beside a fixed tracking.js to 0. So it must really open and close 100 pages. Count, do not measure memory. Node prints a MaxListenersExceededWarning when you run it: that is the ticket's warning. reproduce() must finish within 2 seconds.

  2. Task 2

    tracking.js: a closed page lets go

    Fix openTracking(bus, orderId, send) in tracking.js so close() really stops the tracking: after close(), the page's own listener is off the bus and the page gets no more updates. Other pages keep getting their own updates, even a second page for the same order, and any other 'status' listener on the bus (the kitchen screen's) stays where it is. Calling close() twice is harmless. An open page still gets send(status) for its own order only.

    So after 100 pages are opened and closed, the bus has as many 'status' listeners as before they were opened. The check counts listeners with listenerCount and uses its own bus and orders. A fix that removes every listener on the bus, or only hides the warning with setMaxListeners, is as wrong as the bug.

  3. Task 3

    tracking.test.js: a test that would have caught it

    Write a vitest test file, tracking.test.js, that would have caught this leak before it reached the server. The check runs your file three times: beside the original tracking.js, where at least one test must fail; beside the fixed tracking.js, where every test must pass; and beside a wrong fix whose close() removes every 'status' listener on the bus, where at least one test must fail.

    So open two pages on one bus and close one: the bus must have one 'status' listener left, the closed page must get no update, and the open page must still get its own when you emit one. Each test must finish within 3 seconds.

What you will learn

  • repro.js: count the listeners nobody removed
  • tracking.js: a closed page lets go
  • tracking.test.js: a test that would have caught it

More in Time

Build "Memory leaks from closures" yourself

Tapri runs on JavaScript, and Kabir, its senior developer, keeps assigning you tickets. Each is a real 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 JavaScript traps are free. This one comes with the all labs plan.