Make Iterable.flatten stack safe across empty iterables - #6944
Conversation
|
There was a problem hiding this comment.
Reviewed changes
- Added a regression test in
packages/effect/test/Iterable.test.tsthat reproduces the stack overflow inIterable.flattenwhen iterating through 20,000 consecutive empty inner iterables before yielding the final[1].
ℹ️ This branch currently contains only the reproduction test. The non-recursive implementation fix in
packages/effect/src/Iterable.tsstill needs to land before merge, matching the handoff in the PR description. The test itself is a clean, focused regression spec and should not be weakened.
@v0 or keep the SHA fresh with Dependabot | Fix it ➔ | View workflow run | Using Kimi K2 (free via Pullfrog for OSS) | 𝕏

Summary
A finite iterable containing many consecutive empty inner iterables can throw RangeError before yielding a later value or completing.
Important
This PR starts with focused failing reproduction tests. Add the implementation fix to this same branch; CI is expected to fail until that fix is included.
flatten overflows on a long run of empty iterables
Module:
IterableAudit ID:
core-g-r-iterable-flatten-empty-run-stack-overflowSeverity / confidence: medium / high
What happens
A finite iterable containing many consecutive empty inner iterables can throw RangeError before yielding a later value or completing.
Why it happens
When an inner iterator is exhausted, next clears it and recursively calls itself. Every consecutive empty iterable consumes another JavaScript stack frame, so 20,000 empty iterables overflow before the recursion reaches a later value.
Expected behavior
flatten lazily flattens arbitrary iterable inputs, including empty inner iterables, without iteration depth growing with the number of consecutive empty inputs.
Relevant implementation
These links and excerpts are pinned to audit base
c9b56ab507f224426ee8388dc450da447ec4715f.packages/effect/src/Iterable.ts:1498-1506packages/effect/src/Iterable.ts:1541-1562View problematic code at
packages/effect/src/Iterable.ts:1498-1506View exact lines on GitHub
View problematic code at
packages/effect/src/Iterable.ts:1541-1562View exact lines on GitHub
Reproduction
pnpm vitest run packages/effect/test/Iterable.test.ts -t "flatten is stack safe across empty iterables"Observed failure: The intended failure was reproduced with RangeError: Maximum call stack size exceeded.
Implementation handoff
The initial reproduction tests on this branch are the regression specification for the implementation fix that should follow in this PR.
pnpm vitest run packages/effect/test/Iterable.test.ts -t "flatten is stack safe across empty iterables"Audit provenance
c9b56ab507f224426ee8388dc450da447ec4715fc9b56ab507f224426ee8388dc450da447ec4715fcore-g-r-iterable-flatten-empty-run-stack-overflow