Skip to content

Warn on EventTarget maxListeners > THRESHOLD #35990

Closed
@benjamingr

Description

@benjamingr

tl;dr;

  • I want to add the "max listener warning" to all event targets (and not just NodeEventTarget)
  • I checked with WHATWG and we are allowed to do this.
  • This is because it's very tricky to wire cancellation correctly when attaching "abort" signal handlers.

cc @jasnell and @addaleax whom are probably the most familiar with this code/question.

Also cc @mcollina since you often have smart things to say in issues related to memory leaks :]


Hey,

@bterlson who helps maintain the Azure SDK has been using AbortController and AbortSignal extensively and has raised a few rather severe issues with AbortControllers/Signals.

First - thanks a ton for the valuable feedback and help Brian!

The biggest one IMO pertains to a memory leak he ran into repeatedly where:

  • You have one AbortSignal/AbortController bound to the process (to deal with SIGINT for example) that propagates
  • Actions downstream have their own AbortController/AbortSignals and there exists code that links them up, for example: processLevelSignal.addEventListener('abort', e => childOpController.abort())
  • Since the handlers are never cleaned up - memory leaks, it basically requires library authors to write tricky cancellation aware code. This is so tricky that when we implemented APIs that take signals we didn't always get it right. For example our timers/promises timeout does not clean up its signal on successful completion (added by @jasnell, whom added AbortController and is probably the most competent with this API in core and is a strong dev in general).

We already have NodeEventTarget that checks for this bug and maxListeners like EventEmitter.

I asked WHATWG if we are allowed to emit a warning based on maxListeners to the console like we do for EventEmitters.

Note - this is a problem even if the handlers are attached with { once: true } - like the above case where the issue is when the timer completes successfully.

Metadata

Metadata

Assignees

No one assigned

    Labels

    eventsIssues and PRs related to the events subsystem / EventEmitter.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions