Skip to content

Commit 761667a

Browse files
committed
pythongh-154348: fix data race on nogil build about sys.monitoring.use_tool_id()
1 parent cbe4977 commit 761667a

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

‎Python/instrumentation.c‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2166,6 +2166,10 @@ check_valid_tool(int tool_id)
21662166
return 0;
21672167
}
21682168

2169+
// Protects the interp->monitoring_tool_names array against concurrent
2170+
// use_tool_id/clear_tool_id/free_tool_id/get_tool calls.
2171+
static PyMutex monitoring_tool_names_mutex;
2172+
21692173
/*[clinic input]
21702174
monitoring.use_tool_id
21712175
@@ -2187,11 +2191,14 @@ monitoring_use_tool_id_impl(PyObject *module, int tool_id, PyObject *name)
21872191
return NULL;
21882192
}
21892193
PyInterpreterState *interp = _PyInterpreterState_GET();
2194+
PyMutex_Lock(&monitoring_tool_names_mutex);
21902195
if (interp->monitoring_tool_names[tool_id] != NULL) {
21912196
PyErr_Format(PyExc_ValueError, "tool %d is already in use", tool_id);
2197+
PyMutex_Unlock(&monitoring_tool_names_mutex);
21922198
return NULL;
21932199
}
21942200
interp->monitoring_tool_names[tool_id] = Py_NewRef(name);
2201+
PyMutex_Unlock(&monitoring_tool_names_mutex);
21952202
Py_RETURN_NONE;
21962203
}
21972204

@@ -2213,11 +2220,14 @@ monitoring_clear_tool_id_impl(PyObject *module, int tool_id)
22132220

22142221
PyInterpreterState *interp = _PyInterpreterState_GET();
22152222

2223+
PyMutex_Lock(&monitoring_tool_names_mutex);
22162224
if (interp->monitoring_tool_names[tool_id] != NULL) {
22172225
if (_PyMonitoring_ClearToolId(tool_id) < 0) {
2226+
PyMutex_Unlock(&monitoring_tool_names_mutex);
22182227
return NULL;
22192228
}
22202229
}
2230+
PyMutex_Unlock(&monitoring_tool_names_mutex);
22212231

22222232
Py_RETURN_NONE;
22232233
}
@@ -2239,13 +2249,16 @@ monitoring_free_tool_id_impl(PyObject *module, int tool_id)
22392249
}
22402250
PyInterpreterState *interp = _PyInterpreterState_GET();
22412251

2252+
PyMutex_Lock(&monitoring_tool_names_mutex);
22422253
if (interp->monitoring_tool_names[tool_id] != NULL) {
22432254
if (_PyMonitoring_ClearToolId(tool_id) < 0) {
2255+
PyMutex_Unlock(&monitoring_tool_names_mutex);
22442256
return NULL;
22452257
}
22462258
}
22472259

22482260
Py_CLEAR(interp->monitoring_tool_names[tool_id]);
2261+
PyMutex_Unlock(&monitoring_tool_names_mutex);
22492262
Py_RETURN_NONE;
22502263
}
22512264

@@ -2267,11 +2280,15 @@ monitoring_get_tool_impl(PyObject *module, int tool_id)
22672280
return NULL;
22682281
}
22692282
PyInterpreterState *interp = _PyInterpreterState_GET();
2283+
PyMutex_Lock(&monitoring_tool_names_mutex);
22702284
PyObject *name = interp->monitoring_tool_names[tool_id];
22712285
if (name == NULL) {
2286+
PyMutex_Unlock(&monitoring_tool_names_mutex);
22722287
Py_RETURN_NONE;
22732288
}
2274-
return Py_NewRef(name);
2289+
PyObject *ret = Py_NewRef(name);
2290+
PyMutex_Unlock(&monitoring_tool_names_mutex);
2291+
return ret;
22752292
}
22762293

22772294
/*[clinic input]

0 commit comments

Comments
 (0)