From: Kumar Aditya Date: Thu, 13 Mar 2025 09:41:26 +0000 (+0530) Subject: [3.13] gh-131141: fix data race in instrumentation while registering callback (#131166) X-Git-Tag: v3.13.3~133 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=589382bd04e4326c5582d96f71fe93b3d6365d53;p=thirdparty%2FPython%2Fcpython.git [3.13] gh-131141: fix data race in instrumentation while registering callback (#131166) --- diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-03-12-11-19-46.gh-issue-131141.tQz594.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-03-12-11-19-46.gh-issue-131141.tQz594.rst new file mode 100644 index 000000000000..c1ea679864fb --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-03-12-11-19-46.gh-issue-131141.tQz594.rst @@ -0,0 +1 @@ +Fix data race in :data:`sys.monitoring` instrumentation while registering callback. diff --git a/Python/instrumentation.c b/Python/instrumentation.c index 3481b5df1428..d4bca27f22cd 100644 --- a/Python/instrumentation.c +++ b/Python/instrumentation.c @@ -1399,9 +1399,10 @@ _PyMonitoring_RegisterCallback(int tool_id, int event_id, PyObject *obj) PyInterpreterState *is = _PyInterpreterState_GET(); assert(0 <= tool_id && tool_id < PY_MONITORING_TOOL_IDS); assert(0 <= event_id && event_id < _PY_MONITORING_EVENTS); - PyObject *callback = _Py_atomic_exchange_ptr(&is->monitoring_callables[tool_id][event_id], - Py_XNewRef(obj)); - + _PyEval_StopTheWorld(is); + PyObject *callback = is->monitoring_callables[tool_id][event_id]; + is->monitoring_callables[tool_id][event_id] = Py_XNewRef(obj); + _PyEval_StartTheWorld(is); return callback; }