summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorisaacs <i@izs.me>2013-02-22 10:35:07 -0800
committerisaacs <i@izs.me>2013-02-22 10:35:07 -0800
commitf9a0140ef140d89fed1a194c410cd69ed9990305 (patch)
tree609056bb61be97aaaa1e14cd19b4e09ccc0afe8f
parent22d3eff8f46d336337aa5d9625f6d5be9104bcff (diff)
downloadnode-f9a0140ef140d89fed1a194c410cd69ed9990305.tar.gz
http: Handle hangup writes more gently
-rw-r--r--lib/http.js23
-rw-r--r--test/simple/test-http-destroyed-socket-write.js11
2 files changed, 19 insertions, 15 deletions
diff --git a/lib/http.js b/lib/http.js
index 2446545ba..008143248 100644
--- a/lib/http.js
+++ b/lib/http.js
@@ -447,6 +447,7 @@ function OutgoingMessage() {
this._trailer = '';
this.finished = false;
+ this._hangupClose = false;
}
util.inherits(OutgoingMessage, Stream);
@@ -501,12 +502,24 @@ OutgoingMessage.prototype._writeRaw = function(data, encoding) {
return this.connection.write(data, encoding);
} else if (this.connection && this.connection.destroyed) {
// The socket was destroyed. If we're still trying to write to it,
- // then something bad happened.
- // If we've already raised an error on this message, then just ignore.
- if (!this._hadError) {
- this.emit('error', createHangUpError());
- this._hadError = true;
+ // then something bad happened, but it could be just that we haven't
+ // gotten the 'close' event yet.
+ //
+ // In v0.10 and later, this isn't a problem, since ECONNRESET isn't
+ // ignored in the first place. We'll probably emit 'close' on the
+ // next tick, but just in case it's not coming, set a timeout that
+ // will emit it for us.
+ if (!this._hangupClose) {
+ this._hangupClose = true;
+ var socket = this.socket;
+ var timer = setTimeout(function() {
+ socket.emit('close');
+ });
+ socket.on('close', function() {
+ clearTimeout(timer);
+ });
}
+ return false;
} else {
// buffer, as long as we're not destroyed.
this._buffer(data, encoding);
diff --git a/test/simple/test-http-destroyed-socket-write.js b/test/simple/test-http-destroyed-socket-write.js
index c56298710..b2498fd8a 100644
--- a/test/simple/test-http-destroyed-socket-write.js
+++ b/test/simple/test-http-destroyed-socket-write.js
@@ -75,15 +75,6 @@ server.listen(common.PORT, function() {
var writes = 0;
var sawFalseWrite;
- var gotError = false;
- sec.on('error', function(er) {
- assert.equal(gotError, false);
- gotError = true;
- assert(er.code === 'ECONNRESET');
- clearTimeout(timer);
- test();
- });
-
function write() {
if (++writes === 64) {
clearTimeout(timer);
@@ -121,7 +112,7 @@ server.listen(common.PORT, function() {
console.error('bad happened', sec.output, sec.outputEncodings);
assert.equal(sec.output.length, 0);
assert.equal(sec.outputEncodings, 0);
- assert(gotError);
+ assert(sawFalseWrite);
assert(gotFirstResponse);
assert(gotFirstData);
assert(gotFirstEnd);