[3.14] gh-133467: fix data race in type_set_name (GH-137302) (#137303)

gh-133467: fix data race in `type_set_name` (GH-137302)

Fix data race in `type_set_name` by assigning name under stop the world pause making it thread safe in free-threading.
(cherry picked from commit e99bc7fd44)

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
This commit is contained in:
Miss Islington (bot) 2025-10-07 20:18:23 +02:00 committed by GitHub
parent 070993b8ad
commit bf397e272c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 22 additions and 4 deletions

View file

@ -1434,9 +1434,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;
}
@ -10406,9 +10410,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)