Skip to content

Commit

Permalink
http: fix event listener leak
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Robert Nagy authored and targos committed Aug 26, 2019
1 parent 3cc8fca commit dedbd11
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ function socketOnData(d) {
!statusIsInformational(parser.incoming.statusCode)) {
socket.removeListener('data', socketOnData);
socket.removeListener('end', socketOnEnd);
socket.removeListener('drain', ondrain);
freeParser(parser, req, socket);
}
}
Expand Down Expand Up @@ -609,6 +610,7 @@ function responseKeepAlive(res, req) {
}
socket.removeListener('close', socketCloseListener);
socket.removeListener('error', socketErrorListener);
socket.removeListener('drain', ondrain);
socket.once('error', freeSocketErrorListener);
// There are cases where _handle === null. Avoid those. Passing null to
// nextTick() will call getDefaultTriggerAsyncId() to retrieve the id.
Expand Down
11 changes: 10 additions & 1 deletion test/parallel/test-http-agent-keepalive.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function get(path, callback) {
port: server.address().port,
agent: agent,
path: path
}, callback);
}, callback).on('socket', common.mustCall(checkListeners));
}

function checkDataAndSockets(body) {
Expand Down Expand Up @@ -134,3 +134,12 @@ server.listen(0, common.mustCall(() => {
}));
}));
}));

// Check for listener leaks when reusing sockets.
function checkListeners(socket) {
assert.strictEqual(socket.listenerCount('data'), 1);
assert.strictEqual(socket.listenerCount('drain'), 1);
assert.strictEqual(socket.listenerCount('error'), 1);
// Sockets have onReadableStreamEnd.
assert.strictEqual(socket.listenerCount('end'), 2);
}

0 comments on commit dedbd11

Please sign in to comment.