Skip to content

gh-153290: Fix data race in BytesIO.__setstate__ installing __dict__#153376

Open
bhuvi27 wants to merge 1 commit into
python:mainfrom
bhuvi27:gh-153290-fix-bytesio-setstate-dict-race
Open

gh-153290: Fix data race in BytesIO.__setstate__ installing __dict__#153376
bhuvi27 wants to merge 1 commit into
python:mainfrom
bhuvi27:gh-153290-fix-bytesio-setstate-dict-race

Conversation

@bhuvi27

@bhuvi27 bhuvi27 commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Fixes #153290

On the free-threaded build, BytesIO.__setstate__() installed the instance
__dict__ with a plain pointer store, while the lock-free LOAD_ATTR method
fast path (_CHECK_ATTR_METHOD_LAZY_DICT) reads the same dict slot with an
atomic 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_RELEASE for the store, pairing it with the acquire
load, the same way ensure_nonmanaged_dict() in Objects/dictobject.c
handles non-managed dict slots. This is a no-op on the default build.

Verified with TSAN on a --disable-gil --with-pydebug build: the issue's
reproducer reports the race before the change and is clean after. Added a
regression test in test_free_threading/test_io.py.

…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.
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.

Data race between BytesIO.__setstate__ and lock-free LOAD_ATTR method

1 participant