Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion Python/instrumentation.c
Original file line number Diff line number Diff line change
Expand Up @@ -2166,6 +2166,10 @@ check_valid_tool(int tool_id)
return 0;
}

// Protects the interp->monitoring_tool_names array against concurrent
// use_tool_id/clear_tool_id/free_tool_id/get_tool calls.
static PyMutex monitoring_tool_names_mutex;

/*[clinic input]
monitoring.use_tool_id

Expand All @@ -2187,11 +2191,14 @@ monitoring_use_tool_id_impl(PyObject *module, int tool_id, PyObject *name)
return NULL;
}
PyInterpreterState *interp = _PyInterpreterState_GET();
PyMutex_Lock(&monitoring_tool_names_mutex);
if (interp->monitoring_tool_names[tool_id] != NULL) {
PyErr_Format(PyExc_ValueError, "tool %d is already in use", tool_id);
PyMutex_Unlock(&monitoring_tool_names_mutex);
return NULL;
}
interp->monitoring_tool_names[tool_id] = Py_NewRef(name);
PyMutex_Unlock(&monitoring_tool_names_mutex);
Py_RETURN_NONE;
}

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

PyInterpreterState *interp = _PyInterpreterState_GET();

PyMutex_Lock(&monitoring_tool_names_mutex);
if (interp->monitoring_tool_names[tool_id] != NULL) {
if (_PyMonitoring_ClearToolId(tool_id) < 0) {
PyMutex_Unlock(&monitoring_tool_names_mutex);
return NULL;
}
}
PyMutex_Unlock(&monitoring_tool_names_mutex);

Py_RETURN_NONE;
}
Expand All @@ -2239,13 +2249,16 @@ monitoring_free_tool_id_impl(PyObject *module, int tool_id)
}
PyInterpreterState *interp = _PyInterpreterState_GET();

PyMutex_Lock(&monitoring_tool_names_mutex);
if (interp->monitoring_tool_names[tool_id] != NULL) {
if (_PyMonitoring_ClearToolId(tool_id) < 0) {
PyMutex_Unlock(&monitoring_tool_names_mutex);
return NULL;
}
}

Py_CLEAR(interp->monitoring_tool_names[tool_id]);
PyMutex_Unlock(&monitoring_tool_names_mutex);
Py_RETURN_NONE;
}

Expand All @@ -2267,11 +2280,15 @@ monitoring_get_tool_impl(PyObject *module, int tool_id)
return NULL;
}
PyInterpreterState *interp = _PyInterpreterState_GET();
PyMutex_Lock(&monitoring_tool_names_mutex);
PyObject *name = interp->monitoring_tool_names[tool_id];
if (name == NULL) {
PyMutex_Unlock(&monitoring_tool_names_mutex);
Py_RETURN_NONE;
}
return Py_NewRef(name);
PyObject *ret = Py_NewRef(name);
PyMutex_Unlock(&monitoring_tool_names_mutex);
return ret;
}

/*[clinic input]
Expand Down
Loading