summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2024-06-17 22:13:40 (GMT)
committerGitHub <noreply@github.com>2024-06-17 22:13:40 (GMT)
commit71ad34d2190a1f16ac7dbc70b8bce8c6b2549109 (patch)
tree2cf5b04b86885324725bb7b2efa89f64d890c4be /Objects
parent396f8b0b98441344e1d3223a4075e5e342e0c2df (diff)
downloadcpython-71ad34d2190a1f16ac7dbc70b8bce8c6b2549109.zip
cpython-71ad34d2190a1f16ac7dbc70b8bce8c6b2549109.tar.gz
cpython-71ad34d2190a1f16ac7dbc70b8bce8c6b2549109.tar.bz2
[3.13] gh-120524: Avoid a Race On _PyRuntime.types.managed_static.types[i].interp_count (gh-120657)
gh-120182 added new global state (interp_count), but didn't add thread-safety for it. This change eliminates the possible race. (cherry picked from commit 2c66318cdc0545da37e7046533dfe74bde129d91, AKA gh-120529) Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
Diffstat (limited to 'Objects')
-rw-r--r--Objects/typeobject.c6
1 files changed, 4 insertions, 2 deletions
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 1123ef6..5c490e8 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -246,7 +246,8 @@ managed_static_type_state_init(PyInterpreterState *interp, PyTypeObject *self,
assert((initial == 1) ==
(_PyRuntime.types.managed_static.types[full_index].interp_count == 0));
- _PyRuntime.types.managed_static.types[full_index].interp_count += 1;
+ (void)_Py_atomic_add_int64(
+ &_PyRuntime.types.managed_static.types[full_index].interp_count, 1);
if (initial) {
assert(_PyRuntime.types.managed_static.types[full_index].type == NULL);
@@ -300,7 +301,8 @@ managed_static_type_state_clear(PyInterpreterState *interp, PyTypeObject *self,
state->type = NULL;
assert(state->tp_weaklist == NULL); // It was already cleared out.
- _PyRuntime.types.managed_static.types[full_index].interp_count -= 1;
+ (void)_Py_atomic_add_int64(
+ &_PyRuntime.types.managed_static.types[full_index].interp_count, -1);
if (final) {
assert(!_PyRuntime.types.managed_static.types[full_index].interp_count);
_PyRuntime.types.managed_static.types[full_index].type = NULL;