Skip to content

Raise in Stream.cycle when enumerable reduce call yields no elements#15344

Merged
josevalim merged 3 commits intoelixir-lang:mainfrom
lukaszsamson:ls-empty-stream-reduce
May 6, 2026
Merged

Raise in Stream.cycle when enumerable reduce call yields no elements#15344
josevalim merged 3 commits intoelixir-lang:mainfrom
lukaszsamson:ls-empty-stream-reduce

Conversation

@lukaszsamson
Copy link
Copy Markdown
Contributor

This PR reuses already existing check_cycle_first_element. I'm opening this as a draft. It fixes the infinite loop but I'm not convinced the approach is correct.

Alternatives:

  • raise RuntimeError instead of ArgumentError - argument was correct, halting happens later
  • refactor and pattern match on :halted directly in do_cycle

Fixes #15343

@josevalim
Copy link
Copy Markdown
Member

The fix looks correct to me!

raise RuntimeError instead of ArgumentError - argument was correct, halting happens later

Yes, let's also do this!


Also, the test can be simpler. You can likely use the process dictionary and no need to use tasks. If it takes too long (because of the loop), the test will timeout anyway, so you can just call Stream.cycle |> Enum.take directly and assert it raises!

@lukaszsamson lukaszsamson force-pushed the ls-empty-stream-reduce branch from 20b7fb8 to 4559989 Compare May 6, 2026 09:15
@lukaszsamson lukaszsamson force-pushed the ls-empty-stream-reduce branch from d8d03ae to 7f58859 Compare May 6, 2026 09:30
@lukaszsamson lukaszsamson marked this pull request as ready for review May 6, 2026 09:30
@josevalim josevalim merged commit c3f7e34 into elixir-lang:main May 6, 2026
15 checks passed
@josevalim
Copy link
Copy Markdown
Member

💚 💙 💜 💛 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Stream.cycle/1 infinite loop on halted enumerable

2 participants