From 1fc00f082175e4aaceb0039f331087cae541cda7 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. Backport-PR-URL: https://github.com/nodejs/node/pull/20786 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 7586a48680bb6a..5f1e56caeab981 100644 --- a/lib/_http_agent.js +++ b/lib/_http_agent.js @@ -277,7 +277,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 dfab2fce131122..135d31acaaec2d 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -470,6 +470,7 @@ function socketOnData(d) { // TODO(isaacs): Need a way to reset a stream to fresh state // IE, not flowing, and not explicitly paused. socket._readableState.flowing = null; + socket._httpMessage = null; req.emit(eventName, res, socket, bodyHead); req.emit('close'); 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(); + })); +}));