Skip to content

events: keep event in dispatching state across all listeners - #67765

Open
ychuhai wants to merge 1 commit into
nodejs:mainfrom
ychuhai:fix/eventtarget-dispatch-state
Open

ychuhai wants to merge 1 commit into
nodejs:mainfrom
ychuhai:fix/eventtarget-dispatch-state

Conversation

@ychuhai

@ychuhai ychuhai commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

EventTarget.prototype[kHybridDispatch] sets event[kIsBeingDispatched] = true before the listener loop and clears it after the loop finishes, but it also cleared arg[kIsBeingDispatched] = false inside the loop right after invoking each listener.

As a result, only the first listener saw the event as being dispatched. Any second or later listener observed event.currentTarget === null, event.eventPhase === Event.NONE (0), an empty event.composedPath(), and could mutate the event with initEvent() or re-dispatch it without ERR_EVENT_RECURSION:

const et = new EventTarget();
et.addEventListener('foo', (e) => console.log('L1:', e.currentTarget === et, e.eventPhase, e.composedPath().length));
et.addEventListener('foo', (e) => console.log('L2:', e.currentTarget === et, e.eventPhase, e.composedPath().length));
et.dispatchEvent(new Event('foo'));

// Before:
// L1: true 2 1
// L2: false 0 0

// After:
// L1: true 2 1
// L2: true 2 1

This removes the premature reset inside the loop and wraps the loop in try/finally so kIsBeingDispatched is always cleared when dispatch exits, even if a once-listener removal hook ([kRemoveListener]) throws.

Note (semver-major): re-dispatching the same event synchronously or calling initEvent() from a second or later listener now throws ERR_EVENT_RECURSION or is ignored, respectively, matching the first listener and the WHATWG DOM spec.

Refs: https://dom.spec.whatwg.org/#concept-event-dispatch

@nodejs-github-bot nodejs-github-bot added the needs-ci PRs that need a full CI run. label Nov 24, 2026
EventTarget.prototype[kHybridDispatch] cleared kIsBeingDispatched inside
the listener loop right after the first listener, even though it is
already cleared after the loop finishes. Subsequent listeners observed
event.currentTarget === null, event.eventPhase === Event.NONE, an empty
composedPath(), and could recursively dispatch the same event.

Remove the premature reset inside the dispatch loop and wrap the loop in
try/finally so kIsBeingDispatched is always cleared after dispatch even
if a once-listener removal hook throws. As a result, re-dispatching the
same event synchronously from a second or later listener now throws
ERR_EVENT_RECURSION, matching the first listener and the DOM spec.

Signed-off-by: Yaroslav Chuhai <yaroslavchuhai@gmail.com>
@ychuhai
ychuhai force-pushed the fix/eventtarget-dispatch-state branch from 7a62d29 to e9dbee5 Compare November 24, 2026 23:22
@codecov

codecov Bot commented Nov 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.56%. Comparing base (dd7bff5) to head (e9dbee5).
⚠️ Report is 27 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67765      +/-   ##
==========================================
+ Coverage   90.52%   90.56%   +0.03%     
==========================================
  Files         803      803              
  Lines      285057   285201     +144     
  Branches    55016    55057      +41     
==========================================
+ Hits       258062   258289     +227     
+ Misses      17148    17070      -78     
+ Partials     9847     9842       -5     
Files with missing lines Coverage Δ
lib/internal/event_target.js 99.59% <100.00%> (+0.80%) ⬆️

... and 42 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Labels

needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants