Skip to content

Commit

Permalink
[3.13] gh-120524: Avoid a Race On _PyRuntime.types.managed_static.typ…
Browse files Browse the repository at this point in the history
…es[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 2c66318, AKA gh-120529)

Co-authored-by: Eric Snow <[email protected]>
  • Loading branch information
miss-islington and ericsnowcurrently authored Jun 17, 2024
1 parent 396f8b0 commit 71ad34d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
1 change: 0 additions & 1 deletion Lib/test/test_interpreters/test_stress.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
6 changes: 4 additions & 2 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 71ad34d

Please sign in to comment.