Skip to content

Commit c1012b4

Browse files
phatedaddaleax
authored andcommitted
stream: ensure Stream.pipeline re-throws errors without callback
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 <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent c2c9c0c commit c1012b4

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

lib/internal/streams/pipeline.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ function once(callback) {
1919
};
2020
}
2121

22-
function noop() {}
22+
function noop(err) {
23+
// Rethrow the error if it exists to avoid swallowing it
24+
if (err) throw err;
25+
}
2326

2427
function isRequest(stream) {
2528
return stream.setHeader && typeof stream.abort === 'function';

test/parallel/test-stream-pipeline.js

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ common.crashOnUnhandledRejection();
142142
}
143143
});
144144

145-
pipeline(rs, res);
145+
pipeline(rs, res, () => {});
146146
});
147147

148148
server.listen(0, () => {
@@ -177,7 +177,7 @@ common.crashOnUnhandledRejection();
177177
})
178178
});
179179

180-
pipeline(rs, res);
180+
pipeline(rs, res, () => {});
181181
});
182182

183183
server.listen(0, () => {
@@ -206,7 +206,7 @@ common.crashOnUnhandledRejection();
206206
})
207207
});
208208

209-
pipeline(rs, res);
209+
pipeline(rs, res, () => {});
210210
});
211211

212212
let cnt = 10;
@@ -481,3 +481,35 @@ common.crashOnUnhandledRejection();
481481

482482
run();
483483
}
484+
485+
{
486+
const read = new Readable({
487+
read() {}
488+
});
489+
490+
const transform = new Transform({
491+
transform(data, enc, cb) {
492+
cb(new Error('kaboom'));
493+
}
494+
});
495+
496+
const write = new Writable({
497+
write(data, enc, cb) {
498+
cb();
499+
}
500+
});
501+
502+
read.on('close', common.mustCall());
503+
transform.on('close', common.mustCall());
504+
write.on('close', common.mustCall());
505+
506+
process.on('uncaughtException', common.mustCall((err) => {
507+
assert.deepStrictEqual(err, new Error('kaboom'));
508+
}));
509+
510+
const dst = pipeline(read, transform, write);
511+
512+
assert.strictEqual(dst, write);
513+
514+
read.push('hello');
515+
}

0 commit comments

Comments
 (0)