From db6a98ea911064385ea4e0392322b818050d0f2f Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Sun, 28 Jun 2020 20:58:27 +0200 Subject: [PATCH 1/2] stream: don't read when paused Readable should not read when paused. Noticed while working on further streamifying net with _construct. --- lib/_stream_readable.js | 2 +- test/parallel/test-stream-construct.js | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index 952f2d75b84839..889c3123a63f60 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -633,7 +633,7 @@ function maybeReadMore_(stream, state) { // called push() with new data. In this case we skip performing more // read()s. The execution ends in this method again after the _read() ends // up calling push() with more data. - while (!state.reading && !state.ended && + while (!state.reading && !state.ended && state[kPaused] !== true && (state.length < state.highWaterMark || (state.flowing && state.length === 0))) { const len = state.length; diff --git a/test/parallel/test-stream-construct.js b/test/parallel/test-stream-construct.js index a3e2b4d2e3e8bd..fee27f44006c2d 100644 --- a/test/parallel/test-stream-construct.js +++ b/test/parallel/test-stream-construct.js @@ -242,3 +242,14 @@ testDestroy((opts) => new Writable({ construct: common.mustCall() }); } + +{ + // Readable paused + const readable = new Readable({ + construct: common.mustCall((callback) => { + readable.pause(); + callback(); + }), + read: common.mustNotCall() + }); +} From 8da065a9546fa8168942532f26e28154dd73d554 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Sun, 28 Jun 2020 21:46:52 +0200 Subject: [PATCH 2/2] fixup --- lib/_stream_readable.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index 889c3123a63f60..e34e13efbb05bc 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -213,7 +213,9 @@ function Readable(options) { Stream.call(this, options); destroyImpl.construct(this, () => { - maybeReadMore(this, this._readableState); + if (!this.isPaused()) { + maybeReadMore(this, this._readableState); + } }); } @@ -633,7 +635,7 @@ function maybeReadMore_(stream, state) { // called push() with new data. In this case we skip performing more // read()s. The execution ends in this method again after the _read() ends // up calling push() with more data. - while (!state.reading && !state.ended && state[kPaused] !== true && + while (!state.reading && !state.ended && (state.length < state.highWaterMark || (state.flowing && state.length === 0))) { const len = state.length;