Commit d2dbf592ec2 for nodejs
commit d2dbf592ec294ae41e56268e098a17446e05d01c
Author: Hamza Kargin <hamzakargin@outlook.com>
Date: Mon Nov 23 21:25:34 2026 +0400
stream: retry read after construct when hwm is 0
A read() made while construct() is pending is refused and leaves
needReadable set. After construction, maybeReadMore() does not read
for a non-flowing stream with highWaterMark: 0, so _read() was never
called and consumers such as toArray() or for await waited forever.
Retry the refused read once construction finishes when highWaterMark
is 0.
Fixes: https://github.com/nodejs/node/issues/67668
Assisted-by: AI coding agents
Signed-off-by: Hamza Kargın <hamzakargin@outlook.com>
PR-URL: https://github.com/nodejs/node/pull/67670
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
diff --git a/lib/internal/streams/readable.js b/lib/internal/streams/readable.js
index 9bd7ea63691..d01e2f2e386 100644
--- a/lib/internal/streams/readable.js
+++ b/lib/internal/streams/readable.js
@@ -337,7 +337,10 @@ function ReadableState(options, stream, isDuplex) {
ReadableState.prototype[kOnConstructed] = function onConstructed(stream) {
if ((this[kState] & kNeedReadable) !== 0) {
- maybeReadMore(stream, this);
+ if (this.highWaterMark === 0)
+ process.nextTick(nReadingNextTick, stream);
+ else
+ maybeReadMore(stream, this);
}
};
diff --git a/test/parallel/test-stream-readable-construct-hwm-zero.js b/test/parallel/test-stream-readable-construct-hwm-zero.js
new file mode 100644
index 00000000000..ccabc366d70
--- /dev/null
+++ b/test/parallel/test-stream-readable-construct-hwm-zero.js
@@ -0,0 +1,82 @@
+'use strict';
+
+const common = require('../common');
+
+const assert = require('node:assert');
+const { Duplex, Readable } = require('node:stream');
+
+function create(construct, objectMode = false) {
+ const chunks = objectMode ? [{ a: 1 }, { b: 2 }] : ['a', 'b'];
+ return new Readable({
+ highWaterMark: 0,
+ objectMode,
+ construct,
+ read() {
+ this.push(chunks.length ? chunks.shift() : null);
+ },
+ });
+}
+
+create((cb) => setImmediate(cb)).toArray().then(common.mustCall((data) => {
+ assert.deepStrictEqual(data.map(String), ['a', 'b']);
+}));
+
+create((cb) => cb()).toArray().then(common.mustCall((data) => {
+ assert.deepStrictEqual(data.map(String), ['a', 'b']);
+}));
+
+create((cb) => setImmediate(cb), true).toArray().then(common.mustCall((data) => {
+ assert.deepStrictEqual(data, [{ a: 1 }, { b: 2 }]);
+}));
+
+(async () => {
+ const data = [];
+ for await (const chunk of create((cb) => setImmediate(cb))) {
+ data.push(String(chunk));
+ }
+ assert.deepStrictEqual(data, ['a', 'b']);
+})().then(common.mustCall());
+
+{
+ const stream = create((cb) => setImmediate(cb));
+ const data = [];
+ stream.on('readable', () => {
+ let chunk;
+ while ((chunk = stream.read()) !== null) data.push(String(chunk));
+ });
+ stream.on('end', common.mustCall(() => {
+ assert.deepStrictEqual(data, ['a', 'b']);
+ }));
+}
+
+{
+ const stream = create((cb) => setImmediate(cb));
+ const data = [];
+ stream.on('data', (chunk) => data.push(String(chunk)));
+ stream.on('end', common.mustCall(() => {
+ assert.deepStrictEqual(data, ['a', 'b']);
+ }));
+}
+
+{
+ const chunks = ['a', 'b'];
+ const duplex = new Duplex({
+ readableHighWaterMark: 0,
+ construct(cb) {
+ setImmediate(cb);
+ },
+ read() {
+ this.push(chunks.length ? chunks.shift() : null);
+ },
+ write: common.mustNotCall(),
+ });
+ duplex.toArray().then(common.mustCall((data) => {
+ assert.deepStrictEqual(data.map(String), ['a', 'b']);
+ }));
+}
+
+new Readable({
+ highWaterMark: 0,
+ construct: common.mustCall((cb) => setImmediate(cb)),
+ read: common.mustNotCall(),
+});