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
- Use a
keepAlive https.Agent.
- Make repeated requests through the plugin.
- Inspect the socket and check event listener counts.
- 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
Socket Event Listener Leak with keepAlive https.Agent
Issue Description
When using the plugin with a
keepAlivehttps.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
keepAlivehttps.Agent.1.Suggested Fix
Add a per-socket guard before attaching listeners, ensuring that each socket is instrumented only once:
This prevents duplicate listener attachment when sockets are reused by the agent.
Environment