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 3 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
49 changes: 49 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,51 @@ const assert = require('assert');
assert.deepStrictEqual(await newStream.toArray(), [Buffer.from('Steve RogersOn your left')]);
})().then(common.mustCall());
}

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

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

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

_read() {
// Emulate some slow processing
setTimeout(() => {
if (this.stuff.length) {
this.push(this.stuff.shift());
} else if (this.writableEnded) {
this.push(null);
} else {
setTimeout(() => this._read(), 100);
jakecastelli marked this conversation as resolved.
Show resolved Hide resolved
}
}, 100);
}
}

const pass = new PassThrough({ objectMode: true });
const slow = new SlowProcessor();

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

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

setTimeout(() => {
composed.destroy(new Error('an unexpected error'));
assert.strictEqual(slow.destroyed, true);
}, 100);
}