Skip to content

Commit

Permalink
stream: propagate write error to end callback
Browse files Browse the repository at this point in the history
Properly propagate all write errors throught to
end callback.

Fixes: nodejs#33684
  • Loading branch information
ronag committed Jun 21, 2020
1 parent fdf10ad commit 5d289d9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const {
ERR_STREAM_WRITE_AFTER_END,
ERR_UNKNOWN_ENCODING
} = require('internal/errors').codes;
const { once } = require('internal/util');

const { errorOrDestroy } = destroyImpl;

Expand Down Expand Up @@ -586,8 +587,17 @@ Writable.prototype.end = function(chunk, encoding, cb) {
encoding = null;
}

if (chunk !== null && chunk !== undefined)
this.write(chunk, encoding);
if (typeof cb === 'function') {
cb = once(cb);
}

if (chunk != null) {
this.write(chunk, encoding, cb ? function(err) {
if (err) {
cb(err);
}
} : null);
}

// .end() fully uncorks.
if (state.corked) {
Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-stream-writable-end-cb-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,23 @@ const stream = require('stream');
writable.emit('error', new Error('kaboom'));
}));
}

{
// Invoke end with write error
const writable = new stream.Writable();

writable._write = (chunk, encoding, cb) => {
process.nextTick(cb);
};
writable._final = (cb) => {
process.nextTick(cb);
};

writable.end();
writable.end('asd', common.mustCall((err) => {
assert.strictEqual(err.code, 'ERR_STREAM_WRITE_AFTER_END');
}));
writable.on('error', common.mustCall((err) => {
assert.strictEqual(err.code, 'ERR_STREAM_WRITE_AFTER_END');
}));
}

0 comments on commit 5d289d9

Please sign in to comment.