diff --git a/lib/http.js b/lib/http.js index 5e86a33f0d..bd0aec972c 100644 --- a/lib/http.js +++ b/lib/http.js @@ -1235,10 +1235,15 @@ Agent.prototype._establishNewConnection = function() { // has a request but no response and it never emitted an error event: // THEN we need to trigger it manually. // There must be a better way to do this. - if (socket._httpMessage && - !socket._httpMessage.res && - !socket._httpMessage._hadError) { - socket._httpMessage.emit('error', new Error('socket hang up')); + if (socket._httpMessage) { + if (socket._httpMessage.res) { + socket._httpMessage.res.emit('aborted'); + socket._httpMessage.res.emit('close'); + } else { + if (!socket._httpMessage._hadError) { + socket._httpMessage.emit('error', new Error('socket hang up')); + } + } } self._removeSocket(socket); diff --git a/test/simple/test-http-abort-client.js b/test/simple/test-http-abort-client.js new file mode 100644 index 0000000000..e54a50631e --- /dev/null +++ b/test/simple/test-http-abort-client.js @@ -0,0 +1,46 @@ +var common = require("../common"); +var http = require("http"); +var assert = require("assert"); + +var server = http.Server(function (req, res) { + console.log("Server accepted request."); + res.writeHead(200); + res.write("Part of my res."); + + res.destroy(); +}); + +var responseClose = false + +server.listen(common.PORT, function() { + var client = http.get({ + port: common.PORT, + headers: { connection: "keep-alive" } + + }, function(res) { + server.close(); + + console.log("Got res: " + res.statusCode); + console.dir(res.headers); + + res.on("data", function (chunk) { + console.log("Read " + chunk.length + " bytes"); + console.log(chunk.toString()); + }); + + res.on("end", function () { + console.log("Response ended."); + }); + + // it would be nice if this worked: + res.on("close", function () { + console.log("Response aborted"); + responseClose = true; + }); + }); +}); + + +process.on('exit', function() { + assert.ok(responseClose); +});