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: fix pipeline callback not called on ended stream #46600

Merged
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
12 changes: 10 additions & 2 deletions lib/internal/streams/pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const {
isTransformStream,
isWebStream,
isReadableStream,
isReadableEnded,
} = require('internal/streams/utils');
const { AbortController } = require('internal/abort_controller');

Expand Down Expand Up @@ -417,10 +418,17 @@ function pipe(src, dst, finish, { end }) {
// Compat. Before node v10.12.0 stdio used to throw an error so
// pipe() did/does not end() stdio destinations.
// Now they allow it but "secretly" don't close the underlying fd.
src.once('end', () => {

function endFn() {
ended = true;
dst.end();
});
}

if (isReadableEnded(src)) { // End the destination if the source has already ended.
process.nextTick(endFn);
} else {
src.once('end', endFn);
}
} else {
finish();
}
Expand Down
25 changes: 25 additions & 0 deletions test/parallel/test-stream-pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -1591,3 +1591,28 @@ const tsp = require('timers/promises');
assert.strictEqual(writable.endCount, 1);
}));
}

{
const readable = new Readable({
read() {}
});
readable.on('end', common.mustCall(() => {
pipeline(readable, new PassThrough(), common.mustSucceed());
}));
readable.push(null);
readable.read();
}

{
const dup = new Duplex({
read() {},
write(chunk, enc, cb) {
cb();
}
});
dup.on('end', common.mustCall(() => {
pipeline(dup, new PassThrough(), common.mustSucceed());
}));
dup.push(null);
dup.read();
}
12 changes: 11 additions & 1 deletion test/parallel/test-webstreams-pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const common = require('../common');
const assert = require('assert');
const { Readable, Writable, Transform, pipeline } = require('stream');
const { Readable, Writable, Transform, pipeline, PassThrough } = require('stream');
const { pipeline: pipelinePromise } = require('stream/promises');
const { ReadableStream, WritableStream, TransformStream } = require('stream/web');
const http = require('http');
Expand Down Expand Up @@ -410,3 +410,13 @@ const http = require('http');
}
c.close();
}

{
const rs = new ReadableStream({
start(controller) {
controller.close();
}
});

pipeline(rs, new PassThrough(), common.mustSucceed());
}