Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: fix broken pipeline error propagation #31835

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions lib/internal/streams/pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,10 @@ function pipeline(...streams) {
}

let error;
let value;
const destroys = [];

function finish(err, val, final) {
function finish(err, final) {
if (!error && err) {
error = err;
}
Expand All @@ -146,13 +147,13 @@ function pipeline(...streams) {
}

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

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

Expand Down Expand Up @@ -197,12 +198,12 @@ function pipeline(...streams) {
const pt = new PassThrough();
if (isPromise(ret)) {
ret
.catch((err) => {
pt.destroy(err);
})
.then((val) => {
value = val;
pt.end(val);
ronag marked this conversation as resolved.
Show resolved Hide resolved
finish(null, val, true);
})
.catch((err) => {
finish(err, null, true);
});
} else if (isIterable(ret, true)) {
pump(ret, pt, finish);
Expand All @@ -212,7 +213,7 @@ function pipeline(...streams) {
}

ret = pt;
wrap(ret, true, false, true);
ronag marked this conversation as resolved.
Show resolved Hide resolved
wrap(ret, false, true, true);
}
} else if (isStream(stream)) {
if (isReadable(ret)) {
Expand Down
25 changes: 25 additions & 0 deletions test/parallel/test-stream-pipeline-uncaught.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

const common = require('../common');
const {
pipeline,
PassThrough
} = require('stream');
const assert = require('assert');

process.on('uncaughtException', common.mustCall((err) => {
assert.strictEqual(err.message, 'asd');
}));

// Ensure that pipeline that ends with Promise
// still propagates error to uncaughtException.
const s = new PassThrough();
s.end('asd');
pipeline(s, async function(source) {
for await (const chunk of source) {
chunk;
}
}, common.mustCall((err) => {
assert.ok(!err);
ronag marked this conversation as resolved.
Show resolved Hide resolved
throw new Error('asd');
ronag marked this conversation as resolved.
Show resolved Hide resolved
}));
6 changes: 1 addition & 5 deletions test/parallel/test-stream-pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -613,11 +613,9 @@ const { promisify } = require('util');
yield 'hello';
yield 'world';
}, async function*(source) {
const ret = [];
for await (const chunk of source) {
ret.push(chunk.toUpperCase());
yield chunk.toUpperCase();
}
yield ret;
}, async function(source) {
let ret = '';
for await (const chunk of source) {
Expand Down Expand Up @@ -754,7 +752,6 @@ const { promisify } = require('util');
}, common.mustCall((err) => {
assert.strictEqual(err, undefined);
assert.strictEqual(ret, 'asd');
assert.strictEqual(s.destroyed, true);
}));
}

Expand All @@ -775,7 +772,6 @@ const { promisify } = require('util');
}, common.mustCall((err) => {
assert.strictEqual(err, undefined);
assert.strictEqual(ret, 'asd');
assert.strictEqual(s.destroyed, true);
}));
}

Expand Down