Skip to content

Commit

Permalink
stream: avoid writeAfterEnd() while ending
Browse files Browse the repository at this point in the history
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: #18170
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
陈刚 authored and mcollina committed Jan 30, 2018
1 parent 49b3798 commit c7ca07a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down
24 changes: 24 additions & 0 deletions test/parallel/test-stream-writable-write-writev-finish.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit c7ca07a

Please sign in to comment.