Repository navigation
process: keep a listener that is removed during an emit until the emit returns - #47121
Conversation
|
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.
This is the second PR of a stack of three: #47120, #47121, #47122. |
…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.
6c111e7 to
5c698f8
Compare
f8327dd to
1372b0d
Compare
There was a problem hiding this comment.
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:
keepAliveIfHeldByEmitruns before the map drops its ownRefPtrin all three removal paths (remove,removeAll,clear), and the only other holders of aSimpleRegisteredEventListenerare the by-value copies ininnerInvokeEventListeners, 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 internalerrorre-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
willRemoveis correct: the loop'sEnsureStillAliveScopecovers 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.
Problem
process, the listener does not run if a garbage collection comes between the removal and the call.src/jsc/bindings/webcore/EventEmitter.cpp:227). The GC does not see that copy, and collects the function.Fix
m_removedWhileFiring. The GC visits that list.test/js/node/events/event-emitter.test.ts, 11 new tests fail on process: run a once() listener one time under a re-entrant emit #47120 and pass here. Also ranprocess.test.js,node-stream.test.jsand 119 Nodeparalleltests.Background
processuses a native emitter (WebCore::EventEmitter). The GC reaches the listener functions through its listener map (visitJSEventListeners,IdentifierEventListenerMap.h:75). A registration holds its function weakly.Downsides
on()+removeAllListeners(type)37 more. No path allocates more..textgrows by 1,280 bytes.Notes
Repro, with no forced collection. The first listener removes the second one and then only allocates.
The three ways to remove.
removeListener(type, listener),removeAllListeners(type)andremoveAllListeners()each put the registration on the list. The new tests have one block for each of them, with anon()listener and aonce()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:
heapStats, fewer than 200 of 400 functions are left).processand 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(), inremove()or inremoveAll(), 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.
emit, 1on()listeneremit, 4on()listenersonce()+emit()once()+emit()on()+off()once()+off()removeListenerof the first of 8 +prependListeneron()+removeAllListeners(type)listeners(), 8 listenersIdentifierEventListenerMap: 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).test-event-emitter-*,test-events-*,test-process-*,test-signal-*andtest-worker-messaging-*files oftest/js/node/test/parallelpass as plain scripts. The other two need thenode:testrunner, also on main.Not run locally: Windows and macOS.