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

[v13.x backport] stream: don't destroy final readable stream in pipeline #32111

Closed
Closed
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
stream: don't destroy final readable stream in pipeline
If the last stream in a pipeline is still usable/readable
don't destroy it to allow further composition.

Fixes: #32105
Backport-PR-URL: #32111
ronag committed Mar 5, 2020
commit 07228c680a622f9f94059cad4cefb19f8cf0a47b
9 changes: 6 additions & 3 deletions lib/internal/streams/pipeline.js
Original file line number Diff line number Diff line change
@@ -36,15 +36,18 @@ function destroyStream(stream, err) {
if (typeof stream.close === 'function') return stream.close();
}

function destroyer(stream, reading, writing, callback) {
function destroyer(stream, reading, writing, final, callback) {
callback = once(callback);
let destroyed = false;

if (eos === undefined) eos = require('internal/streams/end-of-stream');
eos(stream, { readable: reading, writable: writing }, (err) => {
if (destroyed) return;
destroyed = true;
destroyStream(stream, err);
const readable = stream.readable || isRequest(stream);
if (err || !final || !readable) {
destroyStream(stream, err);
}
callback(err);
});

@@ -176,7 +179,7 @@ function pipeline(...streams) {
}

function wrap(stream, reading, writing, final) {
destroys.push(destroyer(stream, reading, writing, (err) => {
destroys.push(destroyer(stream, reading, writing, final, (err) => {
finish(err, final);
}));
}
46 changes: 46 additions & 0 deletions test/parallel/test-stream-pipeline.js
Original file line number Diff line number Diff line change
@@ -918,6 +918,52 @@ const { promisify } = require('util');
const dst = new PassThrough({ autoDestroy: false });
pipeline(src, dst, common.mustCall(() => {
assert.strictEqual(src.destroyed, true);
assert.strictEqual(dst.destroyed, false);
}));
src.end();
}

{
const server = http.createServer((req, res) => {
});

server.listen(0, () => {
const req = http.request({
port: server.address().port
});

const body = new PassThrough();
pipeline(
body,
req,
common.mustCall((err) => {
assert(!err);
assert(!req.res);
assert(!req.aborted);
req.abort();
server.close();
})
);
body.end();
});
}

{
const src = new PassThrough();
const dst = new PassThrough();
pipeline(src, dst, common.mustCall((err) => {
assert(!err);
assert.strictEqual(dst.destroyed, false);
}));
src.end();
}

{
const src = new PassThrough();
const dst = new PassThrough();
dst.readable = false;
pipeline(src, dst, common.mustCall((err) => {
assert(!err);
assert.strictEqual(dst.destroyed, true);
}));
src.end();