https://github.com/python/cpython/commit/71ad34d2190a1f16ac7dbc70b8bce8c6b2549109
commit: 71ad34d2190a1f16ac7dbc70b8bce8c6b2549109
branch: 3.13
author: Miss Islington (bot) <31488909+miss-isling...@users.noreply.github.com>
committer: ericsnowcurrently <ericsnowcurren...@gmail.com>
date: 2024-06-17T16:13:40-06:00
summary:

[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 <ericsnowcurren...@gmail.com>

files:
M Lib/test/test_interpreters/test_stress.py
M Objects/typeobject.c

diff --git a/Lib/test/test_interpreters/test_stress.py 
b/Lib/test/test_interpreters/test_stress.py
index 40d2d77a7b9d3e..e400535b2a0e4e 100644
--- a/Lib/test/test_interpreters/test_stress.py
+++ b/Lib/test/test_interpreters/test_stress.py
@@ -22,7 +22,6 @@ def test_create_many_sequential(self):
             interp = interpreters.create()
             alive.append(interp)
 
-    @unittest.skip('(temporary) gh-120524: there is a race that needs fixing')
     @support.requires_resource('cpu')
     def test_create_many_threaded(self):
         alive = []
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 1123ef6eb3d9b2..5c490e833739fc 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;

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to