[3.13] gh-131141: fix data race in instrumentation while registering callback (#131166)

This commit is contained in:
Kumar Aditya 2025-03-13 15:11:26 +05:30 committed by GitHub
parent 589f422d1b
commit 589382bd04
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 5 additions and 3 deletions

View file

@ -0,0 +1 @@
Fix data race in :data:`sys.monitoring` instrumentation while registering callback.

View file

@ -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;
}