[Python-checkins] gh-90623: signal.raise_signal() calls PyErr_CheckSignals() (#91756)

vstinner webhook-mailer at python.org
Wed Apr 20 21:15:30 EDT 2022


https://github.com/python/cpython/commit/031f1e6040d1bb0454bd6da417fd9e38aad4fc89
commit: 031f1e6040d1bb0454bd6da417fd9e38aad4fc89
branch: main
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2022-04-21T03:14:57+02:00
summary:

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.

files:
A Misc/NEWS.d/next/Library/2022-04-20-18-47-27.gh-issue-90623.5fROpX.rst
M Modules/posixmodule.c
M 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
index 00000000000000..566cf35c328689
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-04-20-18-47-27.gh-issue-90623.5fROpX.rst
@@ -0,0 +1,2 @@
+:func:`signal.raise_signal` and :func:`os.kill` now check immediately for
+pending signals. Patch by Victor Stinner.
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 345ed710248f9b..a9132a78994ce4 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -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;
diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c
index 1ee5c669df015f..02c58ff538ecdc 100644
--- a/Modules/signalmodule.c
+++ b/Modules/signalmodule.c
@@ -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;
 }
 



More information about the Python-checkins mailing list