aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2024-06-14 16:05:51 -0600
committerMichał Górny <mgorny@gentoo.org>2024-06-15 07:45:11 +0200
commit19755e18457bc8f16e119bc5366a2f8632ff647c (patch)
tree599c52f3f0122e11f9c82e1dc39af4273e629a98
parent[3.13] gh-120161: Fix a Crash in the _datetime Module (gh-120518) (diff)
downloadcpython-gentoo-3.13.0b2_p2.tar.gz
cpython-gentoo-3.13.0b2_p2.tar.bz2
cpython-gentoo-3.13.0b2_p2.zip
Avoid a race on _PyRuntime.types.managed_static.types[i].interp_count.gentoo-3.13.0b2_p2
Closes: https://github.com/python/cpython/pull/120529
-rw-r--r--Include/internal/pycore_typeobject.h1
-rw-r--r--Objects/typeobject.c4
2 files changed, 5 insertions, 0 deletions
diff --git a/Include/internal/pycore_typeobject.h b/Include/internal/pycore_typeobject.h
index 05f125f928c..e1d9e109f77 100644
--- a/Include/internal/pycore_typeobject.h
+++ b/Include/internal/pycore_typeobject.h
@@ -31,6 +31,7 @@ struct _types_runtime_state {
unsigned int next_version_tag;
struct {
+ PyMutex mutex;
struct {
PyTypeObject *type;
int64_t interp_count;
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index f55b77a6e54..44ca7211fba 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -244,9 +244,11 @@ managed_static_type_state_init(PyInterpreterState *interp, PyTypeObject *self,
? index
: index + _Py_MAX_MANAGED_STATIC_BUILTIN_TYPES;
+ PyMutex_Lock(&_PyRuntime.types.managed_static.mutex);
assert((initial == 1) ==
(_PyRuntime.types.managed_static.types[full_index].interp_count == 0));
_PyRuntime.types.managed_static.types[full_index].interp_count += 1;
+ PyMutex_Unlock(&_PyRuntime.types.managed_static.mutex);
if (initial) {
assert(_PyRuntime.types.managed_static.types[full_index].type == NULL);
@@ -300,7 +302,9 @@ managed_static_type_state_clear(PyInterpreterState *interp, PyTypeObject *self,
state->type = NULL;
assert(state->tp_weaklist == NULL); // It was already cleared out.
+ PyMutex_Lock(&_PyRuntime.types.managed_static.mutex);
_PyRuntime.types.managed_static.types[full_index].interp_count -= 1;
+ PyMutex_Unlock(&_PyRuntime.types.managed_static.mutex);
if (final) {
assert(!_PyRuntime.types.managed_static.types[full_index].interp_count);
_PyRuntime.types.managed_static.types[full_index].type = NULL;