summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorKumar Aditya <kumaraditya@python.org>2025-03-12 18:41:52 (GMT)
committerGitHub <noreply@github.com>2025-03-12 18:41:52 (GMT)
commitea57ffa02e42dc430f2cb2312cdfc3d7ff7a5c70 (patch)
tree7ba596bed34b4dbe395e1e291ad6812fa81a7e61 /Python
parent25f24b01e3b675c6afbb653f07034a3c9a6aee32 (diff)
downloadcpython-ea57ffa02e42dc430f2cb2312cdfc3d7ff7a5c70.zip
cpython-ea57ffa02e42dc430f2cb2312cdfc3d7ff7a5c70.tar.gz
cpython-ea57ffa02e42dc430f2cb2312cdfc3d7ff7a5c70.tar.bz2
gh-131141: fix data race in instrumentation while registering callback (#131142)
Diffstat (limited to 'Python')
-rw-r--r--Python/instrumentation.c23
1 files changed, 13 insertions, 10 deletions
diff --git a/Python/instrumentation.c b/Python/instrumentation.c
index f871d19..c180f4c 100644
--- a/Python/instrumentation.c
+++ b/Python/instrumentation.c
@@ -3006,13 +3006,6 @@ static PyObject *make_branch_handler(int tool_id, PyObject *handler, bool right)
return (PyObject *)callback;
}
-/* Consumes a reference to obj */
-static PyObject *exchange_callables(int tool_id, int event_id, PyObject *obj)
-{
- PyInterpreterState *is = _PyInterpreterState_GET();
- return _Py_atomic_exchange_ptr(&is->monitoring_callables[tool_id][event_id], obj);
-}
-
PyObject *
_PyMonitoring_RegisterCallback(int tool_id, int event_id, PyObject *obj)
{
@@ -3036,11 +3029,21 @@ _PyMonitoring_RegisterCallback(int tool_id, int event_id, PyObject *obj)
return NULL;
}
}
- Py_XDECREF(exchange_callables(tool_id, PY_MONITORING_EVENT_BRANCH_RIGHT, right));
- res = exchange_callables(tool_id, PY_MONITORING_EVENT_BRANCH_LEFT, left);
+ PyInterpreterState *interp = _PyInterpreterState_GET();
+ _PyEval_StopTheWorld(interp);
+ PyObject *old_right = interp->monitoring_callables[tool_id][PY_MONITORING_EVENT_BRANCH_RIGHT];
+ interp->monitoring_callables[tool_id][PY_MONITORING_EVENT_BRANCH_RIGHT] = right;
+ res = interp->monitoring_callables[tool_id][PY_MONITORING_EVENT_BRANCH_LEFT];
+ interp->monitoring_callables[tool_id][PY_MONITORING_EVENT_BRANCH_LEFT] = left;
+ _PyEval_StartTheWorld(interp);
+ Py_XDECREF(old_right);
}
else {
- res = exchange_callables(tool_id, event_id, Py_XNewRef(obj));
+ PyInterpreterState *interp = _PyInterpreterState_GET();
+ _PyEval_StopTheWorld(interp);
+ res = interp->monitoring_callables[tool_id][event_id];
+ interp->monitoring_callables[tool_id][event_id] = Py_XNewRef(obj);
+ _PyEval_StartTheWorld(interp);
}
if (res != NULL && Py_TYPE(res) == &_PyLegacyBranchEventHandler_Type) {
_PyLegacyBranchEventHandler *wrapper = (_PyLegacyBranchEventHandler *)res;