Skip to content

process: keep a listener that is removed during an emit until the emit returns - #47121

Open
robobun wants to merge 1 commit into
robobun/16f79adc/native-emitter-once-reentrant-emitfrom
robobun/16f79adc/native-emitter-keep-removed-listener
Open

robobun wants to merge 1 commit into
robobun/16f79adc/native-emitter-once-reentrant-emitfrom
robobun/16f79adc/native-emitter-keep-removed-listener

Conversation

@robobun

@robobun robobun commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

Fix

Background

Downsides

  • An emit runs 7 more instructions, on() + removeAllListeners(type) 37 more. No path allocates more.
  • Each native emitter grows by 32 bytes. .text grows by 1,280 bytes.
  • A listener that is removed during an emit stays in memory until that emit returns.
Notes

Repro, with no forced collection. The first listener removes the second one and then only allocates.

const rounds = 200;
let lost = 0;
for (let r = 0; r < rounds; r++) {
  const type = "nat-" + r;
  let called = false;
  process.on(type, () => {
    process.removeAllListeners(type);
    let junk = [];
    for (let i = 0; i < 200000; i++) junk.push({ i, s: "x" + i });
    junk = null;
  });
  (() => {
    process.on(type, () => {
      called = true;
    });
  })();
  process.emit(type);
  if (!called) lost++;
}
console.log(lost, "of", rounds, "emits did not call the removed listener");
node v26.3.0:      0 of 200
main (9b8347a24e): 129, 135 and 147 of 200 (three runs)
this branch:       0 of 200 (three runs)

The three ways to remove. removeListener(type, listener), removeAllListeners(type) and removeAllListeners() each put the registration on the list. The new tests have one block for each of them, with an on() listener and a once() listener. In each test the first listener removes the third one, the second listener collects (Bun.gc(true)), and the third one must run. One more test does this on each object that uses the native emitter (the list is in #47120).

The once removal of the emit loop itself does not use the list. The loop holds the function for the call, and the fired bit of #47120 stops a second call.

Release. An emit that returns has dropped its copy. It then removes the list entries that have one reference left, the list's own. An entry that an outer emit still holds stays. These tests hold that:

  • A listener that the outer emit removed still runs after a nested emit returned and a collection ran.
  • Inside one long emit, the listeners that 200 nested emits removed are collected before the long emit returns (heapStats, fewer than 200 of 400 functions are left).
  • Inside one long emit, listeners that are removed with no emit of their event are not kept at all.
  • After an emit returned, the 200 listeners that it removed are collected, on process and on an emitter that stays alive.

Each clause was taken out in turn in a debug build. Each time at least one of these tests failed: no list entry in clear(), in remove() or in removeAll(), no release, a release of every entry, a release only when the outermost emit returns, a list entry for every removal.

Cost (release builds on main 9b8347a: #47120, then this change on it, linux x64)

Instructions of the main thread for each loop iteration, counted with single steps between two markers. Three runs of one binary differ by up to 1.1 in the interpreter and 3.6 in FTL.

per iteration interpreter FTL mallocs
emit, 1 on() listener 1135.8 to 1142.8 (+7.0) 634.5 to 641.5 (+7.0) 0 to 0
emit, 4 on() listeners 2941.5 to 2948.1 (+6.7) 1572.2 to 1579.2 (+7.0) 1 to 1
once() + emit() 3579.8 to 3592.6 (+12.8) 2859.2 to 2873.2 (+14.0) 3 to 3
3 once() + emit() 9372.4 to 9403.8 (+31.3) 7643.6 to 7674.6 (+31.0) 11 to 11
on() + off() 3576.0 to 3581.0 (+5.0) 3107.0 to 3111.8 (+4.8) 4 to 4
once() + off() 3576.6 to 3581.0 (+4.3) 3106.8 to 3115.1 (+8.4) 4 to 4
removeListener of the first of 8 + prependListener 3488.6 to 3507.6 (+19.0) 3017.0 to 3035.9 (+18.8) 4 to 4
on() + removeAllListeners(type) 2950.7 to 2987.6 (+36.9) 2538.9 to 2577.7 (+38.8) 3 to 3
listeners(), 8 listeners 1877.3 to 1877.9 (+0.6) 1458.5 to 1459.5 (+1.0) 1 to 1
  • IdentifierEventListenerMap: 184 to 216 bytes (one vector with room for two entries). EventEmitter: 264 to 296 bytes. SimpleRegisteredEventListener: 16 bytes on both builds.
  • .text: 83,497,513 to 83,498,793 bytes. The stripped binary has the same size on both builds.

Users. No user reported this fault. #9348 is the report that #11329 closed. The third point of this request for #47122 needs it ("wrappers stay alive through a reentrant dispatch snapshot").

Also run (debug ASAN build)

  • test/js/node/events/event-emitter.test.ts: 128 pass.
  • process-signal-listener-count.test.ts, process-nexttick.test.js, process-memory-pressure.test.ts, process-stdin.test.ts, node-stream.test.js, spawn.ipc.test.ts, spawn-ipc-gc.test.ts: all pass.
  • process.test.js: 196 pass, 1 fail (the Node version check of this machine).
  • process-on.test.ts: 2 pass, 1 fail (should work inside --compile).
  • The two failures are not from this change: release builds of main and of this branch give the same two.
  • 119 of the 121 test-event-emitter-*, test-events-*, test-process-*, test-signal-* and test-worker-messaging-* files of test/js/node/test/parallel pass as plain scripts. The other two need the node:test runner, also on main.

Not run locally: Windows and macOS.

@robobun

robobun commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 5:06 PM PT - Oct 27th, 2026

@robobun, your commit 6c111e7 is building: #134042

@robobun

robobun commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator Author

Reproduced on main (9b8347a) with the script in the Notes of the PR body. A listener that an earlier listener removes during an emit does not run in 129 to 147 of 200 emits, with no forced collection. Node v26.3.0 runs it in all 200.

CI: no failure comes from this change.

  • Build 134132 has one red job, on macOS x64. The test is bun check > finishes within 10 s on conditions nested deeper than the stack allows in test/cli/check/check.test.ts: bun check ran 10.1 s and was killed. This PR does not touch bun check.
  • The test-aot-drawn jobs that fail are soft failures. They fail in the same way on main (build 134092).
  • The other 184 jobs passed.

This is the second PR of a stack of three: #47120, #47121, #47122.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review found no issues

No high-confidence issues detected in this change.

…t returns

The emit loop of the native EventEmitter still calls a listener that an
earlier listener of the same emit removed. After the removal only the loop's
copy of the listener vector held the registration, and the GC does not see
that copy. A collection before the call dropped the function, and the loop
skipped the listener.

The listener map now keeps a registration that is removed while an emit holds
it, and visits it, until that emit returns.
@robobun
robobun force-pushed the robobun/16f79adc/native-emitter-once-reentrant-emit branch from 6c111e7 to 5c698f8 Compare October 28, 2026 01:46
@robobun
robobun force-pushed the robobun/16f79adc/native-emitter-keep-removed-listener branch from f8327dd to 1372b0d Compare October 28, 2026 01:46

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed the updated version of this PR and found no bugs; since it adds GC-rooting logic keyed on reference counts to the native emitter that backs process, a human look would still be worthwhile.

What was reviewed:

  • Refcount-as-liveness signal: keepAliveIfHeldByEmit runs before the map drops its own RefPtr in all three removal paths (remove, removeAll, clear), and the only other holders of a SimpleRegisteredEventListener are the by-value copies in innerInvokeEventListeners, so !hasOneRef() does mean "an emit holds it".
  • Release ordering: the emit's copy is a by-value parameter destroyed before releaseListenersRemovedWhileFiring() runs on the next line, and nested emits (including the internal error re-dispatch) cannot drop entries an outer copy still holds.
  • Lock discipline: appends happen inside the already-locked removal paths with no re-acquire; the slow release takes the lock itself from an unlocked caller; the unlocked isEmpty() fast check only races with same-thread writers (the GC thread is a locked reader).
  • The once-listener path's no-op willRemove is correct: the loop's EnsureStillAliveScope covers that call and the fired bit stops any outer emit from calling it again.
Extended reasoning...

The change touches the native WebCore::EventEmitter listener map (src/jsc/bindings/webcore/IdentifierEventListenerMap.{h,cpp}, EventEmitter.cpp) used by process and other native emitters, adding a GC-visited m_removedWhileFiring vector so listeners removed mid-emit stay rooted until the emit returns, plus 11 tests in test/js/node/events/event-emitter.test.ts. It touches no injection, auth, or data-exposure surface, but it is GC-safety and reference-count logic, which the repository's review guidance names as its most-blocked category. The C++ diff is small (~60 lines) and I traced the refcount, release ordering, and lock paths without finding a defect, and no CODEOWNER covers these files; the decision to defer rather than approve rests on the category of code and the design choice of using hasOneRef() as the liveness signal, which a maintainer should weigh. The bug hunt exited on dry_streak and no earlier review from another party is outstanding.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant