Skip to content

Commit

Permalink
stream: ensure Stream.pipeline re-throws errors without callback
Browse files Browse the repository at this point in the history
Fixes an issue where Stream.pipeline wouldn't re-throw errors
on a stream if no callback was specified, thus swallowing
said errors.

Fixes: #20303

PR-URL: #20437
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
phated authored and addaleax committed May 31, 2018
1 parent af06581 commit d1e81b0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
5 changes: 4 additions & 1 deletion lib/internal/streams/pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ function once(callback) {
};
}

function noop() {}
function noop(err) {
// Rethrow the error if it exists to avoid swallowing it
if (err) throw err;
}

function isRequest(stream) {
return stream.setHeader && typeof stream.abort === 'function';
Expand Down
38 changes: 35 additions & 3 deletions test/parallel/test-stream-pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ common.crashOnUnhandledRejection();
}
});

pipeline(rs, res);
pipeline(rs, res, () => {});
});

server.listen(0, () => {
Expand Down Expand Up @@ -177,7 +177,7 @@ common.crashOnUnhandledRejection();
})
});

pipeline(rs, res);
pipeline(rs, res, () => {});
});

server.listen(0, () => {
Expand Down Expand Up @@ -206,7 +206,7 @@ common.crashOnUnhandledRejection();
})
});

pipeline(rs, res);
pipeline(rs, res, () => {});
});

let cnt = 10;
Expand Down Expand Up @@ -481,3 +481,35 @@ common.crashOnUnhandledRejection();

run();
}

{
const read = new Readable({
read() {}
});

const transform = new Transform({
transform(data, enc, cb) {
cb(new Error('kaboom'));
}
});

const write = new Writable({
write(data, enc, cb) {
cb();
}
});

read.on('close', common.mustCall());
transform.on('close', common.mustCall());
write.on('close', common.mustCall());

process.on('uncaughtException', common.mustCall((err) => {
assert.deepStrictEqual(err, new Error('kaboom'));
}));

const dst = pipeline(read, transform, write);

assert.strictEqual(dst, write);

read.push('hello');
}

0 comments on commit d1e81b0

Please sign in to comment.