Uh oh!
There was an error while loading. Please reload this page.
gh-117657: Use an atomic store to set type flags. - #127588
Conversation
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.
Uh oh!
There was an error while loading. Please reload this page.
colesbury
left a comment
There was a problem hiding this comment.
Overall, looks good to me. I think type_set_flags can be simplified and I have a question below about locking.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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
commented
Dec 4, 2024
If I add the assert, it fails like this: The code in When this runs, the type is already marked ready. The type lock is held inside Looking at The tp_version_tag is set with an atomic store (by A possible solution could be to change the |
nascheme
commented
Dec 4, 2024
Using |
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.