Skip to content

events: detach signal abort listener on listener removal - #67501

Open
gurgunday wants to merge 1 commit into
nodejs:mainfrom
gurgunday:fix/eventtarget-signal-listener-leak
Open

gurgunday wants to merge 1 commit into
nodejs:mainfrom
gurgunday:fix/eventtarget-signal-listener-leak

Conversation

@gurgunday

@gurgunday gurgunday commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Ran into this while running some benchmarks and seeing the memory grow

Removing an event listener that was added with a signal leaves its abort listener hanging on the signal.

As an example, here, each add/remove cycle leaks one listener, until the signal aborts (if ever)

const ac = new AbortController();
const et = new EventTarget();
function h() {}

for (let i = 0; i < 8000; i++) {
  et.addEventListener('foo', h, { signal: ac.signal });
  et.removeEventListener('foo', h);
}

events.getEventListeners(ac.signal, 'abort').length; // 8000 on main, 0 with the fix

addEventListener() with a signal registers an abort listener on the
signal that was never removed when the event listener was removed by
other means (removeEventListener(), a once listener firing,
removeAllListeners(), weak listener cleanup). Every add/remove cycle
on a long-lived signal left one more abort listener behind, slowing
down later add/remove calls on that signal.

Keep the abort listener and a weak reference to the signal on the
Listener and detach it when the listener is removed.

Assisted-by: claude:opus-5.5
Signed-off-by: Gürgün Dayıoğlu <hey@gurgun.day>
@nodejs-github-bot nodejs-github-bot added the needs-ci PRs that need a full CI run. label Nov 15, 2026
@codecov

codecov Bot commented Nov 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.66%. Comparing base (55da353) to head (decdf0d).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67501      +/-   ##
==========================================
- Coverage   90.66%   90.66%   -0.01%     
==========================================
  Files         799      799              
  Lines      280245   280268      +23     
  Branches    54121    54125       +4     
==========================================
+ Hits       254097   254102       +5     
- Misses      16277    16286       +9     
- Partials     9871     9880       +9     
Files with missing lines Coverage Δ
lib/internal/event_target.js 98.81% <100.00%> (+0.02%) ⬆️

... and 23 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