From 4a940aadfabc35be404da64e9ab7014a8cc71728 Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Wed, 16 May 2018 22:15:20 +0200 Subject: [PATCH] http: do not rely on the 'agentRemove' event Do not use the `'agentRemove'` event to null `socket._httpMessage` as that event is public and can be used to not keep a request in the agent. PR-URL: https://github.com/nodejs/node/pull/20786 Fixes: https://github.com/nodejs/node/issues/20690 Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell Reviewed-By: Trivikram Kamat Reviewed-By: Anatoli Papirovski --- lib/_http_agent.js | 1 - lib/_http_client.js | 1 + test/parallel/test-http-agent-remove.js | 21 +++++++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-http-agent-remove.js diff --git a/lib/_http_agent.js b/lib/_http_agent.js index 303f56bfe4ba21..67f3d667b28169 100644 --- a/lib/_http_agent.js +++ b/lib/_http_agent.js @@ -276,7 +276,6 @@ function installListeners(agent, s, options) { s.removeListener('close', onClose); s.removeListener('free', onFree); s.removeListener('agentRemove', onRemove); - s._httpMessage = null; } s.on('agentRemove', onRemove); } diff --git a/lib/_http_client.js b/lib/_http_client.js index 4b21cee745b3ce..4542a81cf0e9a1 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -467,6 +467,7 @@ function socketOnData(d) { socket.removeListener('close', socketCloseListener); socket.removeListener('error', socketErrorListener); + socket._httpMessage = null; socket.readableFlowing = null; req.emit(eventName, res, socket, bodyHead); diff --git a/test/parallel/test-http-agent-remove.js b/test/parallel/test-http-agent-remove.js new file mode 100644 index 00000000000000..24fc7fcb8282fe --- /dev/null +++ b/test/parallel/test-http-agent-remove.js @@ -0,0 +1,21 @@ +'use strict'; +const { mustCall } = require('../common'); + +const http = require('http'); +const { strictEqual } = require('assert'); + +const server = http.createServer(mustCall((req, res) => { + res.flushHeaders(); +})); + +server.listen(0, mustCall(() => { + const req = http.get({ + port: server.address().port + }, mustCall(() => { + const { socket } = req; + socket.emit('agentRemove'); + strictEqual(socket._httpMessage, req); + socket.destroy(); + server.close(); + })); +}));