diff options
author | 陈刚 <chengang07@meituan.com> | 2018-01-16 12:17:58 +0800 |
---|---|---|
committer | Matteo Collina <hello@matteocollina.com> | 2018-01-30 15:47:36 +0100 |
commit | c7ca07ab50bdfa29e43bb6d556544a298deeb300 (patch) | |
tree | 08689842603438c701abe40c5c751efeb3778bdf | |
parent | 49b3798f816fafe6af9a6de3303f6adc77d53259 (diff) | |
download | node-new-c7ca07ab50bdfa29e43bb6d556544a298deeb300.tar.gz |
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 <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
-rw-r--r-- | lib/_stream_writable.js | 2 | ||||
-rw-r--r-- | test/parallel/test-stream-writable-write-writev-finish.js | 24 |
2 files changed, 25 insertions, 1 deletions
diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js index de96a90255..154c87bf5c 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 c4aaa36606..c0bb60f925 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(); +} |