gh-117657: Make Py_TYPE and Py_SET_TYPE thread safe (GH-120165)

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Nadeshiko Manju <me@manjusaka.me>
This commit is contained in:
Ken Jin 2024-06-12 20:41:07 +08:00 committed by GitHub
parent ce3879bd45
commit e16aed63f6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 45 additions and 4 deletions

View file

@ -6633,9 +6633,15 @@ object_set_class(PyObject *self, PyObject *value, void *closure)
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);
if (oldto->tp_flags & Py_TPFLAGS_HEAPTYPE)
Py_END_CRITICAL_SECTION();
if (oldto->tp_flags & Py_TPFLAGS_HEAPTYPE) {
Py_DECREF(oldto);
}
RARE_EVENT_INC(set_class);
return 0;