Skip to content

Commit 643b785

Browse files
committed
stream: forward compose tail error after finish
compose() only listened for 'data' and 'end' on the last stream it was given. When that stream emitted 'error' after its writable side had already finished, pipeline() had already removed its own error listener, so the error reached no listener and was raised as an unhandled 'error' event, crashing the process. Forward tail errors to the composed Duplex by destroying it whenever the tail errors before the composition has been destroyed. Signed-off-by: Matteo Collina <matteo.collina@gmail.com>
1 parent d5570d9 commit 643b785

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

‎lib/internal/streams/compose.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@ module.exports = function compose(...streams) {
196196
tail.on('end', function() {
197197
d.push(null);
198198
});
199+
200+
tail.on('error', function(err) {
201+
if (!d.destroyed) {
202+
d.destroy(err);
203+
}
204+
});
199205
} else if (isWebStream(tail)) {
200206
const readable = isTransformStream(tail) ? tail.readable : tail;
201207
const reader = readable.getReader();
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const {
6+
compose,
7+
Duplex,
8+
Readable,
9+
} = require('stream');
10+
11+
// Returns a Duplex whose writable side finishes (emitting 'finish') but whose
12+
// readable side then errors instead of ending. This mimics a zlib decompressor
13+
// that only detects truncated input during its final flush, which runs after the
14+
// writable side has already finished, so 'end' is never emitted.
15+
function createTail() {
16+
return new Duplex({
17+
write(chunk, encoding, callback) {
18+
callback();
19+
},
20+
final(callback) {
21+
callback();
22+
setImmediate(() => this.destroy(new Error('readable-side failure after finish')));
23+
},
24+
read() {},
25+
});
26+
}
27+
28+
// Regression test: an error emitted by the last stream of compose() after its
29+
// writable side has finished must be forwarded to the composed Duplex, rather
30+
// than becoming an unhandled 'error' event on the tail.
31+
{
32+
const composed = compose(Readable.from([Buffer.from('x')]), createTail());
33+
34+
composed.on('error', common.mustCall((err) => {
35+
assert.strictEqual(err.message, 'readable-side failure after finish');
36+
}));
37+
composed.resume();
38+
}
39+
40+
// The same error must reject a `for await` consumer of the composed stream.
41+
{
42+
const composed = compose(Readable.from([Buffer.from('x')]), createTail());
43+
44+
(async () => {
45+
try {
46+
for await (const chunk of composed) {
47+
assert.ok(chunk);
48+
}
49+
assert.fail('should have thrown');
50+
} catch (err) {
51+
assert.strictEqual(err.message, 'readable-side failure after finish');
52+
}
53+
})().then(common.mustCall());
54+
}

0 commit comments

Comments
 (0)