Skip to content

Commit

Permalink
Avoid a race on _PyRuntime.types.managed_static.types[i].interp_count.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsnowcurrently authored and mgorny committed Jun 15, 2024
1 parent 08c3740 commit 19755e1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
1 change: 1 addition & 0 deletions Include/internal/pycore_typeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct _types_runtime_state {
unsigned int next_version_tag;

struct {
PyMutex mutex;
struct {
PyTypeObject *type;
int64_t interp_count;
Expand Down
4 changes: 4 additions & 0 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,11 @@ managed_static_type_state_init(PyInterpreterState *interp, PyTypeObject *self,
? index
: index + _Py_MAX_MANAGED_STATIC_BUILTIN_TYPES;

PyMutex_Lock(&_PyRuntime.types.managed_static.mutex);
assert((initial == 1) ==
(_PyRuntime.types.managed_static.types[full_index].interp_count == 0));
_PyRuntime.types.managed_static.types[full_index].interp_count += 1;
PyMutex_Unlock(&_PyRuntime.types.managed_static.mutex);

if (initial) {
assert(_PyRuntime.types.managed_static.types[full_index].type == NULL);
Expand Down Expand Up @@ -300,7 +302,9 @@ managed_static_type_state_clear(PyInterpreterState *interp, PyTypeObject *self,
state->type = NULL;
assert(state->tp_weaklist == NULL); // It was already cleared out.

PyMutex_Lock(&_PyRuntime.types.managed_static.mutex);
_PyRuntime.types.managed_static.types[full_index].interp_count -= 1;
PyMutex_Unlock(&_PyRuntime.types.managed_static.mutex);
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 19755e1

Please sign in to comment.