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(); + })); +}));