diff --git a/base/channels.jl b/base/channels.jl index c84e2da112f85..1461f14285b5d 100644 --- a/base/channels.jl +++ b/base/channels.jl @@ -51,7 +51,6 @@ function fetch(c::Channel) end function take!(c::Channel) - !isopen(c) && !isready(c) && throw(closed_exception()) wait(c) v = shift!(c.data) notify(c.cond_put, nothing, false, false) # notify only one, since only one slot has become available for a put!. @@ -64,6 +63,7 @@ isready(c::Channel) = n_avail(c) > 0 function wait(c::Channel) while !isready(c) + !isopen(c) && throw(closed_exception()) wait(c.cond_take) end nothing diff --git a/test/parallel_exec.jl b/test/parallel_exec.jl index 4c5eb78c5cd73..343264e27cfee 100644 --- a/test/parallel_exec.jl +++ b/test/parallel_exec.jl @@ -538,6 +538,24 @@ function testcpt() end testcpt() +# Test multiple "for" loops waiting on the same channel which +# is closed after adding a few elements. +c=Channel() +results=[] +@sync begin + for i in 1:20 + @async for i in c + push!(results, i) + end + end + sleep(1.0) + for i in 1:5 + put!(c,i) + end + close(c) +end +@test sum(results) == 15 + @test_throws ArgumentError sleep(-1) @test_throws ArgumentError timedwait(()->false, 0.1, pollint=-0.5)