Skip to content

Commit dace55a

Browse files
committed
Add throw keyword argument in waitany
1 parent 0501a1a commit dace55a

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

base/task.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,10 +366,10 @@ function wait(t::Task)
366366
end
367367

368368
# Wait multiple tasks
369-
waitany(tasks) = _wait_multiple(tasks)
370-
waitall(tasks; failfast=false, throw=false) = _wait_multiple(tasks, true, failfast, throw)
369+
waitany(tasks; throw=false) = _wait_multiple(tasks, throw)
370+
waitall(tasks; failfast=false, throw=false) = _wait_multiple(tasks, throw, true, failfast)
371371

372-
function _wait_multiple(waiting_tasks, all=false, failfast=false, throwexc=false)
372+
function _wait_multiple(waiting_tasks, throwexc=false, all=false, failfast=false)
373373
tasks = Task[]
374374

375375
for t in waiting_tasks
@@ -409,7 +409,7 @@ function _wait_multiple(waiting_tasks, all=false, failfast=false, throwexc=false
409409
if nremaining == 0
410410
return tasks, Task[]
411411
elseif any(done_mask) && (!all || (failfast && exception))
412-
if throwexc && failfast && exception
412+
if throwexc && (!all || failfast) && exception
413413
exceptions = [t.exception for t in tasks[done_mask] if istaskfailed(t)]
414414
throw(CompositeException(exceptions))
415415
else

test/threads_exec.jl

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,15 +1218,28 @@ end
12181218

12191219
for tasks_type in (Vector{Task}, Set{Task}, Tuple{Task})
12201220
@testset "waitany" begin
1221-
tasks, event = create_tasks()
1222-
sleep(0.1)
1223-
done, pending = waitany(convert_tasks(tasks_type, tasks))
1224-
@test length(done) == 2
1225-
@test tasks[1] done
1226-
@test tasks[2] done
1227-
@test length(pending) == 1
1228-
@test tasks[3] pending
1229-
teardown(tasks, event)
1221+
@testset "no options" begin
1222+
tasks, event = create_tasks()
1223+
sleep(0.1)
1224+
done, pending = waitany(convert_tasks(tasks_type, tasks))
1225+
@test length(done) == 2
1226+
@test tasks[1] done
1227+
@test tasks[2] done
1228+
@test length(pending) == 1
1229+
@test tasks[3] pending
1230+
teardown(tasks, event)
1231+
end
1232+
1233+
@testset "throw=true" begin
1234+
tasks, event = create_tasks()
1235+
push!(tasks, Threads.@spawn error("Error"))
1236+
1237+
@test_throws CompositeException begin
1238+
waitany(convert_tasks(tasks_type, tasks); throw=true)
1239+
end
1240+
1241+
teardown(tasks, event)
1242+
end
12301243
end
12311244

12321245
@testset "waitall" begin

0 commit comments

Comments
 (0)