Skip to content

Commit 57f45ee

Browse files
authored
gh-128759: Fix accesses to tp_version_tag. (GH-129750)
We should use a relaxed atomic load in the free threading build in `PyType_Modified()` because that's called without the type lock held. It's not necessary to use atomics in `type_modified_unlocked()`. We should also use `FT_ATOMIC_STORE_UINT_RELAXED()` instead of the `UINT32` variant because `tp_version_tag` is declared as `unsigned int`.
1 parent 3cf68cd commit 57f45ee

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

Objects/typeobject.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,7 @@ set_version_unlocked(PyTypeObject *tp, unsigned int version)
10101010
_Py_atomic_add_uint16(&tp->tp_versions_used, 1);
10111011
}
10121012
#endif
1013-
FT_ATOMIC_STORE_UINT32_RELAXED(tp->tp_version_tag, version);
1013+
FT_ATOMIC_STORE_UINT_RELAXED(tp->tp_version_tag, version);
10141014
#ifndef Py_GIL_DISABLED
10151015
if (version != 0) {
10161016
PyTypeObject **slot =
@@ -1039,7 +1039,8 @@ type_modified_unlocked(PyTypeObject *type)
10391039
We don't assign new version tags eagerly, but only as
10401040
needed.
10411041
*/
1042-
if (FT_ATOMIC_LOAD_UINT_RELAXED(type->tp_version_tag) == 0) {
1042+
ASSERT_TYPE_LOCK_HELD();
1043+
if (type->tp_version_tag == 0) {
10431044
return;
10441045
}
10451046
// Cannot modify static builtin types.
@@ -1094,7 +1095,7 @@ void
10941095
PyType_Modified(PyTypeObject *type)
10951096
{
10961097
// Quick check without the lock held
1097-
if (type->tp_version_tag == 0) {
1098+
if (FT_ATOMIC_LOAD_UINT_RELAXED(type->tp_version_tag) == 0) {
10981099
return;
10991100
}
11001101

0 commit comments

Comments
 (0)