Skip to content

Socket event listeners are attached multiple times, causing a memory leak with keepAlive agents #21

@0xHristo

Description

@0xHristo

Socket Event Listener Leak with keepAlive https.Agent

Issue Description

When using the plugin with a keepAlive https.Agent, socket event listeners are attached on every request.
Over time, this accumulates listeners on the same socket, which can trigger memory leaks and MaxListenersExceededWarnings.


Expected Behavior

Each socket should only have one set of event listeners attached, regardless of how many requests reuse that socket.


Actual Behavior

Event listeners are attached multiple times to the same socket.
The listener count grows with each request made through the plugin.


Reproduction Steps

  1. Use a keepAlive https.Agent.
  2. Make repeated requests through the plugin.
  3. Inspect the socket and check event listener counts.
  4. Observe that listener counts grow beyond 1.

Suggested Fix

Add a per-socket guard before attaching listeners, ensuring that each socket is instrumented only once:

if (!socket.haveEvents) {
  socket.haveEvents = true;
  // attach listeners
}

This prevents duplicate listener attachment when sockets are reused by the agent.


Environment

  • Node.js: all
  • superagent: all
  • OS: all

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions