Skip to content

Commit 4f09b54

Browse files
vtjnashKristofferC
authored and
KristofferC
committed
channels: fix memory ordering violation in iterate (#52407)
Channel `iterate` calls might miss trailing items without this patch. I have not seen proof of this reaching a failure, but we do appear to be missing this ordering specification in visual review. Should not make a difference to generated code on x86, which already has TSO guaranteed, but may alter optimizations and other CPUs with weaker memory orderings. (cherry picked from commit 856e112)
1 parent e94785f commit 4f09b54

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

base/channels.jl

+6-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,12 @@ function close(c::Channel, @nospecialize(excp::Exception))
211211
end
212212
nothing
213213
end
214-
isopen(c::Channel) = ((@atomic :monotonic c.state) === :open)
214+
215+
# Use acquire here to pair with release store in `close`, so that subsequent `isready` calls
216+
# are forced to see `isready == true` if they see `isopen == false`. This means users must
217+
# call `isopen` before `isready` if you are using the race-y APIs (or call `iterate`, which
218+
# does this right for you).
219+
isopen(c::Channel) = ((@atomic :acquire c.state) === :open)
215220

216221
"""
217222
bind(chnl::Channel, task::Task)

0 commit comments

Comments
 (0)