@@ -3006,13 +3006,6 @@ static PyObject *make_branch_handler(int tool_id, PyObject *handler, bool right)
3006
3006
return (PyObject * )callback ;
3007
3007
}
3008
3008
3009
- /* Consumes a reference to obj */
3010
- static PyObject * exchange_callables (int tool_id , int event_id , PyObject * obj )
3011
- {
3012
- PyInterpreterState * is = _PyInterpreterState_GET ();
3013
- return _Py_atomic_exchange_ptr (& is -> monitoring_callables [tool_id ][event_id ], obj );
3014
- }
3015
-
3016
3009
PyObject *
3017
3010
_PyMonitoring_RegisterCallback (int tool_id , int event_id , PyObject * obj )
3018
3011
{
@@ -3036,11 +3029,21 @@ _PyMonitoring_RegisterCallback(int tool_id, int event_id, PyObject *obj)
3036
3029
return NULL ;
3037
3030
}
3038
3031
}
3039
- Py_XDECREF (exchange_callables (tool_id , PY_MONITORING_EVENT_BRANCH_RIGHT , right ));
3040
- res = exchange_callables (tool_id , PY_MONITORING_EVENT_BRANCH_LEFT , left );
3032
+ PyInterpreterState * interp = _PyInterpreterState_GET ();
3033
+ _PyEval_StopTheWorld (interp );
3034
+ PyObject * old_right = interp -> monitoring_callables [tool_id ][PY_MONITORING_EVENT_BRANCH_RIGHT ];
3035
+ interp -> monitoring_callables [tool_id ][PY_MONITORING_EVENT_BRANCH_RIGHT ] = right ;
3036
+ res = interp -> monitoring_callables [tool_id ][PY_MONITORING_EVENT_BRANCH_LEFT ];
3037
+ interp -> monitoring_callables [tool_id ][PY_MONITORING_EVENT_BRANCH_LEFT ] = left ;
3038
+ _PyEval_StartTheWorld (interp );
3039
+ Py_XDECREF (old_right );
3041
3040
}
3042
3041
else {
3043
- res = exchange_callables (tool_id , event_id , Py_XNewRef (obj ));
3042
+ PyInterpreterState * interp = _PyInterpreterState_GET ();
3043
+ _PyEval_StopTheWorld (interp );
3044
+ res = interp -> monitoring_callables [tool_id ][event_id ];
3045
+ interp -> monitoring_callables [tool_id ][event_id ] = Py_XNewRef (obj );
3046
+ _PyEval_StartTheWorld (interp );
3044
3047
}
3045
3048
if (res != NULL && Py_TYPE (res ) == & _PyLegacyBranchEventHandler_Type ) {
3046
3049
_PyLegacyBranchEventHandler * wrapper = (_PyLegacyBranchEventHandler * )res ;
0 commit comments