Skip to content

events: fix getMaxListeners() for NodeEventTarget - #67925

Open
deokjinkim wants to merge 1 commit into
nodejs:mainfrom
deokjinkim:261201_events_fix_getMaxListeners_for_NodeEventTarget
Open

deokjinkim wants to merge 1 commit into
nodejs:mainfrom
deokjinkim:261201_events_fix_getMaxListeners_for_NodeEventTarget

Conversation

@deokjinkim

@deokjinkim deokjinkim commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

events.getMaxListeners() returns the default (10) for a NodeEventTarget such as a MessagePort, whatever was set with events.setMaxListeners() or port.setMaxListeners().

It checks for a getMaxListeners method first and, if there is one, reads _maxListeners as it would for an EventEmitter. A NodeEventTarget has that method, but its limit is stored under kMaxEventTargetListeners, which is where events.setMaxListeners() writes it for any EventTarget. So _maxListeners is always undefined on these objects and the default is returned.

This checks kMaxEventTargetListeners first, the same order events.setMaxListeners() uses. Results for EventEmitter and plain EventTarget are unchanged.

The test is in test-nodeeventtarget.js and constructs a NodeEventTarget directly, so it doesn't depend on MessagePort inheriting from it. It covers getEventListeners(), listenerCount(), getMaxListeners() and setMaxListeners() from node:events. Only the getMaxListeners() assertions fail without the fix (10 !== 3).

Fixes: #67916

AI disclosure: written with the help of Claude Code. I checked the cause against the source, reproduced the bug on main, and reviewed the fix and the test.

@nodejs-github-bot nodejs-github-bot added events Issues and PRs related to EventEmitter and the events module. needs-ci PRs that need a full CI run. labels Dec 1, 2026
@deokjinkim
deokjinkim force-pushed the 261201_events_fix_getMaxListeners_for_NodeEventTarget branch from 2137f6b to c5f2f2b Compare December 1, 2026 04:50
@codecov

codecov Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.59%. Comparing base (d4318d9) to head (f50abd0).
⚠️ Report is 8 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67925      +/-   ##
==========================================
- Coverage   90.59%   90.59%   -0.01%     
==========================================
  Files         802      802              
  Lines      286656   286659       +3     
  Branches    55383    55371      -12     
==========================================
- Hits       259710   259694      -16     
- Misses      17058    17075      +17     
- Partials     9888     9890       +2     
Files with missing lines Coverage Δ
lib/events.js 99.60% <100.00%> (+<0.01%) ⬆️

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

events.getMaxListeners() treated any object with a getMaxListeners()
method as an EventEmitter and read `_maxListeners` from it. A
NodeEventTarget, such as a MessagePort, has that method but stores
the value under kMaxEventTargetListeners, so the default was always
returned. Check for the EventTarget value first, in the same order
as events.setMaxListeners().

Fixes: nodejs#67916
Assisted-by: a closed-source coding agent
Signed-off-by: Deokjin Kim <deokjin81.kim@gmail.com>
@deokjinkim
deokjinkim force-pushed the 261201_events_fix_getMaxListeners_for_NodeEventTarget branch from c5f2f2b to f50abd0 Compare December 1, 2026 09:53
@Renegade334 Renegade334 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 Dec 1, 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 1, 2026
@nodejs-github-bot

This comment has been minimized.

@Renegade334 Renegade334 added the resume-ci Add this label to resume the latest eligible Jenkins CI run on a PR with an approving review. label Dec 1, 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 Dec 1, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@Renegade334 Renegade334 added the commit-queue PRs queued for automated landing through the Commit Queue. label Dec 1, 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. commit-queue PRs queued for automated landing through the Commit Queue. events Issues and PRs related to EventEmitter and the events module. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

events.getMaxListeners() always returns default value when passed a NodeEventTarget

3 participants