Skip to content

Commit f39ad8a

Browse files
Robert NagyTrott
Robert Nagy
authored andcommitted
http: fix event listener leak
Fixes: #29239 PR-URL: #29245 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Jiawen Geng <technicalcute@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 4e188b3 commit f39ad8a

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/_http_client.js

+2
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ function socketOnData(d) {
506506
!statusIsInformational(parser.incoming.statusCode)) {
507507
socket.removeListener('data', socketOnData);
508508
socket.removeListener('end', socketOnEnd);
509+
socket.removeListener('drain', ondrain);
509510
freeParser(parser, req, socket);
510511
}
511512
}
@@ -613,6 +614,7 @@ function responseKeepAlive(res, req) {
613614
}
614615
socket.removeListener('close', socketCloseListener);
615616
socket.removeListener('error', socketErrorListener);
617+
socket.removeListener('drain', ondrain);
616618
socket.once('error', freeSocketErrorListener);
617619
// There are cases where _handle === null. Avoid those. Passing null to
618620
// nextTick() will call getDefaultTriggerAsyncId() to retrieve the id.

test/parallel/test-http-agent-keepalive.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function get(path, callback) {
5252
port: server.address().port,
5353
agent: agent,
5454
path: path
55-
}, callback);
55+
}, callback).on('socket', common.mustCall(checkListeners));
5656
}
5757

5858
function checkDataAndSockets(body) {
@@ -134,3 +134,12 @@ server.listen(0, common.mustCall(() => {
134134
}));
135135
}));
136136
}));
137+
138+
// Check for listener leaks when reusing sockets.
139+
function checkListeners(socket) {
140+
assert.strictEqual(socket.listenerCount('data'), 1);
141+
assert.strictEqual(socket.listenerCount('drain'), 1);
142+
assert.strictEqual(socket.listenerCount('error'), 1);
143+
// Sockets have onReadableStreamEnd.
144+
assert.strictEqual(socket.listenerCount('end'), 2);
145+
}

0 commit comments

Comments
 (0)