Skip to content

Commit c7ca07a

Browse files
陈刚mcollina
陈刚
authored andcommitted
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: #18170 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 49b3798 commit c7ca07a

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/_stream_writable.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ Writable.prototype.write = function(chunk, encoding, cb) {
273273
if (typeof cb !== 'function')
274274
cb = nop;
275275

276-
if (state.ended)
276+
if (state.ending)
277277
writeAfterEnd(this, cb);
278278
else if (isBuf || validChunk(this, state, chunk, cb)) {
279279
state.pendingcb++;

test/parallel/test-stream-writable-write-writev-finish.js

+24
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,27 @@ const stream = require('stream');
154154
};
155155
rs.pipe(ws);
156156
}
157+
158+
{
159+
const w = new stream.Writable();
160+
w._write = (chunk, encoding, cb) => {
161+
process.nextTick(cb);
162+
};
163+
w.on('error', common.mustCall());
164+
w.on('prefinish', () => {
165+
w.write("shouldn't write in prefinish listener");
166+
});
167+
w.end();
168+
}
169+
170+
{
171+
const w = new stream.Writable();
172+
w._write = (chunk, encoding, cb) => {
173+
process.nextTick(cb);
174+
};
175+
w.on('error', common.mustCall());
176+
w.on('finish', () => {
177+
w.write("should't write in finish listener");
178+
});
179+
w.end();
180+
}

0 commit comments

Comments
 (0)