Skip to content

Commit

Permalink
stream: fix deadlock when pipeing to full sink
Browse files Browse the repository at this point in the history
When piping a paused Readable to a full Writable we didn't
register a drain listener which cause the src to never
resume.

Refs: nodejs#48666
PR-URL: nodejs#48691
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
ronag authored and Ceres6 committed Aug 14, 2023
1 parent a863354 commit 661bf65
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
4 changes: 1 addition & 3 deletions lib/internal/streams/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -833,9 +833,7 @@ Readable.prototype.pipe = function(dest, pipeOpts) {
// Start the flow if it hasn't been started already.

if (dest.writableNeedDrain === true) {
if (state.flowing) {
pause();
}
pause();
} else if (!state.flowing) {
debug('pipe resume');
src.resume();
Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-stream-pipe-deadlock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

const common = require('../common');
const { Readable, Writable } = require('stream');

// https://github.com/nodejs/node/issues/48666
(async () => {
// Prepare src that is internally ended, with buffered data pending
const src = new Readable({ read() {} });
src.push(Buffer.alloc(100));
src.push(null);
src.pause();

// Give it time to settle
await new Promise((resolve) => setImmediate(resolve));

const dst = new Writable({
highWaterMark: 1000,
write(buf, enc, cb) {
process.nextTick(cb);
}
});

dst.write(Buffer.alloc(1000)); // Fill write buffer
dst.on('finish', common.mustCall());
src.pipe(dst);
})().then(common.mustCall());

0 comments on commit 661bf65

Please sign in to comment.