Skip to content

Commit

Permalink
stream: avoid destroying writable source
Browse files Browse the repository at this point in the history
User might still want to be able to use the writable side
of src. This is in the case where e.g. the Duplex input
is not directly connected to its output. Such a case could
happen when the Duplex is reading from a socket and then echos
the data back on the same socket.

Fixes: 4d93e10#commitcomment-37751035

Backport-PR-URL: #32211
  • Loading branch information
ronag committed Mar 11, 2020
1 parent 28cd36a commit a0f3d2b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/internal/streams/pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ function destroyer(stream, reading, writing, final, callback) {
eos(stream, { readable: reading, writable: writing }, (err) => {
if (destroyed) return;
destroyed = true;

if (!err && reading && !writing && stream.writable) {
return callback();
}

const readable = stream.readable || isRequest(stream);
if (err || !final || !readable) {
destroyImpl.destroyer(stream, err);
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-stream-pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -995,3 +995,19 @@ const { promisify } = require('util');
assert.strictEqual(res, '');
}));
}

{
// Might still want to be able to use the writable side
// of src. This is in the case where e.g. the Duplex input
// is not directly connected to its output. Such a case could
// happen when the Duplex is reading from a socket and then echos
// the data back on the same socket.
const src = new PassThrough();
assert.strictEqual(src.writable, true);
const dst = new PassThrough();
pipeline(src, dst, common.mustCall((err) => {
assert.strictEqual(src.writable, true);
assert.strictEqual(src.destroyed, false);
}));
src.push(null);
}

0 comments on commit a0f3d2b

Please sign in to comment.