Repository navigation
Description
Version
v24.22.0, also on main (e8c279d)
Platform
Darwin 27.2.0 arm64
Subsystem
stream
What steps will reproduce the bug?
'use strict';
const { Readable } = require('node:stream');
const chunks = ['a', 'b'];
const stream = new Readable({
highWaterMark: 0,
construct(callback) {
setImmediate(callback);
},
read() {
this.push(chunks.length ? chunks.shift() : null);
},
});
let done = false;
stream.toArray().then((data) => {
done = true;
console.log('done', data.map(String));
});
process.on('exit', () => {
if (!done) console.log('toArray() never resolved');
});How often does it reproduce? Is there a required condition?
Always, when a Readable has construct() and highWaterMark: 0, and it is consumed with read() (toArray(), for await, a 'readable' listener). Sync and async construct() both hang. Flowing mode ('data') works.
What is the expected behavior? Why is that the expected behavior?
done [ 'a', 'b' ], the same as without construct(). With highWaterMark: 1 it resolves too.
What do you see instead?
toArray() never resolved. _read() is never called.
Additional information
A read() during construct() is refused (
node/lib/internal/streams/readable.js
Line 787 in e8c279d
| if ((state[kState] & (kReading | kEnded | kDestroyed | kErrored | kConstructed)) !== kConstructed) { |
needReadable set. When construction finishes, onConstructed only calls maybeReadMore() (node/lib/internal/streams/readable.js
Lines 338 to 342 in e8c279d
| ReadableState.prototype[kOnConstructed] = function onConstructed(stream) { | |
| if ((this[kState] & kNeedReadable) !== 0) { | |
| maybeReadMore(stream, this); | |
| } | |
| }; |
highWaterMark is 0 and the stream is not flowing (node/lib/internal/streams/readable.js
Lines 957 to 959 in e8c279d
| while ((state[kState] & (kReading | kEnded)) === 0 && | |
| (state.length < state.highWaterMark || | |
| ((state[kState] & kFlowing) !== 0 && state.length === 0))) { |
_read() again.
fs.ReadStream uses construct() too, so fs.createReadStream(file, { highWaterMark: 0 }).toArray() hangs for the same reason.
I found this while looking into #67667. I have a fix ready and will open a PR.
Activity
stream: retry read after construct when hwm is 0
stream: retry read after construct when hwm is 0