Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a concurrency bug in iterate(::Dict) #44534

Merged
merged 4 commits into from
Mar 12, 2022

Conversation

tkf
Copy link
Member

@tkf tkf commented Mar 9, 2022

fix #35835

This patch simply avoids mutating the Dict struct in iterate.

An alternative (probably correct) fix is

diff --git a/base/dict.jl b/base/dict.jl
index 12ff26fbeb..c98ae28947 100644
--- a/base/dict.jl
+++ b/base/dict.jl
@@ -696,7 +696,7 @@ end
 function skip_deleted_floor!(h::Dict)
     idx = skip_deleted(h, h.idxfloor)
     if idx != 0
-        h.idxfloor = idx
+        @atomic :monotonic h.idxfloor = idx
     end
     idx
 end

but I'm posting a simpler approach since I think it makes keeping Dict implementation correct hard and drastically decreases the number of maintainers who can reason about Dict code.

@tkf tkf requested a review from vtjnash March 9, 2022 05:50
@tkf tkf added collections Data structures holding multiple items, e.g. sets multithreading Base.Threads and related functionality labels Mar 9, 2022
Copy link
Member

@vtjnash vtjnash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the main purpose is to make pop! work in O(1) time, so we do not need it here.

@tkf tkf added the merge me PR is reviewed. Merge when all tests are passing label Mar 10, 2022
@DilumAluthge DilumAluthge merged commit 6be86a3 into JuliaLang:master Mar 12, 2022
@tkf tkf deleted the dict-iterate branch March 12, 2022 01:11
@giordano giordano removed the merge me PR is reviewed. Merge when all tests are passing label Mar 12, 2022
@IanButterworth IanButterworth added backport 1.6 Change should be backported to release-1.6 backport 1.7 backport 1.8 Change should be backported to release-1.8 labels Apr 28, 2022
KristofferC pushed a commit that referenced this pull request May 16, 2022
KristofferC pushed a commit that referenced this pull request May 23, 2022
KristofferC pushed a commit that referenced this pull request May 23, 2022
@KristofferC KristofferC removed the backport 1.8 Change should be backported to release-1.8 label May 28, 2022
@KristofferC KristofferC removed the backport 1.6 Change should be backported to release-1.6 label Jul 6, 2022
KristofferC pushed a commit that referenced this pull request Dec 21, 2022
staticfloat pushed a commit that referenced this pull request Dec 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
collections Data structures holding multiple items, e.g. sets multithreading Base.Threads and related functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Data-race in iterate(::Dict)?
7 participants