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>
This commit is contained in:
陈刚 authored and Evan Lucas committed 2018-01-30 14:25:47 -06:00
1 parent 25bebae61c
commit c4abdcdc30
2 files changed
+25 -1

No files matched your search

+1 -1
View File
@@ -283,7 +283,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++;
@@ -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();
}