Skip to content

Commit

Permalink
stream: make sure _destroy is called
Browse files Browse the repository at this point in the history
PR-URL: #53213
Fixes: #51987
Reviewed-By: Robert Nagy <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
  • Loading branch information
jakecastelli authored and targos committed Jun 20, 2024
1 parent 4a3525b commit f4efb7f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/internal/streams/compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,14 @@ module.exports = function compose(...streams) {
ondrain = null;
onfinish = null;

if (isNodeStream(tail)) {
destroyer(tail, err);
}

if (onclose === null) {
callback(err);
} else {
onclose = callback;
if (isNodeStream(tail)) {
destroyer(tail, err);
}
}
};

Expand Down
45 changes: 45 additions & 0 deletions test/parallel/test-stream-compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

const common = require('../common');
const {
Duplex,
Readable,
Transform,
Writable,
Expand Down Expand Up @@ -494,3 +495,47 @@ const assert = require('assert');
assert.deepStrictEqual(await newStream.toArray(), [Buffer.from('Steve RogersOn your left')]);
})().then(common.mustCall());
}

{
class DuplexProcess extends Duplex {
constructor(options) {
super({ ...options, objectMode: true });
this.stuff = [];
}

_write(message, _, callback) {
this.stuff.push(message);
callback();
}

_destroy(err, cb) {
cb(err);
}

_read() {
if (this.stuff.length) {
this.push(this.stuff.shift());
} else if (this.writableEnded) {
this.push(null);
} else {
this._read();
}
}
}

const pass = new PassThrough({ objectMode: true });
const duplex = new DuplexProcess();

const composed = compose(
pass,
duplex
).on('error', () => {});

composed.write('hello');
composed.write('world');
composed.end();

composed.destroy(new Error('an unexpected error'));
assert.strictEqual(duplex.destroyed, true);

}

0 comments on commit f4efb7f

Please sign in to comment.