Skip to content

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

Merged
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
gurgunday:fix/eventtarget-signal-listener-leak
Dec 3, 2026
Merged

nodejs-github-bot merged 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 444 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.

@gurgunday gurgunday added author ready PRs with CI started, the required approvals, and no outstanding review comments. request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. labels Nov 30, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 30, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@gurgunday gurgunday added the resume-ci Add this label to resume the latest eligible Jenkins CI run on a PR with an approving review. label Nov 30, 2026
@github-actions github-actions Bot removed the resume-ci Add this label to resume the latest eligible Jenkins CI run on a PR with an approving review. label Nov 30, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@gurgunday gurgunday added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Dec 3, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Dec 3, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@gurgunday gurgunday added the commit-queue PRs queued for automated landing through the Commit Queue. label Dec 3, 2026
@nodejs-github-bot
nodejs-github-bot merged commit 04ac372 into nodejs:main Dec 3, 2026
83 checks passed
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Landed in 04ac372

@nodejs-github-bot nodejs-github-bot removed the commit-queue PRs queued for automated landing through the Commit Queue. label Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs with CI started, the required approvals, and no outstanding review comments. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants