Skip to content

Commit 1dc66de

Browse files
committed
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.
1 parent 64c20ff commit 1dc66de

6 files changed

Lines changed: 48 additions & 1 deletion

File tree

‎Doc/c-api/exceptions.rst‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,10 @@ Signal Handling
707707
:c:func:`!PyErr_CheckSignals` should be called by long-running C code
708708
frequently enough so that the response appears immediate to humans.
709709
710+
On the :term:`free-threaded build`, calling this function also allows
711+
memory freed by other threads to be released. Long-running C code that does
712+
not call it delays that until it returns, which can increase memory use.
713+
710714
Handlers invoked by this function currently include:
711715
712716
- Signal handlers, including Python functions registered using
@@ -741,6 +745,10 @@ Signal Handling
741745
.. versionchanged:: 3.15
742746
The exception set by :c:func:`PyThreadState_SetAsyncExc` is now raised.
743747
748+
.. versionchanged:: next
749+
On the free-threaded build, this function now allows memory freed by
750+
other threads to be released.
751+
744752
745753
.. c:function:: void PyErr_SetInterrupt()
746754

‎Include/internal/pycore_qsbr.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ _Py_qbsr_goal_reached(struct _qsbr_thread_state *qsbr, uint64_t goal)
132132
// Advance the write sequence and return the new goal. This should be called
133133
// after data is removed. The returned goal is used with `_Py_qsbr_poll()` to
134134
// determine when it is safe to reclaim (free) the memory.
135-
extern uint64_t
135+
// Export for '_testinternalcapi' shared extension.
136+
PyAPI_FUNC(uint64_t)
136137
_Py_qsbr_advance(struct _qsbr_shared *shared);
137138

138139
// Return the next value for the write sequence (current plus the increment).

‎Lib/test/test_free_threading/test_capi.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,14 @@ def worker():
4242
sys.modules.pop(module_name, None)
4343

4444

45+
class TestQSBR(unittest.TestCase):
46+
def test_check_signals_reports_quiescent_state(self):
47+
# PyErr_CheckSignals() reports a quiescent state, so the
48+
# thread catches up with the write sequence advanced just before it.
49+
_testinternalcapi = import_helper.import_module('_testinternalcapi')
50+
goal, seq = _testinternalcapi.qsbr_advance_and_check_signals()
51+
self.assertGreaterEqual(seq, goal)
52+
53+
4554
if __name__ == "__main__":
4655
unittest.main()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
On the :term:`free-threaded build`, :c:func:`PyErr_CheckSignals` now allows
2+
memory freed by other threads to be released while long-running C code that
3+
calls it is still running. Previously that memory built up until the C code
4+
returned.

‎Modules/_testinternalcapi.c‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3029,6 +3029,22 @@ get_long_lived_total(PyObject *self, PyObject *Py_UNUSED(ignored))
30293029
return PyLong_FromInt64(PyInterpreterState_Get()->gc.long_lived_total);
30303030
}
30313031

3032+
// Advance the QSBR write sequence, call PyErr_CheckSignals(), and return
3033+
// (new write sequence, this thread's read sequence).
3034+
static PyObject *
3035+
qsbr_advance_and_check_signals(PyObject *self, PyObject *Py_UNUSED(ignored))
3036+
{
3037+
struct _qsbr_thread_state *qsbr =
3038+
((_PyThreadStateImpl *)_PyThreadState_GET())->qsbr;
3039+
uint64_t goal = _Py_qsbr_advance(qsbr->shared);
3040+
if (PyErr_CheckSignals() < 0) {
3041+
return NULL;
3042+
}
3043+
uint64_t seq = _Py_atomic_load_uint64(&qsbr->seq);
3044+
return Py_BuildValue("(KK)", (unsigned long long)goal,
3045+
(unsigned long long)seq);
3046+
}
3047+
30323048
#endif
30333049

30343050
static PyObject *
@@ -3591,6 +3607,8 @@ static PyMethodDef module_functions[] = {
35913607
{"get_tlbc", get_tlbc, METH_O, NULL},
35923608
{"get_tlbc_id", get_tlbc_id, METH_O, NULL},
35933609
{"get_long_lived_total", get_long_lived_total, METH_NOARGS},
3610+
{"qsbr_advance_and_check_signals", qsbr_advance_and_check_signals,
3611+
METH_NOARGS},
35943612
#endif
35953613
#ifdef _Py_TIER2
35963614
{"uop_symbols_test", _Py_uop_symbols_test, METH_NOARGS},

‎Modules/signalmodule.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "pycore_moduleobject.h" // _PyModule_GetState()
1313
#include "pycore_pyerrors.h" // _PyErr_SetString()
1414
#include "pycore_pystate.h" // _PyThreadState_GET()
15+
#include "pycore_qsbr.h" // _Py_qsbr_quiescent_state()
1516
#include "pycore_signal.h" // _Py_RestoreSignals()
1617
#include "pycore_time.h" // _PyTime_FromSecondsObject()
1718
#include "pycore_tuple.h" // _PyTuple_FromPairSteal
@@ -1775,6 +1776,12 @@ PyErr_CheckSignals(void)
17751776
{
17761777
PyThreadState *tstate = _PyThreadState_GET();
17771778

1779+
#ifdef Py_GIL_DISABLED
1780+
/* Let QSBR free memory released by other threads while long-running
1781+
native code runs. The GC run below also reports a quiescent state. */
1782+
_Py_qsbr_quiescent_state(((_PyThreadStateImpl *)tstate)->qsbr);
1783+
#endif
1784+
17781785
/* Opportunistically check if the GC is scheduled to run and run it
17791786
if we have a request. This is done here because native code needs
17801787
to call this API if is going to run for some time without executing

0 commit comments

Comments
 (0)