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.
This commit is contained in:
Eric Snow 2024-06-17 15:16:00 -04:00 committed by GitHub
parent e73c42e15c
commit 2c66318cdc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 4 additions and 3 deletions

View File

@ -22,7 +22,6 @@ class StressTests(TestBase):
interp = interpreters.create() interp = interpreters.create()
alive.append(interp) alive.append(interp)
@unittest.skip('(temporary) gh-120524: there is a race that needs fixing')
@support.requires_resource('cpu') @support.requires_resource('cpu')
def test_create_many_threaded(self): def test_create_many_threaded(self):
alive = [] alive = []

View File

@ -246,7 +246,8 @@ managed_static_type_state_init(PyInterpreterState *interp, PyTypeObject *self,
assert((initial == 1) == assert((initial == 1) ==
(_PyRuntime.types.managed_static.types[full_index].interp_count == 0)); (_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) { if (initial) {
assert(_PyRuntime.types.managed_static.types[full_index].type == NULL); 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; state->type = NULL;
assert(state->tp_weaklist == NULL); // It was already cleared out. 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) { if (final) {
assert(!_PyRuntime.types.managed_static.types[full_index].interp_count); assert(!_PyRuntime.types.managed_static.types[full_index].interp_count);
_PyRuntime.types.managed_static.types[full_index].type = NULL; _PyRuntime.types.managed_static.types[full_index].type = NULL;