summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2024-06-12 13:37:26 (GMT)
committerGitHub <noreply@github.com>2024-06-12 13:37:26 (GMT)
commit91c4444d22a36119c83c9a21bfe0efe39d745086 (patch)
tree966fd48a18468834aa7a2dbd5c10d514a74f3599 /Objects
parent39825a7533ccf1aa0343d14fe88015db4ee6ef93 (diff)
downloadcpython-91c4444d22a36119c83c9a21bfe0efe39d745086.zip
cpython-91c4444d22a36119c83c9a21bfe0efe39d745086.tar.gz
cpython-91c4444d22a36119c83c9a21bfe0efe39d745086.tar.bz2
[3.13] gh-117657: Make Py_TYPE and Py_SET_TYPE thread safe (GH-120165) (GH-120403)
gh-117657: Make Py_TYPE and Py_SET_TYPE thread safe (GH-120165) (cherry picked from commit e16aed63f64b18a26859eff3de976ded373e66b8) Co-authored-by: Ken Jin <kenjin@python.org> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Nadeshiko Manju <me@manjusaka.me>
Diffstat (limited to 'Objects')
-rw-r--r--Objects/typeobject.c8
1 files changed, 7 insertions, 1 deletions
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 2d001b7..eec25b0 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -6474,9 +6474,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;