Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/elixir/lib/stream.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 22 additions & 9 deletions lib/elixir/lib/stream/reducers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -101,7 +95,16 @@ 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.
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)
Expand Down Expand Up @@ -135,7 +138,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
Expand Down
56 changes: 56 additions & 0 deletions lib/elixir/test/elixir/stream_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
Loading