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: make sure _destroy is called on the tail #53213

Merged
merged 5 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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);

}