From db4b99d53b9bd82ace6067479be7cf8a0c9b388b Mon Sep 17 00:00:00 2001 From: rickyes <0x19951125@gmail.com> Date: Wed, 28 Oct 2020 15:14:48 +0800 Subject: [PATCH 1/5] http: OnFinish will not be triggered again when finished --- lib/_http_outgoing.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index 48d113bb5cf232..9bb49ad630b901 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -808,9 +808,7 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) { this.socket.cork(); } - if (chunk) { - write_(this, chunk, encoding, null, true); - } else if (this.finished) { + if (this.finished) { if (typeof callback === 'function') { if (!this.writableFinished) { this.on('finish', callback); @@ -819,6 +817,8 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) { } } return this; + } else if (chunk) { + write_(this, chunk, encoding, null, true); } else if (!this._header) { this._contentLength = 0; this._implicitHeader(); From 7757632500d8b8c3b69d57c7a36f042d5fb09834 Mon Sep 17 00:00:00 2001 From: rickyes <0x19951125@gmail.com> Date: Wed, 28 Oct 2020 18:09:05 +0800 Subject: [PATCH 2/5] fixup --- lib/_http_outgoing.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index 9bb49ad630b901..7394cc4a40ec19 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -808,7 +808,15 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) { this.socket.cork(); } - if (this.finished) { + if (chunk) { + if (this.finished) { + onError(this, + new ERR_STREAM_WRITE_AFTER_END(), + typeof callback !== 'function' ? nop : callback); + return this; + } + write_(this, chunk, encoding, null, true); + } else if (this.finished) { if (typeof callback === 'function') { if (!this.writableFinished) { this.on('finish', callback); @@ -817,8 +825,6 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) { } } return this; - } else if (chunk) { - write_(this, chunk, encoding, null, true); } else if (!this._header) { this._contentLength = 0; this._implicitHeader(); From 114362ba705c4b92d88220edbece578d2b300be8 Mon Sep 17 00:00:00 2001 From: rickyes <0x19951125@gmail.com> Date: Wed, 28 Oct 2020 19:53:20 +0800 Subject: [PATCH 3/5] fixup --- test/parallel/test-http-outgoing-end-multiple.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test/parallel/test-http-outgoing-end-multiple.js b/test/parallel/test-http-outgoing-end-multiple.js index 7c43e1f59d5849..ed42c913375e84 100644 --- a/test/parallel/test-http-outgoing-end-multiple.js +++ b/test/parallel/test-http-outgoing-end-multiple.js @@ -3,9 +3,17 @@ const common = require('../common'); const assert = require('assert'); const http = require('http'); +const onWriteAfterEndError = common.mustCall((err) => { + assert.strictEqual(err.code, 'ERR_STREAM_WRITE_AFTER_END'); +}, 2); + const server = http.createServer(common.mustCall(function(req, res) { res.end('testing ended state', common.mustCall()); - res.end(common.mustCall()); + res.end(common.mustCall((err) => { + assert.strictEqual(err.code, 'ERR_STREAM_ALREADY_FINISHED'); + })); + res.end('end', onWriteAfterEndError); + res.on('error', onWriteAfterEndError); res.on('finish', common.mustCall(() => { res.end(common.mustCall((err) => { assert.strictEqual(err.code, 'ERR_STREAM_ALREADY_FINISHED'); From 6e1c6975a7d545a722b4d41b23ff5199f479ab0a Mon Sep 17 00:00:00 2001 From: rickyes <0x19951125@gmail.com> Date: Wed, 28 Oct 2020 20:44:42 +0800 Subject: [PATCH 4/5] fixup! got error event before error callback --- lib/_http_outgoing.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index 7394cc4a40ec19..a92eaa3293c51c 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -685,10 +685,10 @@ function onError(msg, err, callback) { } function emitErrorNt(msg, err, callback) { - callback(err); if (typeof msg.emit === 'function' && !msg._closed) { msg.emit('error', err); } + callback(err); } function write_(msg, chunk, encoding, callback, fromEnd) { From 381d8728d153a9b84e9fff8702d113feb18a847b Mon Sep 17 00:00:00 2001 From: rickyes <0x19951125@gmail.com> Date: Sun, 8 Nov 2020 20:58:32 +0800 Subject: [PATCH 5/5] fixup! callback order align with stream.Writable --- lib/_http_outgoing.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index a92eaa3293c51c..7394cc4a40ec19 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -685,10 +685,10 @@ function onError(msg, err, callback) { } function emitErrorNt(msg, err, callback) { + callback(err); if (typeof msg.emit === 'function' && !msg._closed) { msg.emit('error', err); } - callback(err); } function write_(msg, chunk, encoding, callback, fromEnd) {