Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Doc/c-api/exceptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down
3 changes: 2 additions & 1 deletion Include/internal/pycore_qsbr.h
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_free_threading/test_capi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Original file line number Diff line number Diff line change
@@ -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.
18 changes: 18 additions & 0 deletions Modules/_testinternalcapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down Expand Up @@ -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},
Expand Down
7 changes: 7 additions & 0 deletions Modules/signalmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading