Skip to content

Commit 4f634bd

Browse files
author
José Valim
committed
Support infinity timeout on yield_many, closes #7633
Signed-off-by: José Valim <[email protected]>
1 parent 8e1ebaf commit 4f634bd

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/elixir/lib/task.ex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,15 +648,19 @@ defmodule Task do
648648
@spec yield_many([t], timeout) :: [{t, {:ok, term} | {:exit, term} | nil}]
649649
def yield_many(tasks, timeout \\ 5000) do
650650
timeout_ref = make_ref()
651-
timer_ref = Process.send_after(self(), timeout_ref, timeout)
651+
652+
timer_ref =
653+
if timeout != :infinity do
654+
Process.send_after(self(), timeout_ref, timeout)
655+
end
652656

653657
try do
654658
yield_many(tasks, timeout_ref, :infinity)
655659
catch
656660
{:noconnection, reason} ->
657661
exit({reason, {__MODULE__, :yield_many, [tasks, timeout]}})
658662
after
659-
Process.cancel_timer(timer_ref)
663+
timer_ref && Process.cancel_timer(timer_ref)
660664
receive do: (^timeout_ref -> :ok), after: (0 -> :ok)
661665
end
662666
end

lib/elixir/test/elixir/task_test.exs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,19 @@ defmodule TaskTest do
323323
assert Task.yield_many([task1, task2, task3], 0) ==
324324
[{task1, {:ok, :result}}, {task2, nil}, {task3, {:exit, :normal}}]
325325
end
326+
327+
test "returns results on infinity timeout" do
328+
task1 = %Task{ref: make_ref(), owner: self()}
329+
task2 = %Task{ref: make_ref(), owner: self()}
330+
task3 = %Task{ref: make_ref(), owner: self()}
331+
332+
send(self(), {task1.ref, :result})
333+
send(self(), {task2.ref, :result})
334+
send(self(), {:DOWN, task3.ref, :process, self(), :normal})
335+
336+
assert Task.yield_many([task1, task2, task3], :infinity) ==
337+
[{task1, {:ok, :result}}, {task2, {:ok, :result}}, {task3, {:exit, :normal}}]
338+
end
326339
end
327340

328341
describe "shutdown/2" do

0 commit comments

Comments
 (0)