diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-08-20-14-17-47.gh-issue-137992.fcL3SK.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-08-20-14-17-47.gh-issue-137992.fcL3SK.rst new file mode 100644 index 00000000000000..068f04120daa3a --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-08-20-14-17-47.gh-issue-137992.fcL3SK.rst @@ -0,0 +1,2 @@ +Ensure that :c:func:`PyRefTracer_SetTracer` sync with all existing threads when called +to avoid races in the free threaded build. Patch by Pablo Galindo diff --git a/Objects/object.c b/Objects/object.c index 2c07c2e9841b0d..c9bcc0c7b09e63 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -3287,6 +3287,8 @@ _Py_SetRefcnt(PyObject *ob, Py_ssize_t refcnt) int PyRefTracer_SetTracer(PyRefTracer tracer, void *data) { _Py_AssertHoldsTstate(); + + _PyEval_StopTheWorldAll(&_PyRuntime); if (_PyRuntime.ref_tracer.tracer_func != NULL) { _PyReftracerTrack(NULL, PyRefTracer_TRACKER_REMOVED); if (PyErr_Occurred()) { @@ -3295,6 +3297,7 @@ int PyRefTracer_SetTracer(PyRefTracer tracer, void *data) { } _PyRuntime.ref_tracer.tracer_func = tracer; _PyRuntime.ref_tracer.tracer_data = data; + _PyEval_StartTheWorldAll(&_PyRuntime); return 0; }