Skip to content

PyErr_CheckSignals should be callable without holding the GIL #133465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
zackw opened this issue May 5, 2025 · 2 comments
Open

PyErr_CheckSignals should be callable without holding the GIL #133465

zackw opened this issue May 5, 2025 · 2 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-C-API type-feature A feature request or enhancement

Comments

@zackw
Copy link

zackw commented May 5, 2025

Feature or enhancement

Proposal:

Compiled-code modules that implement time-consuming operations that don’t require manipulating Python objects, are supposed to call PyErr_CheckSignals frequently throughout each such operation, so that if the user interrupts the operation with control-C, it is cancelled promptly.

In the normal case where no signals are pending, PyErr_CheckSignals is cheap; however, callers must hold the GIL, and compiled-code modules that implement time-consuming operations are also supposed to release the GIL during each such operation. The overhead of reclaiming the GIL in order to call PyErr_CheckSignals,
and then releasing it again, sufficiently often for reasonable user responsiveness, can be substantial.

If my understanding of the thread-state rules is correct, PyErr_CheckSignals only needs the GIL if it has work to do. Checking whether there is a pending signal, or a pending request to run the cycle collector, requires only a couple of atomic loads. Therefore, I propose that we should restructure PyErr_CheckSignals and its close relatives (_PyErr_CheckSignals and _PyErr_CheckSignalsTstate) so that all the “do we have anything to do” checks are done in a batch before anything that needs the GIL. If any of them are true, then the function should acquire the GIL itself, repeat the check (because another thread could have stolen the event while we were waiting for the GIL), and then actually do the work, thus enabling callers to not hold the GIL.

I have already prepared and tested an implementation of this change and will submit a PR shortly.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

@zackw zackw added the type-feature A feature request or enhancement label May 5, 2025
zackw added a commit to MillionConcepts/cpython-patches that referenced this issue May 5, 2025
@picnixz picnixz added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label May 7, 2025
@zackw
Copy link
Author

zackw commented May 19, 2025

Please see https://research.owlfolio.org/pubs/2025-pyext-ctrlc-talk/ for a "decompressed" rationale for this change, with analysis of the status quo, performance measurements, and discussion.

@zackw
Copy link
Author

zackw commented Jun 3, 2025

Discussion in #133466 wound up requesting that design issues be forwarded to the C-API working group; that thread is linked above. I have also posted https://discuss.python.org/t/ergonomics-of-signal-checks-with-detached-thread-state/94355 soliciting feedback from extension developers.

zackw added a commit to MillionConcepts/cpython-patches that referenced this issue Jun 10, 2025
Add new C-API functions `PyErr_CheckSignalsDetached` and
`PyErr_AreSignalsPending`.

`PyErr_CheckSignalsDetached` can *only* be called by threads
that *don’t* have an attached thread state.  It does the same thing as
`PyErr_CheckSignals`, except that it guarantees it will only reattach
the supplied thread state if necessary in order to run signal
handlers.  (Also, it never runs the cycle collector.)

`PyErr_AreSignalsPending` can be called with or without an attached
thread state.  It reports to its caller whether signals are pending,
but never runs any handlers itself.

Rationale: Compiled-code modules that implement time-consuming
operations that don’t require manipulating Python objects, are
supposed to call PyErr_CheckSignals frequently throughout each such
operation, so that if the user interrupts the operation with
control-C, it is canceled promptly.

In the normal case where no signals are pending, PyErr_CheckSignals
is cheap (two atomic memory operations); however, callers must have
an attached thread state, and compiled-code modules that implement
time-consuming operations are also supposed to detach their thread
state during each such operation.  The overhead of re-attaching a
thread state in order to call PyErr_CheckSignals, and then releasing
it again, sufficiently often for reasonable user responsiveness, can
be substantial, particularly in traditional (non-free-threaded) builds.
These new functions permit compiled-code modules to avoid that extra
overhead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-C-API type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants