Skip to content

Commit

Permalink
Fix bug in channels with multiple for loops on the same channel
Browse files Browse the repository at this point in the history
  • Loading branch information
amitmurthy committed Jul 9, 2016
1 parent 6d5eb28 commit 58a0af4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion base/channels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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!.
Expand All @@ -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
Expand Down
18 changes: 18 additions & 0 deletions test/parallel_exec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 58a0af4

Please sign in to comment.