From c7ca07ab50bdfa29e43bb6d556544a298deeb300 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=88=9A?= Date: Tue, 16 Jan 2018 12:17:58 +0800 Subject: [PATCH] stream: avoid writeAfterEnd() while ending Calling `writable.end()` will probably synchronously call `writable.write()`, in such a situation the `state.ended` is false and `writable.write()` doesn't trigger `writeAfterEnd()`. PR-URL: https://github.com/nodejs/node/pull/18170 Reviewed-By: Anna Henningsen Reviewed-By: Matteo Collina Reviewed-By: James M Snell --- lib/_stream_writable.js | 2 +- ...est-stream-writable-write-writev-finish.js | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js index de96a902552..154c87bf5c7 100644 --- a/lib/_stream_writable.js +++ b/lib/_stream_writable.js @@ -273,7 +273,7 @@ Writable.prototype.write = function(chunk, encoding, cb) { if (typeof cb !== 'function') cb = nop; - if (state.ended) + if (state.ending) writeAfterEnd(this, cb); else if (isBuf || validChunk(this, state, chunk, cb)) { state.pendingcb++; diff --git a/test/parallel/test-stream-writable-write-writev-finish.js b/test/parallel/test-stream-writable-write-writev-finish.js index c4aaa36606c..c0bb60f925c 100644 --- a/test/parallel/test-stream-writable-write-writev-finish.js +++ b/test/parallel/test-stream-writable-write-writev-finish.js @@ -154,3 +154,27 @@ const stream = require('stream'); }; rs.pipe(ws); } + +{ + const w = new stream.Writable(); + w._write = (chunk, encoding, cb) => { + process.nextTick(cb); + }; + w.on('error', common.mustCall()); + w.on('prefinish', () => { + w.write("shouldn't write in prefinish listener"); + }); + w.end(); +} + +{ + const w = new stream.Writable(); + w._write = (chunk, encoding, cb) => { + process.nextTick(cb); + }; + w.on('error', common.mustCall()); + w.on('finish', () => { + w.write("should't write in finish listener"); + }); + w.end(); +}