Skip to content

Commit

Permalink
stream: fix pipeline calling end on destination more than once
Browse files Browse the repository at this point in the history
Fixes: #42866
PR-URL: #46226
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Robert Nagy <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Minwoo Jung <[email protected]>
  • Loading branch information
debadree25 authored and RafaelGSS committed Jan 20, 2023
1 parent 4c1c20f commit 1869559
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/internal/streams/pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ function pipe(src, dst, finish, { end }) {
}
});

src.pipe(dst, { end });
src.pipe(dst, { end: false }); // If end is true we already will have a listener to end dst.

if (end) {
// Compat. Before node v10.12.0 stdio used to throw an error so
Expand Down
35 changes: 35 additions & 0 deletions test/parallel/test-stream-pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -1556,3 +1556,38 @@ const tsp = require('timers/promises');
})
);
}

{
class CustomReadable extends Readable {
_read() {
this.push('asd');
this.push(null);
}
}

class CustomWritable extends Writable {
constructor() {
super();
this.endCount = 0;
this.str = '';
}

_write(chunk, enc, cb) {
this.str += chunk;
cb();
}

end() {
this.endCount += 1;
super.end();
}
}

const readable = new CustomReadable();
const writable = new CustomWritable();

pipeline(readable, writable, common.mustSucceed(() => {
assert.strictEqual(writable.str, 'asd');
assert.strictEqual(writable.endCount, 1);
}));
}

0 comments on commit 1869559

Please sign in to comment.