Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose setMaxListeners property on AbortSignal #54758

Open
phryneas opened this issue Sep 4, 2024 · 14 comments
Open

Expose setMaxListeners property on AbortSignal #54758

phryneas opened this issue Sep 4, 2024 · 14 comments
Labels
feature request Issues that request new features to be added to Node.js. web-standards Issues and PRs related to Web APIs

Comments

@phryneas
Copy link

phryneas commented Sep 4, 2024

What is the problem this feature will solve?

Right now, there is no stable way to change the maxEventTargetListeners of an AbortSignal without importing from node:events.

This makes it impossible to write isomorphic library code that adds more than the default 10 event listeners, without having to resort to workarounds such as

/**
 * A workaround to set the `maxListeners` property of a node EventEmitter without having to import
 * the `node:events` module, which would make the code non-portable.
 */
function setMaxListeners(maxListeners: number, emitter: any) {
  const key = Object.getOwnPropertySymbols(new AbortController().signal).find(
    (key) => key.description === "events.maxEventTargetListeners"
  );
  if (key) emitter[key] = maxListeners;
}

This relies on the events.maxEventTargetListeners symbol description, which, to my knowledge is not part of any public API and could change at any time.

What is the feature you are proposing to solve the problem?

Add a AbortSignal.setMaxListeners instance method so isomorphic code could do something like

if ('setMaxListeners' in signal) signal.setMaxListeners(20)

What alternatives have you considered?

Alternatively, if this is not an option as it could pollute a spec-defined object with additional properties, use Symbol.for instead of new Symbol for kMaxEventTargetListeners and make that a part of the official api.


I would be willing to implement this feature.

@phryneas phryneas added the feature request Issues that request new features to be added to Node.js. label Sep 4, 2024
@mcollina
Copy link
Member

mcollina commented Sep 4, 2024

The right avenue to purse this change is WHATWG (the group maintaining those standards). From past experience, any time we deviate from the standard we cause more harm than good.

@phryneas
Copy link
Author

phryneas commented Sep 4, 2024

But isn't maxEventTargetListeners in itself - a limit on the number of listeners and issuing a warning - already a node-only deviation from the standard?

So this would try to explore a way to get closer to the standard, not further away.

@phryneas
Copy link
Author

phryneas commented Sep 4, 2024

By that logic, another alternative suggestion here could be:

Alternative suggestion: set no default listener count limit on AbortController

On all newly created AbortSignal instances, set the kMaxEventTargetListeners to 0 by default to align with standard behaviour - users that want a limit on event listeners can still use setMaxListeners to set a specific limit.

In the research before opening this ticket, I've seen a lot of instances where people were getting this warning with undici fetch, so this seems to be a somewhat common problem.

In my case, this is ironically caused because I want to pass an AbortSignal as the signal option to a lot of addEventListener calls so I can make sure to clean them up.

Either way, it seems that AbortSignal is just made to be reused by many fetch calls or addEventListener calls, so the current limit of 10 is easily reached by normal intended usage (and afaik not to spec).

@RedYetiDev RedYetiDev added the web-standards Issues and PRs related to Web APIs label Sep 4, 2024
@mcollina
Copy link
Member

mcollina commented Sep 4, 2024

@jasnell you added the warning in
#36001

@KhafraDev you added those getters/setters in
#47039

What do you folks think?

@KhafraDev
Copy link
Member

node shouldn't have implemented warnings or interop with EventEmitter in the first place, but we absolutely shouldn't deviate further from the spec and other implementations

@mcollina
Copy link
Member

mcollina commented Sep 4, 2024

@nodejs/web-standards what do you all think?

@mcollina
Copy link
Member

mcollina commented Sep 4, 2024

Pinging @lucacasonato too (hope you don't mind).

@lucacasonato
Copy link

Agree with @KhafraDev. If anything this should be a setter function you import from node:events that you pass the signal to as the first argument.

@jasnell
Copy link
Member

jasnell commented Sep 4, 2024

I agree. We should not extend the standard API with non-standard extensions.

@ljharb
Copy link
Member

ljharb commented Sep 4, 2024

+1 (no nonstandard extensions). I do like the alternative suggestion tho (#54758 (comment))

@benjamingr
Copy link
Member

benjamingr commented Sep 4, 2024

I agree with the alternative suggestion as well

@mcollina
Copy link
Member

mcollina commented Sep 4, 2024

@jasnell are you ok for #54758 (comment)?

@benjamingr
Copy link
Member

Just so we're clear - we are within our rights from the spec's PoV to emit a warning on more than N listeners (or really anything, warnings aren't considered observable). We've clarified that several times with WHATWG explicitly to make sure.

@jasnell
Copy link
Member

jasnell commented Sep 4, 2024

I actually prefer that warning remain in place as it has actually proved useful in a couple of cases. But I won't block removing it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. web-standards Issues and PRs related to Web APIs
Projects
Status: Awaiting Triage
Development

No branches or pull requests

8 participants