]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-90623: signal.raise_signal() calls PyErr_CheckSignals() (#91756)
authorVictor Stinner <vstinner@python.org>
Thu, 21 Apr 2022 01:14:57 +0000 (03:14 +0200)
committerGitHub <noreply@github.com>
Thu, 21 Apr 2022 01:14:57 +0000 (03:14 +0200)
signal.raise_signal() and os.kill() now call PyErr_CheckSignals() to
check immediately for pending signals.

Misc/NEWS.d/next/Library/2022-04-20-18-47-27.gh-issue-90623.5fROpX.rst [new file with mode: 0644]
Modules/posixmodule.c
Modules/signalmodule.c

diff --git a/Misc/NEWS.d/next/Library/2022-04-20-18-47-27.gh-issue-90623.5fROpX.rst b/Misc/NEWS.d/next/Library/2022-04-20-18-47-27.gh-issue-90623.5fROpX.rst
new file mode 100644 (file)
index 0000000..566cf35
--- /dev/null
@@ -0,0 +1,2 @@
+:func:`signal.raise_signal` and :func:`os.kill` now check immediately for
+pending signals. Patch by Victor Stinner.
index 345ed710248f9bb992c294370c23143ef56328c0..a9132a78994ce44fcd0e5573e979c083bfc9f342 100644 (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;
index 1ee5c669df015f158121f7e633263fe9708027b6..02c58ff538ecdce8f34565d62adb7a0cecb13599 100644 (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;
 }