From 079f7fa419b2006b73cb4076e88f321a273ceead Mon Sep 17 00:00:00 2001 From: Mandar Joshi Date: Sat, 14 Nov 2026 22:32:45 +0530 Subject: [PATCH 1/2] Close zipped and cycled streams properly on errors Signed-off-by: Mandar Joshi --- lib/elixir/lib/stream.ex | 11 ++++- lib/elixir/lib/stream/reducers.ex | 34 +++++++++++----- lib/elixir/test/elixir/stream_test.exs | 56 ++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 10 deletions(-) diff --git a/lib/elixir/lib/stream.ex b/lib/elixir/lib/stream.ex index 321b19e3e7d..0cc68410cfa 100644 --- a/lib/elixir/lib/stream.ex +++ b/lib/elixir/lib/stream.ex @@ -1431,7 +1431,16 @@ defmodule Stream do defp do_cycle(reduce, inner_acc, cycle, {:cont, acc}, fun) do case reduce.({:cont, inner_acc}) do {:suspended, [element], new_reduce} -> - do_cycle(new_reduce, inner_acc, cycle, fun.(element, acc), fun) + next_acc = + try do + fun.(element, acc) + catch + kind, reason -> + new_reduce.({:halt, inner_acc}) + :erlang.raise(kind, reason, __STACKTRACE__) + end + + do_cycle(new_reduce, inner_acc, cycle, next_acc, fun) {_, [element]} -> do_cycle(cycle, [], cycle, fun.(element, acc), fun) diff --git a/lib/elixir/lib/stream/reducers.ex b/lib/elixir/lib/stream/reducers.ex index 28ae03a17f0..21e41c3107e 100644 --- a/lib/elixir/lib/stream/reducers.ex +++ b/lib/elixir/lib/stream/reducers.ex @@ -71,13 +71,7 @@ defmodule Stream.Reducers do end defp do_zip_enum(zips, {:cont, acc}, callback, zip_fun) do - try do - do_zip_next(zips, acc, callback, [], [], zip_fun) - catch - kind, reason -> - do_zip_close(zips) - :erlang.raise(kind, reason, __STACKTRACE__) - else + case do_zip_next(zips, acc, callback, [], [], zip_fun) do {:next, buffer, acc} -> do_zip_enum(buffer, acc, callback, zip_fun) @@ -101,7 +95,19 @@ defmodule Stream.Reducers do end defp do_zip_next([{fun, [], :cont} | zips], acc, callback, yielded_elems, buffer, zip_fun) do - case fun.({:cont, []}) do + # If the current stream fails, it is responsible for its own cleanup, + # so we only close the streams that were already advanced and the ones + # that were not advanced yet. + result = + try do + fun.({:cont, []}) + catch + kind, reason -> + do_zip_close(:lists.reverse(buffer, zips)) + :erlang.raise(kind, reason, __STACKTRACE__) + end + + case result do {:suspended, [elem | next_acc], fun} -> next_buffer = [{fun, next_acc, :cont} | buffer] do_zip_next(zips, acc, callback, [elem | yielded_elems], next_buffer, zip_fun) @@ -135,7 +141,17 @@ defmodule Stream.Reducers do # "yielded_elems" is a reversed list of results for the current iteration of # zipping. That is to say, the nth element from each of the enums being zipped. # It needs to be reversed and passed to the zipping function so it can do it's thing. - {:next, :lists.reverse(buffer), callback.(zip_fun.(:lists.reverse(yielded_elems)), acc)} + buffer = :lists.reverse(buffer) + + try do + callback.(zip_fun.(:lists.reverse(yielded_elems)), acc) + catch + kind, reason -> + do_zip_close(buffer) + :erlang.raise(kind, reason, __STACKTRACE__) + else + acc -> {:next, buffer, acc} + end end defp do_zip_close(zips) do diff --git a/lib/elixir/test/elixir/stream_test.exs b/lib/elixir/test/elixir/stream_test.exs index f2f0ef043f5..37ae7d6046a 100644 --- a/lib/elixir/test/elixir/stream_test.exs +++ b/lib/elixir/test/elixir/stream_test.exs @@ -1458,6 +1458,40 @@ defmodule StreamTest do assert Process.get(:stream_done) end + test "zip/1 closes the started streams on outer error" do + stream = Stream.zip([traced_resource(:a), traced_resource(:b)]) + + assert_raise RuntimeError, "oops", fn -> Enum.each(stream, fn _ -> raise "oops" end) end + assert stream_traces() == [start: :a, start: :b, after: {:a, 1}, after: {:b, 1}] + + assert catch_throw(Enum.each(stream, fn {x, _} -> if x == 2, do: throw(:oops) end)) == :oops + assert stream_traces() == [start: :a, start: :b, after: {:a, 3}, after: {:b, 3}] + end + + test "zip/1 closes the streams once on inner error" do + stream = Stream.zip([traced_resource(:a), traced_resource(:b, 2)]) + + assert_raise RuntimeError, "b failed", fn -> Enum.to_list(stream) end + assert stream_traces() == [start: :a, start: :b, after: {:b, 2}, after: {:a, 3}] + + stream = Stream.zip([stream, traced_resource(:c)]) + + assert_raise RuntimeError, "b failed", fn -> Enum.to_list(stream) end + + assert stream_traces() == + [start: :a, start: :b, start: :c, after: {:b, 2}, after: {:a, 3}, after: {:c, 2}] + end + + test "cycle/1 closes on outer error" do + stream = Stream.cycle(traced_resource(:a)) + + assert_raise RuntimeError, "oops", fn -> + Enum.each(stream, fn x -> if x == 1, do: raise("oops") end) + end + + assert stream_traces() == [start: :a, after: {:a, 2}] + end + test "with_index/2" do stream = Stream.with_index([1, 2, 3]) assert lazy?(stream) @@ -1496,6 +1530,28 @@ defmodule StreamTest do "#Stream<[enum: 1..10, funs: " <> _ = Stream.map(1..10, & &1) |> inspect() end + defp traced_resource(name, fail_at \\ nil) do + Stream.resource( + fn -> + send(self(), {:stream_trace, {:start, name}}) + 0 + end, + fn + ^fail_at -> raise "#{name} failed" + acc -> {[acc], acc + 1} + end, + fn acc -> send(self(), {:stream_trace, {:after, {name, acc}}}) end + ) + end + + defp stream_traces do + receive do + {:stream_trace, trace} -> [trace | stream_traces()] + after + 0 -> [] + end + end + defp lazy?(stream) do match?(%Stream{}, stream) or is_function(stream, 2) end From df7afcf46da170173710ef3a8e4d6d9d2383a3f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 14 Nov 2026 18:23:24 +0100 Subject: [PATCH 2/2] Apply suggestion from @josevalim --- lib/elixir/lib/stream/reducers.ex | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/lib/elixir/lib/stream/reducers.ex b/lib/elixir/lib/stream/reducers.ex index 21e41c3107e..5ecb51d3c19 100644 --- a/lib/elixir/lib/stream/reducers.ex +++ b/lib/elixir/lib/stream/reducers.ex @@ -98,16 +98,13 @@ defmodule Stream.Reducers do # If the current stream fails, it is responsible for its own cleanup, # so we only close the streams that were already advanced and the ones # that were not advanced yet. - result = - try do - fun.({:cont, []}) - catch - kind, reason -> - do_zip_close(:lists.reverse(buffer, zips)) - :erlang.raise(kind, reason, __STACKTRACE__) - end - - case result do + try do + fun.({:cont, []}) + catch + kind, reason -> + do_zip_close(:lists.reverse(buffer, zips)) + :erlang.raise(kind, reason, __STACKTRACE__) + else {:suspended, [elem | next_acc], fun} -> next_buffer = [{fun, next_acc, :cont} | buffer] do_zip_next(zips, acc, callback, [elem | yielded_elems], next_buffer, zip_fun)