]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-120524: Avoid a Race On _PyRuntime.types.managed_static.types[i].interp_count...
authorEric Snow <ericsnowcurrently@gmail.com>
Mon, 17 Jun 2024 19:16:00 +0000 (15:16 -0400)
committerGitHub <noreply@github.com>
Mon, 17 Jun 2024 19:16:00 +0000 (13:16 -0600)
gh-120182 added new global state (interp_count), but didn't add thread-safety for it.  This change eliminates the possible race.

Lib/test/test_interpreters/test_stress.py
Objects/typeobject.c

index 40d2d77a7b9d3e2b19763ef6895a6327b5ede72a..e400535b2a0e4ef0f14f02f7f8e92df4135d263a 100644 (file)
@@ -22,7 +22,6 @@ class StressTests(TestBase):
             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 = []
index eb296414bb7befc089f00fa2d42cf80aa62ef3d2..958f42430c80a2fbc4b24c93bdbfc8a9218707be 100644 (file)
@@ -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;