Browse documentation

Idle drains run intervals while another timer is pending, with HTML's nesting clamp

Date: 2026-12-04 Area: runtime, testing Supersedes in part: ADR 0113's "Intervals are excluded from the idle drains entirely"; ADR 0113's other decisions stand. Related: ADR 0214, Testing API, #2599

Context

Under real timers the virtual clock moves only in the drains: the engine's idle point (after the entry module, which under GocciaTestRunner is the test file's load phase), a test body's end, and the waits in between. ADR 0113 left intervals out of every drain except the one that runs while the tests run, because an uncleared interval is never exhausted and would spend the whole 10000-timer budget.

So a setInterval a test file started while it loaded did not fire until the tests ran, while its one-shot timers all ran during loading. web-platform-tests' async tests start their timers while the file loads, and three subtests in html/webappapis/timers failed on it (missing-timeout-setinterval.any.js twice, setinterval-settimeout-clamping.any.js once); a fourth, cleartimeout-clearinterval.any.js's "Clear interval with clearTimeout", passed even when clearTimeout did nothing, and was left out.

The HTML Standard runs an interval's ticks in due order with the timeouts ("timer initialization steps", "run steps after a timeout"). It also clamps a delay below 4ms to 4ms once the task that starts the timer is nested more than 5 timers deep, an interval's own ticks included, so a zero-period interval or a zero-delay chain lets later timers run. The queue did not clamp: a zero-period interval re-armed at the instant it ran, so the clock could not move past it.

Decision

  • Every real-timer drain runs the intervals in due order with the other timers while a timer that is not an interval is pending. Once only intervals are left, the drain ends there, so an uncleared interval still cannot keep it going. The drain that runs while the tests run keeps running intervals until the report is done, as before. The drain after the report runs no intervals, as before: no test is left to take an exception of theirs.
  • Under real timers the queue applies HTML's nesting clamp to setTimeout and setInterval: a timer started from a timer's task nested more than 5 deep waits at least 4ms, so an interval started outside a timer runs six ticks at once and each one after that 4ms after the last. A timer's nesting level ends when its callback returns: HTML's microtask checkpoint runs each microtask as the running task, so a microtask the callback queued, like a task that is not a timer's (a FinalizationRegistry cleanup, a fetch settling), has nesting level 0. The host's own timers, AbortSignal.timeout's, run DOM's "run steps after a timeout", which has no nesting level: they are not clamped and their tasks are not timer tasks. Fake timers keep Vitest's semantics and do not clamp.
  • An exception an interval's callback throws during a drain is a timer callback's exception (ADR 0214): the test runner attributes it to the unit that scheduled the interval, and elsewhere it fails the run.

Consequences

  • A test file's load phase behaves as a browser loading the same script would, up to the point where only intervals are left. The three known failures for #2599 in tests/web-platform-tests/helpers/expectations.js are removed, and "Clear interval with clearTimeout" runs again: it fails when clearTimeout does nothing.
  • A test body's interval runs with the body's timeouts at the end of the test, where it used to be skipped there.
  • A zero-delay chain deeper than 5 timers now moves the virtual clock by 4ms a step, so a timeout due a few milliseconds later can run inside the chain, as it does in a browser.