mirror of
https://github.com/nodejs/node.git
synced 2026-11-29 14:27:57 -05:00
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>
This commit is contained in:
1 parent
4da7a4baaf
commit
d2dbf592ec
2 files changed
+86
-1
No files matched your search
@@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -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(),
|
||||
});
|
||||
Reference in new issue
Block a user