[3.13] gh-120198: Stop the world when setting __class__ on free-threaded build (#121591)

(cherry-picked from commit 3bfc9c8)
This commit is contained in:
Ken Jin 2024-07-12 19:35:53 +08:00 committed by GitHub
parent 15c875a57c
commit cd74ed0a71
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 65 additions and 58 deletions

View file

@ -6378,28 +6378,11 @@ differs:
return 0;
}
static int
object_set_class(PyObject *self, PyObject *value, void *closure)
object_set_class_world_stopped(PyObject *self, PyTypeObject *newto)
{
if (value == NULL) {
PyErr_SetString(PyExc_TypeError,
"can't delete __class__ attribute");
return -1;
}
if (!PyType_Check(value)) {
PyErr_Format(PyExc_TypeError,
"__class__ must be set to a class, not '%s' object",
Py_TYPE(value)->tp_name);
return -1;
}
PyTypeObject *newto = (PyTypeObject *)value;
if (PySys_Audit("object.__setattr__", "OsO",
self, "__class__", value) < 0) {
return -1;
}
PyTypeObject *oldto = Py_TYPE(self);
/* In versions of CPython prior to 3.5, the code in
@ -6465,39 +6448,66 @@ object_set_class(PyObject *self, PyObject *value, void *closure)
/* Changing the class will change the implicit dict keys,
* so we must materialize the dictionary first. */
if (oldto->tp_flags & Py_TPFLAGS_INLINE_VALUES) {
PyDictObject *dict = _PyObject_MaterializeManagedDict(self);
PyDictObject *dict = _PyObject_GetManagedDict(self);
if (dict == NULL) {
return -1;
dict = _PyObject_MaterializeManagedDict_LockHeld(self);
if (dict == NULL) {
return -1;
}
}
bool error = false;
Py_BEGIN_CRITICAL_SECTION2(self, dict);
// If we raced after materialization and replaced the dict
// then the materialized dict should no longer have the
// inline values in which case detach is a nop.
assert(_PyObject_GetManagedDict(self) == dict ||
dict->ma_values != _PyObject_InlineValues(self));
assert(_PyObject_GetManagedDict(self) == dict);
if (_PyDict_DetachFromObject(dict, self) < 0) {
error = true;
}
Py_END_CRITICAL_SECTION2();
if (error) {
return -1;
}
}
if (newto->tp_flags & Py_TPFLAGS_HEAPTYPE) {
Py_INCREF(newto);
}
Py_BEGIN_CRITICAL_SECTION(self);
// The real Py_TYPE(self) (`oldto`) may have changed from
// underneath us in another thread, so we re-fetch it here.
oldto = Py_TYPE(self);
Py_SET_TYPE(self, newto);
Py_END_CRITICAL_SECTION();
return 0;
}
else {
return -1;
}
}
static int
object_set_class(PyObject *self, PyObject *value, void *closure)
{
if (value == NULL) {
PyErr_SetString(PyExc_TypeError,
"can't delete __class__ attribute");
return -1;
}
if (!PyType_Check(value)) {
PyErr_Format(PyExc_TypeError,
"__class__ must be set to a class, not '%s' object",
Py_TYPE(value)->tp_name);
return -1;
}
PyTypeObject *newto = (PyTypeObject *)value;
if (PySys_Audit("object.__setattr__", "OsO",
self, "__class__", value) < 0) {
return -1;
}
#ifdef Py_GIL_DISABLED
PyInterpreterState *interp = _PyInterpreterState_GET();
_PyEval_StopTheWorld(interp);
#endif
PyTypeObject *oldto = Py_TYPE(self);
int res = object_set_class_world_stopped(self, newto);
#ifdef Py_GIL_DISABLED
_PyEval_StartTheWorld(interp);
#endif
if (res == 0) {
if (oldto->tp_flags & Py_TPFLAGS_HEAPTYPE) {
Py_DECREF(oldto);
}
@ -6505,9 +6515,7 @@ object_set_class(PyObject *self, PyObject *value, void *closure)
RARE_EVENT_INC(set_class);
return 0;
}
else {
return -1;
}
return res;
}
static PyGetSetDef object_getsets[] = {