diff --git a/Python/instrumentation.c b/Python/instrumentation.c index 0af2070b5cd983..ed9f7fd613caa4 100644 --- a/Python/instrumentation.c +++ b/Python/instrumentation.c @@ -2187,11 +2187,15 @@ monitoring_use_tool_id_impl(PyObject *module, int tool_id, PyObject *name) return NULL; } PyInterpreterState *interp = _PyInterpreterState_GET(); + PyCriticalSection cs; + PyCriticalSection_Begin(&cs, module); if (interp->monitoring_tool_names[tool_id] != NULL) { PyErr_Format(PyExc_ValueError, "tool %d is already in use", tool_id); + PyCriticalSection_End(&cs); return NULL; } interp->monitoring_tool_names[tool_id] = Py_NewRef(name); + PyCriticalSection_End(&cs); Py_RETURN_NONE; } @@ -2213,11 +2217,15 @@ monitoring_clear_tool_id_impl(PyObject *module, int tool_id) PyInterpreterState *interp = _PyInterpreterState_GET(); + PyCriticalSection cs; + PyCriticalSection_Begin(&cs, module); if (interp->monitoring_tool_names[tool_id] != NULL) { if (_PyMonitoring_ClearToolId(tool_id) < 0) { + PyCriticalSection_End(&cs); return NULL; } } + PyCriticalSection_End(&cs); Py_RETURN_NONE; } @@ -2239,13 +2247,20 @@ monitoring_free_tool_id_impl(PyObject *module, int tool_id) } PyInterpreterState *interp = _PyInterpreterState_GET(); - if (interp->monitoring_tool_names[tool_id] != NULL) { + bool active; + Py_BEGIN_CRITICAL_SECTION(module); + active = (interp->monitoring_tool_names[tool_id] != NULL); + Py_END_CRITICAL_SECTION(); + + if (active) { if (_PyMonitoring_ClearToolId(tool_id) < 0) { return NULL; } } + Py_BEGIN_CRITICAL_SECTION(module); Py_CLEAR(interp->monitoring_tool_names[tool_id]); + Py_END_CRITICAL_SECTION(); Py_RETURN_NONE; } @@ -2267,11 +2282,14 @@ monitoring_get_tool_impl(PyObject *module, int tool_id) return NULL; } PyInterpreterState *interp = _PyInterpreterState_GET(); - PyObject *name = interp->monitoring_tool_names[tool_id]; + PyObject *name; + Py_BEGIN_CRITICAL_SECTION(module); + name = Py_XNewRef(interp->monitoring_tool_names[tool_id]); + Py_END_CRITICAL_SECTION(); if (name == NULL) { Py_RETURN_NONE; } - return Py_NewRef(name); + return name; } /*[clinic input]