Skip to content
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

bpo-43356: Allow passing a signal number to interrupt_main() #24755

Merged
merged 6 commits into from
Mar 11, 2021

Conversation

pitrou
Copy link
Member

@pitrou pitrou commented Mar 4, 2021

Also introduce a new C API PyErr_SetInterruptEx(int signum), and expand the C API docs a bit.

https://bugs.python.org/issue43356

Modules/signalmodule.c Outdated Show resolved Hide resolved
Modules/signalmodule.c Outdated Show resolved Hide resolved
Doc/library/_thread.rst Outdated Show resolved Hide resolved
Simulate the effect of a :data:`signal.SIGINT` signal arriving in the main
thread. A thread can use this function to interrupt the main thread.
Simulate the effect of a signal arriving in the main thread.
A thread can use this function to interrupt the main thread.
Copy link
Member

@pablogsal pablogsal Mar 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Maybe we should add here a small sentence indicating that the signal will be processed by the main thread in the eval loop or when it calls PyErr_CheckSignals(). There may be users that get confused if the main thread is locked on some foreign mutex or running in some C code that does not check for signals.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tweaked the wording slightly. I'm not sure it's good idea to go into a full-length explanation of what happens under the hood, though (especially as it's a CPython implementation detail).

Modules/_threadmodule.c Outdated Show resolved Hide resolved
Modules/_threadmodule.c Outdated Show resolved Hide resolved
@pitrou pitrou force-pushed the issue43356-interrupt-signum branch from 03d8bd6 to c786b60 Compare March 6, 2021 16:21
@pitrou
Copy link
Member Author

pitrou commented Mar 6, 2021

@pablogsal @eryksun Do you want to take another look?

@eryksun
Copy link
Contributor

eryksun commented Mar 7, 2021

👍 Thank you, and thanks also for the related work on bpo-43406.

@pablogsal
Copy link
Member

Looks fantastic, thanks for all the improvements :)

@@ -224,6 +224,9 @@ PyAPI_FUNC(void) PyErr_WriteUnraisable(PyObject *);
/* In signalmodule.c */
PyAPI_FUNC(int) PyErr_CheckSignals(void);
PyAPI_FUNC(void) PyErr_SetInterrupt(void);
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030A0000
PyAPI_FUNC(int) PyErr_SetInterruptEx(int signum);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to exclude it from the limited C API: define it in Include/cpython/pyerrors.h.

About the name, I dislike "Ex" suffix. What about "PyErr_RaiseSignal(signum)"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather keep the name similar, since those functions are closely related. Also, PyErr_RaiseSignal sends the wrong message (since the signal is not raised).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, as for excluding it from the limited C API, it doesn't seem to make sense: PyErr_SetInterrupt is in it. And I don't think it places a burden.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to add a function to the stable ABI, you must run "make regen-limited-abi" and add it to PC/python3dll.c.

And I don't think it places a burden.

Other Python implementations must implement it, it's a maintenance burden for them. _thread.interrupt_main(signum) can be called in C.

If you don't need the function, I would even suggest to remove it (make it internal/private).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't call _thread.interrupt_main from a signal handler.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other Python implementations must implement it, it's a maintenance burden for them

It's almost zero-cost if PyErr_SetInterrupt is already implemented. PyErr_SetInterrupt is the potentially bothersome part, and it's already there.

Modules/signalmodule.c Outdated Show resolved Hide resolved
Doc/c-api/exceptions.rst Outdated Show resolved Hide resolved
Doc/library/_thread.rst Outdated Show resolved Hide resolved
@pitrou pitrou requested a review from a team as a code owner March 11, 2021 22:09
@pitrou pitrou merged commit ba251c2 into python:master Mar 11, 2021
@pitrou pitrou deleted the issue43356-interrupt-signum branch March 11, 2021 22:35
sthagen added a commit to sthagen/python-cpython that referenced this pull request Mar 11, 2021
bpo-43356: Allow passing a signal number to interrupt_main() (pythonGH-24755)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants