gh-90623: signal.raise_signal() calls PyErr_CheckSignals() (#91756)

signal.raise_signal() and os.kill() now call PyErr_CheckSignals() to
check immediately for pending signals.
This commit is contained in:
Victor Stinner 2022-04-21 03:14:57 +02:00 committed by GitHub
parent c77953b23e
commit 031f1e6040
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 1 deletions

View File

@ -0,0 +1,2 @@
:func:`signal.raise_signal` and :func:`os.kill` now check immediately for
pending signals. Patch by Victor Stinner.

View File

@ -7929,8 +7929,17 @@ os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal)
return NULL;
}
#ifndef MS_WINDOWS
if (kill(pid, (int)signal) == -1)
if (kill(pid, (int)signal) == -1) {
return posix_error();
}
// Check immediately if the signal was sent to the current process.
// Don't micro-optimize pid == getpid(), since PyErr_SetString() check
// is cheap.
if (PyErr_CheckSignals()) {
return NULL;
}
Py_RETURN_NONE;
#else /* !MS_WINDOWS */
PyObject *result;

View File

@ -481,6 +481,13 @@ signal_raise_signal_impl(PyObject *module, int signalnum)
if (err) {
return PyErr_SetFromErrno(PyExc_OSError);
}
// If the current thread can handle signals, handle immediately
// the raised signal.
if (PyErr_CheckSignals()) {
return NULL;
}
Py_RETURN_NONE;
}