{{ message }}
gh-132070: Fix PyObject_Realloc thread-safety in free threaded Python - #143441
Merged
Conversation
…Python The PyObject header reference count fields must be initialized using atomic operations because they may be concurrently read by another thread (e.g., from _Py_TryIncref).
Contributor
Author
Yhg1s
approved these changes
Jan 5, 2026
| for (size_t i = 0; i != offset; i += sizeof(void*)) { | ||
| void *word; | ||
| memcpy(&word, (char*)ptr + i, sizeof(void*)); | ||
| _Py_atomic_store_ptr_relaxed((void**)((char*)newp + i), word); |
Member
There was a problem hiding this comment.
Is there a reason for the intermediate word? And why the memcpy instead of just a cast + deref + assignment? Can't this just be:
Suggested change
| _Py_atomic_store_ptr_relaxed((void**)((char*)newp + i), word); | |
| _Py_atomic_store_ptr_relaxed((void**)((char*)newp + i), (void *)((char *)ptr + 1)); |
(If it can't be, it probably deserves a comment.)
Contributor
Author
There was a problem hiding this comment.
My thinking was that the memcpy avoids accessing data the pointers of an incorrect size or strict aliasing issues. For example, we read ob_flags, ob_mutex, ob_gc_bits, and ob_ref_local together as a 64-bit load (on 64-bit systems).
I guess there's still the same issue when we write the data with _Py_atomic_store_ptr_relaxed, so maybe it doesn't matter.
From what I can tell, UBSan and ASan don't check for this kind of thing.
Co-authored-by: T. Wouters <thomas@python.org>
Yhg1s
approved these changes
Jan 6, 2026
thunder-coding
pushed a commit
to thunder-coding/cpython
that referenced
this pull request
Feb 15, 2026
…Python (pythongh-143441) The PyObject header reference count fields must be initialized using atomic operations because they may be concurrently read by another thread (e.g., from `_Py_TryIncref`).
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.

The PyObject header reference count fields must be initialized using atomic operations because they may be concurrently read by another thread (e.g., from
_Py_TryIncref).