-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http: remove stale timeout listeners
In order to prevent a memory leak when using keep alive, ensure that the timeout listener for the request is removed when the response has ended. PR-URL: #9440 Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
- Loading branch information
1 parent
53e8e96
commit dded482
Showing
2 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
test/parallel/test-http-client-timeout-option-listeners.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const http = require('http'); | ||
const assert = require('assert'); | ||
|
||
const agent = new http.Agent({ keepAlive: true }); | ||
|
||
const server = http.createServer((req, res) => { | ||
res.end(''); | ||
}); | ||
|
||
const options = { | ||
agent, | ||
method: 'GET', | ||
port: undefined, | ||
host: common.localhostIPv4, | ||
path: '/', | ||
timeout: common.platformTimeout(100) | ||
}; | ||
|
||
server.listen(0, options.host, common.mustCall(() => { | ||
options.port = server.address().port; | ||
doRequest(common.mustCall((numListeners) => { | ||
assert.strictEqual(numListeners, 1); | ||
doRequest(common.mustCall((numListeners) => { | ||
assert.strictEqual(numListeners, 1); | ||
server.close(); | ||
agent.destroy(); | ||
})); | ||
})); | ||
})); | ||
|
||
function doRequest(cb) { | ||
http.request(options, common.mustCall((response) => { | ||
const sockets = agent.sockets[`${options.host}:${options.port}:`]; | ||
assert.strictEqual(sockets.length, 1); | ||
const socket = sockets[0]; | ||
const numListeners = socket.listeners('timeout').length; | ||
response.resume(); | ||
response.once('end', common.mustCall(() => { | ||
process.nextTick(cb, numListeners); | ||
})); | ||
})).end(); | ||
} |