-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
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
Conversation
Doc/library/_thread.rst
Outdated
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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
Misc/NEWS.d/next/Library/2021-03-04-21-51-20.bpo-43356.X7IGBM.rst
Outdated
Show resolved
Hide resolved
Also introduce a new C API ``PyErr_SetInterruptEx(int signum)``.
03d8bd6
to
c786b60
Compare
@pablogsal @eryksun Do you want to take another look? |
👍 Thank you, and thanks also for the related work on bpo-43406. |
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); |
There was a problem hiding this comment.
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)"?
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Misc/NEWS.d/next/Library/2021-03-04-21-51-20.bpo-43356.X7IGBM.rst
Outdated
Show resolved
Hide resolved
bpo-43356: Allow passing a signal number to interrupt_main() (pythonGH-24755)
Also introduce a new C API
PyErr_SetInterruptEx(int signum)
, and expand the C API docs a bit.https://bugs.python.org/issue43356