Skip to content

Give PyErr_CheckSignals guide-level documentation #134075

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 16, 2025 · 2 comments
Open

Give PyErr_CheckSignals guide-level documentation #134075

zackw opened this issue May 16, 2025 · 2 comments
Labels
docs Documentation in the Doc dir

Comments

@zackw
Copy link

zackw commented May 16, 2025

Documentation

Compiled extensions that "want to be interruptible by user requests" need to call PyErr_CheckSignals periodically; when it returns -1, they should stop work, clean up after themselves, and return to the interpreter.

This function is not mentioned at all in the extending and embedding guide, and its reference documentation makes it sound unimportant. As a result, many extension authors are (it seems to me) not aware that it exists or that they ought to be calling it, and so many extensions that should be interruptible by user requests aren't. For example, if you execute this code

>>> import numpy as np
>>> rng = np.random.default_rng()
>>> x = rng.random(1000000000)

and then immediately hit control-C, you'll be waiting at least a few seconds before you get the prompt back.

PyErr_CheckSignals also has the unusual property of potentially executing arbitrary Python code before returning to its caller, which makes it easy to use unsafely. Making an extension interruptible throughout may necessitate calling PyErr_CheckSignals from innermost loops where one would ideally release the GIL, which makes that worse (see #133465).

Thus, I suggest that a guide to correct use of PyErr_CheckSignals should be added to the extending and embedding guide.

@zackw zackw added the docs Documentation in the Doc dir label May 16, 2025
@zackw
Copy link
Author

zackw commented May 19, 2025

Please see https://research.owlfolio.org/pubs/2025-pyext-ctrlc-talk/ for additional exposition

@AA-Turner
Copy link
Member

Is there a PDF version?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
Status: Todo
Development

No branches or pull requests

2 participants