Skip to content

Commit 9bc1100

Browse files
committed
gh-154348: fix data race on nogil build about sys.monitoring.use_tool_id()
1 parent cbe4977 commit 9bc1100

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

Include/internal/pycore_interp_structs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,7 @@ struct _is {
10221022
Py_ssize_t sys_tracing_threads; /* Count of threads with c_tracefunc set */
10231023
PyObject *monitoring_callables[PY_MONITORING_TOOL_IDS][_PY_MONITORING_EVENTS];
10241024
PyObject *monitoring_tool_names[PY_MONITORING_TOOL_IDS];
1025+
PyMutex monitoring_tool_names_mutex;
10251026
uintptr_t monitoring_tool_versions[PY_MONITORING_TOOL_IDS];
10261027

10271028
struct _Py_interp_cached_objects cached_objects;

Python/instrumentation.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2187,11 +2187,14 @@ monitoring_use_tool_id_impl(PyObject *module, int tool_id, PyObject *name)
21872187
return NULL;
21882188
}
21892189
PyInterpreterState *interp = _PyInterpreterState_GET();
2190+
PyMutex_Lock(&interp->monitoring_tool_names_mutex);
21902191
if (interp->monitoring_tool_names[tool_id] != NULL) {
21912192
PyErr_Format(PyExc_ValueError, "tool %d is already in use", tool_id);
2193+
PyMutex_Unlock(&interp->monitoring_tool_names_mutex);
21922194
return NULL;
21932195
}
21942196
interp->monitoring_tool_names[tool_id] = Py_NewRef(name);
2197+
PyMutex_Unlock(&interp->monitoring_tool_names_mutex);
21952198
Py_RETURN_NONE;
21962199
}
21972200

@@ -2213,11 +2216,14 @@ monitoring_clear_tool_id_impl(PyObject *module, int tool_id)
22132216

22142217
PyInterpreterState *interp = _PyInterpreterState_GET();
22152218

2219+
PyMutex_Lock(&interp->monitoring_tool_names_mutex);
22162220
if (interp->monitoring_tool_names[tool_id] != NULL) {
22172221
if (_PyMonitoring_ClearToolId(tool_id) < 0) {
2222+
PyMutex_Unlock(&interp->monitoring_tool_names_mutex);
22182223
return NULL;
22192224
}
22202225
}
2226+
PyMutex_Unlock(&interp->monitoring_tool_names_mutex);
22212227

22222228
Py_RETURN_NONE;
22232229
}
@@ -2239,13 +2245,16 @@ monitoring_free_tool_id_impl(PyObject *module, int tool_id)
22392245
}
22402246
PyInterpreterState *interp = _PyInterpreterState_GET();
22412247

2248+
PyMutex_Lock(&interp->monitoring_tool_names_mutex);
22422249
if (interp->monitoring_tool_names[tool_id] != NULL) {
22432250
if (_PyMonitoring_ClearToolId(tool_id) < 0) {
2251+
PyMutex_Unlock(&interp->monitoring_tool_names_mutex);
22442252
return NULL;
22452253
}
22462254
}
22472255

22482256
Py_CLEAR(interp->monitoring_tool_names[tool_id]);
2257+
PyMutex_Unlock(&interp->monitoring_tool_names_mutex);
22492258
Py_RETURN_NONE;
22502259
}
22512260

@@ -2267,11 +2276,15 @@ monitoring_get_tool_impl(PyObject *module, int tool_id)
22672276
return NULL;
22682277
}
22692278
PyInterpreterState *interp = _PyInterpreterState_GET();
2279+
PyMutex_Lock(&interp->monitoring_tool_names_mutex);
22702280
PyObject *name = interp->monitoring_tool_names[tool_id];
22712281
if (name == NULL) {
2282+
PyMutex_Unlock(&interp->monitoring_tool_names_mutex);
22722283
Py_RETURN_NONE;
22732284
}
2274-
return Py_NewRef(name);
2285+
PyObject *ret = Py_NewRef(name);
2286+
PyMutex_Unlock(&interp->monitoring_tool_names_mutex);
2287+
return ret;
22752288
}
22762289

22772290
/*[clinic input]

0 commit comments

Comments
 (0)