Skip to content

Commit c335929

Browse files
committed
stream: emit finish when using writev and cork
In Writable, 'finish' was not emitted when using writev() and cork() in the event of an Error during the write. This commit makes it consistent with the write() path, which emits 'finish'. Fixes: #11121
1 parent ccd3ead commit c335929

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

lib/_stream_writable.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,14 +358,19 @@ function doWrite(stream, state, writev, len, chunk, encoding, cb) {
358358
function onwriteError(stream, state, sync, er, cb) {
359359
--state.pendingcb;
360360
if (sync)
361-
process.nextTick(cb, er);
361+
process.nextTick(afterError, stream, state, cb, er);
362362
else
363-
cb(er);
363+
afterError(stream, state, cb, er);
364364

365365
stream._writableState.errorEmitted = true;
366366
stream.emit('error', er);
367367
}
368368

369+
function afterError(stream, state, cb, err) {
370+
cb(err);
371+
finishMaybe(stream, state);
372+
}
373+
369374
function onwriteStateUpdate(state) {
370375
state.writing = false;
371376
state.writecb = null;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const stream = require('stream');
6+
7+
// ensure consistency between the finish event when using cork()
8+
// and writev and when not using them
9+
10+
{
11+
const writable = new stream.Writable();
12+
13+
writable._write = (chunks, encoding, cb) => {
14+
cb(new Error('write test error'));
15+
};
16+
17+
writable.on('finish', common.mustCall());
18+
19+
writable.on('prefinish', common.mustCall());
20+
21+
writable.on('error', common.mustCall((er) => {
22+
assert.strictEqual(er.message, 'write test error');
23+
}));
24+
25+
writable.end('test');
26+
}
27+
28+
{
29+
const writable = new stream.Writable();
30+
31+
writable._write = (chunks, encoding, cb) => {
32+
cb(new Error('write test error'));
33+
};
34+
35+
writable._writev = (chunks, cb) => {
36+
cb(new Error('writev test error'));
37+
};
38+
39+
writable.on('finish', common.mustCall());
40+
41+
writable.on('prefinish', common.mustCall());
42+
43+
writable.on('error', common.mustCall((er) => {
44+
assert.strictEqual(er.message, 'writev test error');
45+
}));
46+
47+
writable.cork();
48+
writable.write('test');
49+
50+
setImmediate(function() {
51+
writable.end('test');
52+
});
53+
}

0 commit comments

Comments
 (0)