#33261·bun

event loop: never re-enter from inside a dispatch callback (de-block expect().resolves and HTMLRewriter.transform, then assert)

Author: aliiCreated Jul 2, 2026Updated Sep 28, 2026

Invariant

The event loop must never be re-entered from inside a poll-dispatch callback.

waitForPromise ("run the event loop until this promise settles") is legitimate only where a synchronous outer context is inherent — the module pipeline and top-level drivers, which run when no dispatch is in flight:

  • entry-point module-graph evaluation (bun file.ts): ES-module evaluation is async by spec, so the synchronous entry must pump until it settles
  • macro loading/evaluation inside the transpiler
  • the REPL / CLI drivers

Every other caller is a synchronous API signature sitting on an asynchronous operation, reachable from inside I/O completion callbacks. Re-entering the loop there is what makes bugs like the one root-caused in #32233 possible: a nested epoll_wait/kevent overwrites the shared ready_polls batch mid-iteration, and any not-yet-dispatched one-shot entries (subprocess pipe readers, process watchers) are lost forever — the owner hangs.

#32233 contained a correct, minimal, well-tested fix that made the dispatcher tolerate re-entry. It was closed deliberately (see the closing comment: https://github.com/oven-sh/bun/pull/32233#issuecomment-4867373017) because tolerating the state entrenches it: the right end-state is that the state cannot occur, and CI screams if someone reintroduces it.

What #32233 established (all of this carries over)

  • Plain concurrent Bun.$ never hangs by itself: 0/20 (Linux) and 0/8 (macOS) on unfixed builds. The loss requires a waitForPromise caller in a continuation of an I/O completion.
  • The nested-tick stack was captured live (gdb, loop->data.tick_depth == 2), with HTMLRewriter.transform (async handler) as a pure-runtime trigger: 20/20 hangs unfixed → 0/20 with the (closed) dispatcher fix.
  • require() is not a trigger: an ordinary graph never pumps the loop, and a top-level-await graph is refused with ERR_REQUIRE_ASYNC_MODULE.
  • Three deterministic regression tests exist in that PR's branch (test/js/bun/spawn/pipe-reader-nested-tick.test.ts): two trigger via bun:test matchers, one via HTMLRewriter; victims cover both Bun.$ and Bun.spawn pipes.

Caller inventory (from waitForPromise / tickWhilePaused call sites)

caller verdict
entry-point / module-graph evaluation (VirtualMachine) keep (top-level by construction)
macro load + eval (transpiler) keep (module pipeline)
REPL / CLI drivers keep (top-level)
bun:test expect().resolves / .rejects / async toThrow remove (work item 1)
HTMLRewriter.transform with async handlers remove (work item 2)
bake render paths, Bun.build plugin waits audit (work item 3)

(Bun.spawnSync / execSync are not waitForPromise callers — they block on their own wait loop and never re-enter the JS event loop.)

Work items

  • 1. bun:test matchers stop blocking the event loop. expect(p).resolves / .rejects / async toThrow return real promises (Jest semantics). To keep existing un-awaited usage from becoming silently vacuous, bun:test tracks matcher promises created during a test and settles them at test end (failing the test if any rejected).
  • 2. HTMLRewriter.transform() with async handlers returns a promise. The synchronous return with async handlers is deprecated (it can only be implemented by pumping the loop).
  • 3. Audit in-tree callers reachable from I/O callbacks (bake render paths, Bun.build plugin waits) and convert them to fully-async or restrict them to top-level contexts.
  • 4. Debug-build assertion at tick entry. loop->current_ready_poll < loop->num_ready_polls at us_loop_run_bun_tick entry means "a callback synchronously waited on the event loop mid-dispatch". Assert (or loud warn + counter) in debug builds so a violation fails CI instead of shipping as a rare hang; no-op in release. This is the enforcement for the invariant above, and it is the same condition #32233's fix keyed on.
  • 5. Repurpose #32233's regression tests. After (1) and (2): the matcher-triggered tests become "these APIs no longer re-enter the loop"; the HTMLRewriter test becomes the assertion's regression test. Until (1)/(2) land they will hang on current builds by design — that is the accepted, known cost of not landing the dispatcher fix.

Notes

  • Until items 1–2 ship, the hang in #32233's test file remains reproducible on releases (expect().resolves in a continuation + concurrent subprocesses). Workaround: await your expect().resolves / .rejects chains, and don't call HTMLRewriter.transform with async handlers from inside I/O callbacks.
  • If this direction ever proves untenable (e.g. a waitForPromise caller that cannot be de-blocked and can run mid-dispatch), #32233's drain is the known-good, reviewed, tested fallback and can be revived as-is.