Skip to content

Commit 7f85a2b

Browse files
Avoid a race on _PyRuntime.types.managed_static.types[i].interp_count.
1 parent b2e71ff commit 7f85a2b

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

Include/internal/pycore_typeobject.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ struct _types_runtime_state {
3131
unsigned int next_version_tag;
3232

3333
struct {
34+
PyMutex mutex;
3435
struct {
3536
PyTypeObject *type;
3637
int64_t interp_count;

Objects/typeobject.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,11 @@ managed_static_type_state_init(PyInterpreterState *interp, PyTypeObject *self,
244244
? index
245245
: index + _Py_MAX_MANAGED_STATIC_BUILTIN_TYPES;
246246

247+
PyMutex_Lock(&_PyRuntime.types.managed_static.mutex);
247248
assert((initial == 1) ==
248249
(_PyRuntime.types.managed_static.types[full_index].interp_count == 0));
249250
_PyRuntime.types.managed_static.types[full_index].interp_count += 1;
251+
PyMutex_Unlock(&_PyRuntime.types.managed_static.mutex);
250252

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

305+
PyMutex_Lock(&_PyRuntime.types.managed_static.mutex);
303306
_PyRuntime.types.managed_static.types[full_index].interp_count -= 1;
307+
PyMutex_Unlock(&_PyRuntime.types.managed_static.mutex);
304308
if (final) {
305309
assert(!_PyRuntime.types.managed_static.types[full_index].interp_count);
306310
_PyRuntime.types.managed_static.types[full_index].type = NULL;

0 commit comments

Comments
 (0)