Skip to content

Commit e6d3654

Browse files
author
陈刚
committed
stream: use state.ending to see if stream called end()
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()`.
1 parent 7809f38 commit e6d3654

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
@@ -283,7 +283,7 @@ Writable.prototype.write = function(chunk, encoding, cb) {
283283
if (typeof cb !== 'function')
284284
cb = nop;
285285

286-
if (state.ended)
286+
if (state.ending)
287287
writeAfterEnd(this, cb);
288288
else if (isBuf || validChunk(this, state, chunk, cb)) {
289289
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)