Skip to content

Commit

Permalink
Add channel state checks to reduce exceptions
Browse files Browse the repository at this point in the history
In channel iteration and in the `@sync` race check.
  • Loading branch information
kpamnany committed Jul 8, 2022
1 parent 60219d6 commit 4084f7e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
18 changes: 11 additions & 7 deletions base/channels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -493,14 +493,18 @@ function show(io::IO, ::MIME"text/plain", c::Channel)
end

function iterate(c::Channel, state=nothing)
try
return (take!(c), nothing)
catch e
if isa(e, InvalidStateException) && e.state === :closed
return nothing
else
rethrow()
if isopen(c) || isready(c)
try
return (take!(c), nothing)
catch e
if isa(e, InvalidStateException) && e.state === :closed
return nothing
else
rethrow()
end
end
else
return nothing
end
end

Expand Down
24 changes: 13 additions & 11 deletions base/task.jl
Original file line number Diff line number Diff line change
Expand Up @@ -424,19 +424,21 @@ function sync_end(c::Channel{Any})
# Capture all waitable objects scheduled after the end of `@sync` and
# include them in the exception. This way, the user can check what was
# scheduled by examining at the exception object.
local racy
for r in c
if !@isdefined(racy)
racy = []
if isready(c)
local racy
for r in c
if !@isdefined(racy)
racy = []
end
push!(racy, r)
end
push!(racy, r)
end
if @isdefined(racy)
if !@isdefined(c_ex)
c_ex = CompositeException()
if @isdefined(racy)
if !@isdefined(c_ex)
c_ex = CompositeException()
end
# Since this is a clear programming error, show this exception first:
pushfirst!(c_ex, ScheduledAfterSyncException(racy))
end
# Since this is a clear programming error, show this exception first:
pushfirst!(c_ex, ScheduledAfterSyncException(racy))
end

if @isdefined(c_ex)
Expand Down

0 comments on commit 4084f7e

Please sign in to comment.