Node.js traps · Chapter 14 of 16

A timer that keeps the process alive

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.

  1. Task 1

    repro.js: catch the timer holding on, on purpose

    Write reproduce() in repro.js. Count the 'Timeout' entries in process.getActiveResourcesInfo() (the things that keep this process alive), start metrics with startMetrics from metrics.js (a flush that does nothing, everyMs 60,000), count one event, count the 'Timeout' entries again, call stop(), and resolve to the second count minus the first.

    The check runs your reproduce() twice: beside the original metrics.js it must resolve to 1 (one interval holds the process open), and beside a fixed metrics.js to 0. Count before and after, because other timers may already be there. No real waiting: call stop() before you resolve, and reproduce() must finish within 2 seconds.

  2. Task 2

    metrics.js: metrics that never hold the process open

    Fix startMetrics({ flush, everyMs }) in metrics.js, so its timer never keeps the process alive, while it still flushes for as long as something else (the API's server) keeps the process running. Every everyMs it calls flush with the counts since the last flush, as { name: number }, and skips the flush when nothing was counted; after stop() it never flushes again. A script that only starts metrics, counts and prints must exit by itself.

    Keep the export, count, stop and what flush receives as they are. The check moves fake time on for several minutes and expects every flush, counts the 'Timeout' entries of process.getActiveResourcesInfo() before and after startMetrics with real timers (no new one), and runs a small script in a separate node that must end within 8 seconds.

  3. Task 3

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

    Write a vitest test file, metrics.test.js, that would have caught the hanging report. The check runs your file three times: beside the original metrics.js, where at least one test must fail; beside the fixed metrics.js, where every test must pass; and beside a wrong fix with a one-time timer that flushes once and never again, where at least one test must fail.

    So check both sides. One test, with real timers: count the 'Timeout' entries of process.getActiveResourcesInfo() before and after startMetrics, expect no new one, then call stop(). Another, with fake timers: count events, move time on by everyMs, count more, move on again, and expect two flushes with the right counts. Each test must finish within 3 seconds.

What you will learn

  • repro.js: catch the timer holding on, on purpose
  • metrics.js: metrics that never hold the process open
  • metrics.test.js: a test that would have caught it

More in Modules

Build "A timer that keeps the process alive" 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.