Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: remove listeners from free sockets #29259

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
http: free listeners on free sockets
Reduced memory usage by ensuring free sockets don't have extra
listeners while in the pool.
  • Loading branch information
ronag committed Sep 23, 2019
commit e817a8b727f07a3de04a76e9f81ed8acbf49111d
2 changes: 2 additions & 0 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,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.
Expand Down
20 changes: 15 additions & 5 deletions test/parallel/test-http-agent-keepalive.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,19 @@ 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);
ronag marked this conversation as resolved.
Show resolved Hide resolved
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);
});
socket.once('free', callback);
socket.once('close', callback);
}