diff --git a/lib/_http_client.js b/lib/_http_client.js index 796216611519ab..e84400377d8c1a 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -618,6 +618,8 @@ function responseKeepAlive(req) { } socket.removeListener('close', socketCloseListener); socket.removeListener('error', socketErrorListener); + socket.removeListener('data', socketOnData); + socket.removeListener('end', socketOnEnd); socket.once('error', freeSocketErrorListener); // There are cases where _handle === null. Avoid those. Passing null to // nextTick() will call getDefaultTriggerAsyncId() to retrieve the id. diff --git a/test/parallel/test-http-agent-keepalive.js b/test/parallel/test-http-agent-keepalive.js index 0a85b30b0a42ce..a1f902fab091e1 100644 --- a/test/parallel/test-http-agent-keepalive.js +++ b/test/parallel/test-http-agent-keepalive.js @@ -140,9 +140,21 @@ 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); + const callback = common.mustCall(() => { + if (!socket.destroyed) { + assert.strictEqual(socket.listenerCount('data'), 0); + assert.strictEqual(socket.listenerCount('drain'), 0); + // Sockets have freeSocketErrorListener. + assert.strictEqual(socket.listenerCount('error'), 1); + // Sockets have onReadableStreamEnd. + assert.strictEqual(socket.listenerCount('end'), 1); + } + + socket.off('free', callback); + socket.off('close', callback); + }); assert.strictEqual(socket.listenerCount('error'), 1); - // Sockets have onReadableStreamEnd. assert.strictEqual(socket.listenerCount('end'), 2); + socket.once('free', callback); + socket.once('close', callback); }