gh-117657: Use an atomic store to set type flags. by nascheme · Pull Request #127588 · python/cpython · GitHub
Skip to content

gh-117657: Use an atomic store to set type flags. - #127588

Merged
nascheme merged 4 commits into
python:mainfrom
nascheme:gh-117657-tp-flags-set-atomic
Feb 26, 2025
Merged

gh-117657: Use an atomic store to set type flags.#127588
nascheme merged 4 commits into
python:mainfrom
nascheme:gh-117657-tp-flags-set-atomic

Conversation

@nascheme

@nascheme nascheme commented Dec 4, 2024

Copy link
Copy Markdown
Member

The PyType_HasFeature() function reads the flags with a relaxed atomic load and without holding the type lock. To avoid data races, use atomic stores to update the type flags.

This change eliminates some warnings from TSAN. For non-free-threaded builds, there should be no performance impact.

The `PyType_HasFeature()` function reads the flags with a relaxed atomic
load and without holding the type lock.  To avoid data races, use atomic
stores if `PyType_Ready()` has already been called.
Comment thread Objects/typeobject.c

@colesbury colesbury left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Overall, looks good to me. I think type_set_flags can be simplified and I have a question below about locking.

Comment thread Objects/typeobject.c Outdated
Comment thread Objects/typeobject.c
Just use FT_ATOMIC_STORE_ULONG_RELAXED() always, not worth the extra
code complexity.
This helps ensure that type flag setting is done in a free-threading
safe way. Fix a few cases were flags were set without holding the type
lock. Use `type_modified_unlocked()` in a couple places where the lock
is already head.
@nascheme

nascheme commented Dec 4, 2024

Copy link
Copy Markdown
Member Author

@nascheme

nascheme commented Dec 4, 2024

Copy link
Copy Markdown
Member Author

@nascheme
nascheme merged commit baae9cb into python:main Feb 26, 2025
@nascheme
nascheme deleted the gh-117657-tp-flags-set-atomic branch February 28, 2025 04:36
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.

3 participants