Repository navigation
Description
What version of Effect is running?
[email protected], @effect/[email protected], on Node 24.21.0 and 26.11.1. Both loops cited below are unchanged on main.
What steps can reproduce the bug?
When a Readable's _read pushes synchronously, one pull of NodeStream.fromReadable reads the source until it ends. Such sources include Readable.from over an array or a synchronous generator, and the counting-stream example in Node's docs. Stream.take(1) reads the whole source, and no timer or timeout can run until the pull returns.
import * as NodeStream from "@effect/platform-node-shared/NodeStream"
import { Effect, Stream } from "effect"
import { Readable } from "node:stream"
let reads = 0
// _read pushes synchronously, like the "counting stream" in Node's docs
const counter = () =>
new Readable({
read() {
reads++
this.push(reads > 500_000 ? null : Buffer.from(String(reads)))
}
})
const run = async (name, stream) => {
reads = 0
const start = performance.now()
const exit = await stream.pipe(
Stream.take(1),
Stream.runCollect,
Effect.timeout("10 millis"),
Effect.exit,
Effect.runPromise
)
console.log(name, exit._tag, `${reads} reads`, `${Math.round(performance.now() - start)} ms`)
}
await run("fromReadable", NodeStream.fromReadable({ evaluate: counter }))
await run("fromAsyncIterable", Stream.fromAsyncIterable(counter(), (e) => e))fromReadable Success 500001 reads 134 ms
fromAsyncIterable Success 1 reads 1 ms
What is the expected behavior?
A pull returns what the Readable has buffered, bounded by its highWaterMark, as it does when _read pushes from setImmediate. Stream.take(1) returns within milliseconds without reading the rest of the source.
What do you see instead?
The first pull reads all 500,000 items to emit the first element, so the stream succeeds long after its 10 ms deadline.
With an endless source, the first pull never returns, for example with evaluate: () => Readable.from((function*() { for (let i = 0;; i++) yield i })()). Neither Effect.timeout nor a plain setTimeout callback runs, and the process aborts with JavaScript heap out of memory.
Additional information
After the first item, readableToPullUnsafe calls read() in a while (true) loop until it returns null (NodeStream.ts#L385-L390). fromDuplex and pipeThroughDuplex read through the same function.
Node calls _read from inside read() whenever the buffer is below highWaterMark, and _read may push synchronously (readable.js#L703-L750). So for such a source, read() returns null only when the source ends.
NodeSocket.fromDuplex drains the same way in readAvailable (NodeSocket.ts#L50-L63). One reader.pull over an endless Duplex whose _read pushes synchronously also runs out of memory.
This is separate from the removed bufferSize option (#7645): draining what is already buffered is fine; the problem is that each read() refills the buffer synchronously.
Failing test for NodeStream.test.ts. It fails on main with expected 100001 to be below 1000; with the push inside setImmediate, it passes:
it.effect("fromReadable reads a bounded amount per pull from a readable that refills synchronously", () =>
Effect.gen(function*() {
let reads = 0
const readable = new Readable({
objectMode: true,
read() {
reads++
this.push(reads > 100_000 ? null : reads)
}
})
const items = yield* NodeStream.fromReadable({ evaluate: () => readable }).pipe(
Stream.take(1),
Stream.runCollect
)
assert.deepStrictEqual(items, [1])
assert.isBelow(reads, 1_000)
}))
Activity