Skip to content

Commit b8b6c2c

Browse files
mcollinajasnell
authored andcommitted
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 PR-URL: #13195 Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Calvin Metcalf <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 2264d9d commit b8b6c2c

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

lib/_stream_writable.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -371,14 +371,19 @@ function doWrite(stream, state, writev, len, chunk, encoding, cb) {
371371
function onwriteError(stream, state, sync, er, cb) {
372372
--state.pendingcb;
373373
if (sync)
374-
process.nextTick(cb, er);
374+
process.nextTick(afterError, stream, state, cb, er);
375375
else
376-
cb(er);
376+
afterError(stream, state, cb, er);
377377

378378
stream._writableState.errorEmitted = true;
379379
stream.emit('error', er);
380380
}
381381

382+
function afterError(stream, state, cb, err) {
383+
cb(err);
384+
finishMaybe(stream, state);
385+
}
386+
382387
function onwriteStateUpdate(state) {
383388
state.writing = false;
384389
state.writecb = null;
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)