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

gh-112075: Fix dict thread safety issues #119288

Merged
merged 2 commits into from
May 24, 2024
Merged

Conversation

Kronuz
Copy link
Contributor

@Kronuz Kronuz commented May 21, 2024

Summary:

For dicts, there are a myriad of variables that are atomics. i.e.:

mp->ma_used
mp->ma_keys
mp->ma_values
entry_ptr->me_value
entry_ptr->me_key
entry_ptr->me_hash
di->di_pos
di->len
values->values[ix]
values->capacity
values->size
keys->dk_nentries
keys->dk_usable
op->ob_ref_shared

All of these can be accessed in write or read mode. When these are being accessed in write mode, regardless of the status of the dictionary lock, the write should be an atomic store. All reads must be atomic loads if the lock is not held, or can be non-atomic reads otherwise.

This diff goes through all of the ma_used use cases and makes sure this holds true.


Test Plan:

./configure --disable-gil --with-thread-sanitizer --with-pydebug
make -j
env TSAN_OPTIONS=suppressions=Tools/tsan/suppressions_free_threading.txt ./python -m test --tsan -j4

All tests passing, no related TSAN issues.


@Kronuz Kronuz marked this pull request as ready for review May 21, 2024 17:37
Objects/dictobject.c Outdated Show resolved Hide resolved
Copy link
Contributor

@DinoV DinoV left a comment

Choose a reason for hiding this comment

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

LGTM!

@DinoV DinoV merged commit f0ed186 into python:main May 24, 2024
36 checks passed
@colesbury
Copy link
Contributor

I think we should backport this to 3.13

@colesbury colesbury added the needs backport to 3.13 bugs and security fixes label Jul 9, 2024
@miss-islington-app
Copy link

Thanks @Kronuz for the PR, and @DinoV for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jul 9, 2024
Fix dict thread safety issues
(cherry picked from commit f0ed186)

Co-authored-by: Germán Méndez Bravo <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Jul 9, 2024

GH-121545 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Jul 9, 2024
colesbury pushed a commit that referenced this pull request Jul 9, 2024
(cherry picked from commit f0ed186)

Co-authored-by: Germán Méndez Bravo <[email protected]>
@bedevere-bot
Copy link

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot AMD64 Windows11 Bigmem 3.13 has failed when building commit 2f15383.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/all/#builders/1463/builds/226) and take a look at the build logs.
  4. Check if the failure is related to this commit (2f15383) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/all/#builders/1463/builds/226

Summary of the results of the build (if available):

Click to see traceback logs
remote: Enumerating objects: 4, done.        
remote: Counting objects:  25% (1/4)        
remote: Counting objects:  50% (2/4)        
remote: Counting objects:  75% (3/4)        
remote: Counting objects: 100% (4/4)        
remote: Counting objects: 100% (4/4), done.        
remote: Total 4 (delta 3), reused 3 (delta 3), pack-reused 0        
From https://github.com/python/cpython
 * branch                  3.13       -> FETCH_HEAD
Note: switching to '2f1538316138425b453d698adac031d4093fa885'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 2f15383161 [3.13] gh-112075: Fix dict thread safety issues (GH-119288) (#121545)
Switched to and reset branch '3.13'

estyxx pushed a commit to estyxx/cpython that referenced this pull request Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants