Skip to content

gh-133467: fix data race in type_set_name #137302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Lib/test/test_free_threading/test_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,20 @@ class ClassB(Base):
obj.__class__ = ClassB


def test_name_change(self):
class Foo:
pass

def writer():
for _ in range(1000):
Foo.__name__ = 'Bar'

def reader():
for _ in range(1000):
Foo.__name__

self.run_one(writer, reader)

def run_one(self, writer_func, reader_func):
barrier = threading.Barrier(NTHREADS)

Expand Down
10 changes: 8 additions & 2 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1535,9 +1535,13 @@ type_set_name(PyObject *tp, PyObject *value, void *Py_UNUSED(closure))
return -1;
}

PyInterpreterState *interp = _PyInterpreterState_GET();
_PyEval_StopTheWorld(interp);
type->tp_name = tp_name;
Py_SETREF(((PyHeapTypeObject*)type)->ht_name, Py_NewRef(value));

PyObject *old_name = ((PyHeapTypeObject*)type)->ht_name;
((PyHeapTypeObject*)type)->ht_name = Py_NewRef(value);
_PyEval_StartTheWorld(interp);
Py_DECREF(old_name);
return 0;
}

Expand Down Expand Up @@ -10706,9 +10710,11 @@ slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)

get = _PyType_LookupRef(tp, &_Py_ID(__get__));
if (get == NULL) {
#ifndef Py_GIL_DISABLED
/* Avoid further slowdowns */
if (tp->tp_descr_get == slot_tp_descr_get)
tp->tp_descr_get = NULL;
#endif
return Py_NewRef(self);
}
if (obj == NULL)
Expand Down
2 changes: 0 additions & 2 deletions Tools/tsan/suppressions_free_threading.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,5 @@ race:PyObject_Realloc

# gh-133467. Some of these could be hard to trigger.
race_top:_Py_slot_tp_getattr_hook
race_top:slot_tp_descr_get
race_top:type_set_name
race_top:set_tp_bases
race_top:type_set_bases_unlocked
Loading