gh-153290: Fix data race in BytesIO.__setstate__ installing __dict__#153376
Open
bhuvi27 wants to merge 1 commit into
Open
gh-153290: Fix data race in BytesIO.__setstate__ installing __dict__#153376bhuvi27 wants to merge 1 commit into
bhuvi27 wants to merge 1 commit into
Conversation
…ict__ On the free-threaded build, __setstate__() installed the instance dictionary with a plain store while the lock-free LOAD_ATTR method fast path reads the same dict slot with an atomic acquire load. Use a release store to pair with that acquire load, matching ensure_nonmanaged_dict() in Objects/dictobject.c.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #153290
On the free-threaded build,
BytesIO.__setstate__()installed the instance__dict__with a plain pointer store, while the lock-free LOAD_ATTR methodfast path (
_CHECK_ATTR_METHOD_LAZY_DICT) reads the same dict slot with anatomic acquire load. ThreadSanitizer reports a data race between the two, and
an unordered store could publish the dict pointer before its contents are
visible to the reading thread.
Use
FT_ATOMIC_STORE_PTR_RELEASEfor the store, pairing it with the acquireload, the same way
ensure_nonmanaged_dict()inObjects/dictobject.chandles non-managed dict slots. This is a no-op on the default build.
Verified with TSAN on a
--disable-gil --with-pydebugbuild: the issue'sreproducer reports the race before the change and is clean after. Added a
regression test in
test_free_threading/test_io.py.BytesIO.__setstate__and lock-free LOAD_ATTR method #153290