Skip to content

Commit 2c66318

Browse files
gh-120524: Avoid a Race On _PyRuntime.types.managed_static.types[i].interp_count (gh-120529)
gh-120182 added new global state (interp_count), but didn't add thread-safety for it. This change eliminates the possible race.
1 parent e73c42e commit 2c66318

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

Lib/test/test_interpreters/test_stress.py

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ def test_create_many_sequential(self):
2222
interp = interpreters.create()
2323
alive.append(interp)
2424

25-
@unittest.skip('(temporary) gh-120524: there is a race that needs fixing')
2625
@support.requires_resource('cpu')
2726
def test_create_many_threaded(self):
2827
alive = []

Objects/typeobject.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,8 @@ managed_static_type_state_init(PyInterpreterState *interp, PyTypeObject *self,
246246

247247
assert((initial == 1) ==
248248
(_PyRuntime.types.managed_static.types[full_index].interp_count == 0));
249-
_PyRuntime.types.managed_static.types[full_index].interp_count += 1;
249+
(void)_Py_atomic_add_int64(
250+
&_PyRuntime.types.managed_static.types[full_index].interp_count, 1);
250251

251252
if (initial) {
252253
assert(_PyRuntime.types.managed_static.types[full_index].type == NULL);
@@ -300,7 +301,8 @@ managed_static_type_state_clear(PyInterpreterState *interp, PyTypeObject *self,
300301
state->type = NULL;
301302
assert(state->tp_weaklist == NULL); // It was already cleared out.
302303

303-
_PyRuntime.types.managed_static.types[full_index].interp_count -= 1;
304+
(void)_Py_atomic_add_int64(
305+
&_PyRuntime.types.managed_static.types[full_index].interp_count, -1);
304306
if (final) {
305307
assert(!_PyRuntime.types.managed_static.types[full_index].interp_count);
306308
_PyRuntime.types.managed_static.types[full_index].type = NULL;

0 commit comments

Comments
 (0)