From 1dc66de17ae94b555e16fbe5d423b4aed026fbf6 Mon Sep 17 00:00:00 2001 From: jg Date: Thu, 3 Dec 2026 20:50:25 +0000 Subject: [PATCH] Sync QSBR state on `PyErr_CheckSignals` On `PyErr_CheckSignals` sync global QSBR state into thread copy. This allows for long running C functions which are expected to call this to help unblocked deferred QSBR memory in FT build from other threads. --- Doc/c-api/exceptions.rst | 8 ++++++++ Include/internal/pycore_qsbr.h | 3 ++- Lib/test/test_free_threading/test_capi.py | 9 +++++++++ ...6-12-03-20-30-00.gh-issue-162705.Qs8rCk.rst | 4 ++++ Modules/_testinternalcapi.c | 18 ++++++++++++++++++ Modules/signalmodule.c | 7 +++++++ 6 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/C_API/2026-12-03-20-30-00.gh-issue-162705.Qs8rCk.rst diff --git a/Doc/c-api/exceptions.rst b/Doc/c-api/exceptions.rst index dd67222bebc3db..acac58bd509d00 100644 --- a/Doc/c-api/exceptions.rst +++ b/Doc/c-api/exceptions.rst @@ -707,6 +707,10 @@ Signal Handling :c:func:`!PyErr_CheckSignals` should be called by long-running C code frequently enough so that the response appears immediate to humans. + On the :term:`free-threaded build`, calling this function also allows + memory freed by other threads to be released. Long-running C code that does + not call it delays that until it returns, which can increase memory use. + Handlers invoked by this function currently include: - Signal handlers, including Python functions registered using @@ -741,6 +745,10 @@ Signal Handling .. versionchanged:: 3.15 The exception set by :c:func:`PyThreadState_SetAsyncExc` is now raised. + .. versionchanged:: next + On the free-threaded build, this function now allows memory freed by + other threads to be released. + .. c:function:: void PyErr_SetInterrupt() diff --git a/Include/internal/pycore_qsbr.h b/Include/internal/pycore_qsbr.h index 98c23384221d9c..e5ee45bfff94bd 100644 --- a/Include/internal/pycore_qsbr.h +++ b/Include/internal/pycore_qsbr.h @@ -132,7 +132,8 @@ _Py_qbsr_goal_reached(struct _qsbr_thread_state *qsbr, uint64_t goal) // Advance the write sequence and return the new goal. This should be called // after data is removed. The returned goal is used with `_Py_qsbr_poll()` to // determine when it is safe to reclaim (free) the memory. -extern uint64_t +// Export for '_testinternalcapi' shared extension. +PyAPI_FUNC(uint64_t) _Py_qsbr_advance(struct _qsbr_shared *shared); // Return the next value for the write sequence (current plus the increment). diff --git a/Lib/test/test_free_threading/test_capi.py b/Lib/test/test_free_threading/test_capi.py index 36021eb33d5900..766d2b2fac9ae6 100644 --- a/Lib/test/test_free_threading/test_capi.py +++ b/Lib/test/test_free_threading/test_capi.py @@ -42,5 +42,14 @@ def worker(): sys.modules.pop(module_name, None) +class TestQSBR(unittest.TestCase): + def test_check_signals_reports_quiescent_state(self): + # PyErr_CheckSignals() reports a quiescent state, so the + # thread catches up with the write sequence advanced just before it. + _testinternalcapi = import_helper.import_module('_testinternalcapi') + goal, seq = _testinternalcapi.qsbr_advance_and_check_signals() + self.assertGreaterEqual(seq, goal) + + if __name__ == "__main__": unittest.main() diff --git a/Misc/NEWS.d/next/C_API/2026-12-03-20-30-00.gh-issue-162705.Qs8rCk.rst b/Misc/NEWS.d/next/C_API/2026-12-03-20-30-00.gh-issue-162705.Qs8rCk.rst new file mode 100644 index 00000000000000..d2ad5d7d20908d --- /dev/null +++ b/Misc/NEWS.d/next/C_API/2026-12-03-20-30-00.gh-issue-162705.Qs8rCk.rst @@ -0,0 +1,4 @@ +On the :term:`free-threaded build`, :c:func:`PyErr_CheckSignals` now allows +memory freed by other threads to be released while long-running C code that +calls it is still running. Previously that memory built up until the C code +returned. diff --git a/Modules/_testinternalcapi.c b/Modules/_testinternalcapi.c index 586b687f233ce0..c58ec657316f68 100644 --- a/Modules/_testinternalcapi.c +++ b/Modules/_testinternalcapi.c @@ -3029,6 +3029,22 @@ get_long_lived_total(PyObject *self, PyObject *Py_UNUSED(ignored)) return PyLong_FromInt64(PyInterpreterState_Get()->gc.long_lived_total); } +// Advance the QSBR write sequence, call PyErr_CheckSignals(), and return +// (new write sequence, this thread's read sequence). +static PyObject * +qsbr_advance_and_check_signals(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + struct _qsbr_thread_state *qsbr = + ((_PyThreadStateImpl *)_PyThreadState_GET())->qsbr; + uint64_t goal = _Py_qsbr_advance(qsbr->shared); + if (PyErr_CheckSignals() < 0) { + return NULL; + } + uint64_t seq = _Py_atomic_load_uint64(&qsbr->seq); + return Py_BuildValue("(KK)", (unsigned long long)goal, + (unsigned long long)seq); +} + #endif static PyObject * @@ -3591,6 +3607,8 @@ static PyMethodDef module_functions[] = { {"get_tlbc", get_tlbc, METH_O, NULL}, {"get_tlbc_id", get_tlbc_id, METH_O, NULL}, {"get_long_lived_total", get_long_lived_total, METH_NOARGS}, + {"qsbr_advance_and_check_signals", qsbr_advance_and_check_signals, + METH_NOARGS}, #endif #ifdef _Py_TIER2 {"uop_symbols_test", _Py_uop_symbols_test, METH_NOARGS}, diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index 85ed113a17e5e8..9923b605c1467e 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -12,6 +12,7 @@ #include "pycore_moduleobject.h" // _PyModule_GetState() #include "pycore_pyerrors.h" // _PyErr_SetString() #include "pycore_pystate.h" // _PyThreadState_GET() +#include "pycore_qsbr.h" // _Py_qsbr_quiescent_state() #include "pycore_signal.h" // _Py_RestoreSignals() #include "pycore_time.h" // _PyTime_FromSecondsObject() #include "pycore_tuple.h" // _PyTuple_FromPairSteal @@ -1775,6 +1776,12 @@ PyErr_CheckSignals(void) { PyThreadState *tstate = _PyThreadState_GET(); +#ifdef Py_GIL_DISABLED + /* Let QSBR free memory released by other threads while long-running + native code runs. The GC run below also reports a quiescent state. */ + _Py_qsbr_quiescent_state(((_PyThreadStateImpl *)tstate)->qsbr); +#endif + /* Opportunistically check if the GC is scheduled to run and run it if we have a request. This is done here because native code needs to call this API if is going to run for some time without executing