socket.destroySoon causes two EventEmitter memory leaks when repetitively using it on a socket that is trying to connect
Open
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 122k
- Forks
- 37.3k
- Avg merge
- 4d 2h
- Merged PRs (30d)
- 283
Description
Version
v22.11.0
Platform
Darwin xxx.local 24.4.0 Darwin Kernel Version 24.4.0: Fri Apr 11 18:32:43 PDT 2025; root:xnu-11417.101.15~117/RELEASE_ARM64_T8103 arm64
Subsystem
net
What steps will reproduce the bug?
const { Socket } = require('node:net');
const socket = new Socket();
socket.on('error', (err) => {
console.log(`error: ${err.code}`);
});
socket.on('connect', () => {
console.log('connected');
});
socket.on('close', () => {
console.log('closed');
});
setInterval(() => {
socket.connect({ host: 'google.com', port: 5000 });
setTimeout(() => {
socket.destroySoon();
}, 5);
}, 100);
How often does it reproduce? Is there a required condition?
It reproduces always. Port 5000 is chosen as it times out.
What is the expected behavior? Why is that the expected behavior?
Seeing an infinite stream of
error: ECANCELED
closed
log lines
What do you see instead?
error: ECANCELED
closed
error: ECANCELED
closed
error: ECANCELED
closed
error: ECANCELED
closed
error: ECANCELED
closed
(node:46138) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 finish listeners added to [Socket]. MaxListeners is 10. Use emitter.setMaxListeners() to increase limit
at genericNodeError (node:internal/errors:983:15)
at wrappedFn (node:internal/errors:537:14)
at _addListener (node:events:592:17)
at Socket.addListener (node:events:610:10)
at Readable.on (node:internal/streams/readable:1128:35)
at Socket.once (node:events:654:8)
at Socket.destroySoon (node:net:795:10)
at Timeout._onTimeout (/Users/robeverhardt/Downloads/test.js:17:12)
at listOnTimeout (node:internal/timers:594:17)
at process.processTimers (node:internal/timers:529:7)
error: ECANCELED
closed
error: ECANCELED
closed
error: ECANCELED
closed
error: ECANCELED
closed
(node:46138) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 connect listeners added to [Socket]. MaxListeners is 10. Use emitter.setMaxListeners() to increase limit
at genericNodeError (node:internal/errors:983:15)
at wrappedFn (node:internal/errors:537:14)
at _addListener (node:events:592:17)
at Socket.addListener (node:events:610:10)
at Readable.on (node:internal/streams/readable:1128:35)
at Socket.once (node:events:654:8)
at Socket._final (node:net:525:17)
at prefinish (node:internal/streams/writable:916:14)
at finishMaybe (node:internal/streams/writable:930:5)
at Writable.end (node:internal/streams/writable:845:5)
Additional information
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the reproduction script and the node:net entry points named in the stack traces, especially Socket.destroySoon and Socket._final. Reproduce the repeated cancelled connections, then trace how finish and connect listeners are added and removed. Done means the scenario no longer produces MaxListenersExceededWarning, with coverage for the regression.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, node.js
- Domain
- networking
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 52/100