Skip to content

Commit

Permalink
stream: do not swallow errors with async iterators and pipeline
Browse files Browse the repository at this point in the history
Before this patch, pipeline() could swallow errors by pre-emptively
producing a ERR_STREAM_PREMATURE_CLOSE that was not really helpful
to the user.

Co-Authored-By: Robert Nagy <ronagy@icloud.com>

PR-URL: #32051
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
mcollina committed Mar 11, 2020
1 parent e00e77e commit d7fe554
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 15 deletions.
39 changes: 24 additions & 15 deletions lib/internal/streams/pipeline.js
Expand Up @@ -123,6 +123,7 @@ async function pump(iterable, writable, finish) {
if (!EE) {
EE = require('events');
}
let error;
try {
for await (const chunk of iterable) {
if (!writable.write(chunk)) {
Expand All @@ -132,7 +133,9 @@ async function pump(iterable, writable, finish) {
}
writable.end();
} catch (err) {
finish(err);
error = err;
} finally {
finish(error);
}
}

Expand All @@ -149,36 +152,37 @@ function pipeline(...streams) {
let value;
const destroys = [];

function finish(err, final) {
if (!error && err) {
let finishCount = 0;

function finish(err) {
const final = --finishCount === 0;

if (err && (!error || error.code === 'ERR_STREAM_PREMATURE_CLOSE')) {
error = err;
}

if (error || final) {
for (const destroy of destroys) {
destroy(error);
}
if (!error && !final) {
return;
}

while (destroys.length) {
destroys.shift()(error);
}

if (final) {
callback(error, value);
}
}

function wrap(stream, reading, writing, final) {
destroys.push(destroyer(stream, reading, writing, final, (err) => {
finish(err, final);
}));
}

let ret;
for (let i = 0; i < streams.length; i++) {
const stream = streams[i];
const reading = i < streams.length - 1;
const writing = i > 0;

if (isStream(stream)) {
wrap(stream, reading, writing, !reading);
finishCount++;
destroys.push(destroyer(stream, reading, writing, !reading, finish));
}

if (i === 0) {
Expand Down Expand Up @@ -224,20 +228,25 @@ function pipeline(...streams) {
pt.destroy(err);
});
} else if (isIterable(ret, true)) {
finishCount++;
pump(ret, pt, finish);
} else {
throw new ERR_INVALID_RETURN_VALUE(
'AsyncIterable or Promise', 'destination', ret);
}

ret = pt;
wrap(ret, false, true, true);

finishCount++;
destroys.push(destroyer(ret, false, true, true, finish));
}
} else if (isStream(stream)) {
if (isReadable(ret)) {
ret.pipe(stream);
} else {
ret = makeAsyncIterable(ret);

finishCount++;
pump(ret, stream, finish);
}
ret = stream;
Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-stream-pipeline.js
Expand Up @@ -984,3 +984,30 @@ const { promisify } = require('util');
}));
src.end();
}

{
let res = '';
const rs = new Readable({
read() {
setImmediate(() => {
rs.push('hello');
});
}
});
const ws = new Writable({
write: common.mustNotCall()
});
pipeline(rs, async function*(stream) {
/* eslint no-unused-vars: off */
for await (const chunk of stream) {
throw new Error('kaboom');
}
}, async function *(source) {
for await (const chunk of source) {
res += chunk;
}
}, ws, common.mustCall((err) => {
assert.strictEqual(err.message, 'kaboom');
assert.strictEqual(res, '');
}));
}

0 comments on commit d7fe554

Please sign in to comment.