[Python-checkins] cpython (merge 3.2 -> default): (Merge 3.2) Issue #11768: The signal handler of the signal module only calls

victor.stinner python-checkins at python.org
Mon Apr 18 16:30:30 CEST 2011


http://hg.python.org/cpython/rev/fdcbc8453304
changeset:   69425:fdcbc8453304
parent:      69422:fcd20a565b95
parent:      69424:28ab8c6ad8f9
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Mon Apr 18 16:30:17 2011 +0200
summary:
  (Merge 3.2) Issue #11768: The signal handler of the signal module only calls
Py_AddPendingCall() for the first signal to fix a deadlock on reentrant or
parallel calls. PyErr_SetInterrupt() writes also into the wake up file.

files:
  Lib/test/test_threadsignals.py |   8 ------
  Misc/NEWS                      |   4 +++
  Modules/signalmodule.c         |  26 +++++++++++++--------
  3 files changed, 20 insertions(+), 18 deletions(-)


diff --git a/Lib/test/test_threadsignals.py b/Lib/test/test_threadsignals.py
--- a/Lib/test/test_threadsignals.py
+++ b/Lib/test/test_threadsignals.py
@@ -30,14 +30,9 @@
 
 # a function that will be spawned as a separate thread.
 def send_signals():
-    print("send_signals: enter (thread %s)" % thread.get_ident(), file=sys.stderr)
-    print("send_signals: raise SIGUSR1", file=sys.stderr)
     os.kill(process_pid, signal.SIGUSR1)
-    print("send_signals: raise SIGUSR2", file=sys.stderr)
     os.kill(process_pid, signal.SIGUSR2)
-    print("send_signals: release signalled_all", file=sys.stderr)
     signalled_all.release()
-    print("send_signals: exit (thread %s)" % thread.get_ident(), file=sys.stderr)
 
 class ThreadSignals(unittest.TestCase):
 
@@ -46,12 +41,9 @@
         # We spawn a thread, have the thread send two signals, and
         # wait for it to finish. Check that we got both signals
         # and that they were run by the main thread.
-        print("test_signals: acquire lock (thread %s)" % thread.get_ident(), file=sys.stderr)
         signalled_all.acquire()
         self.spawnSignallingThread()
-        print("test_signals: wait lock (thread %s)" % thread.get_ident(), file=sys.stderr)
         signalled_all.acquire()
-        print("test_signals: lock acquired", file=sys.stderr)
         # the signals that we asked the kernel to send
         # will come back, but we don't know when.
         # (it might even be after the thread exits
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -110,6 +110,10 @@
 Library
 -------
 
+- Issue #11768: The signal handler of the signal module only calls
+  Py_AddPendingCall() for the first signal to fix a deadlock on reentrant or
+  parallel calls. PyErr_SetInterrupt() writes also into the wake up file.
+
 - Issue #11492: fix several issues with header folding in the email package.
 
 - Issue #11852: Add missing imports and update tests.
diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c
--- a/Modules/signalmodule.c
+++ b/Modules/signalmodule.c
@@ -166,6 +166,20 @@
 }
 
 static void
+trip_signal(int sig_num)
+{
+    Handlers[sig_num].tripped = 1;
+    if (is_tripped)
+        return;
+    /* Set is_tripped after setting .tripped, as it gets
+       cleared in PyErr_CheckSignals() before .tripped. */
+    is_tripped = 1;
+    Py_AddPendingCall(checksignals_witharg, NULL);
+    if (wakeup_fd != -1)
+        write(wakeup_fd, "\0", 1);
+}
+
+static void
 signal_handler(int sig_num)
 {
     int save_errno = errno;
@@ -182,13 +196,7 @@
     if (getpid() == main_pid)
 #endif
     {
-        Handlers[sig_num].tripped = 1;
-        /* Set is_tripped after setting .tripped, as it gets
-           cleared in PyErr_CheckSignals() before .tripped. */
-        is_tripped = 1;
-        Py_AddPendingCall(checksignals_witharg, NULL);
-        if (wakeup_fd != -1)
-            write(wakeup_fd, "\0", 1);
+        trip_signal(sig_num);
     }
 
 #ifndef HAVE_SIGACTION
@@ -946,9 +954,7 @@
 void
 PyErr_SetInterrupt(void)
 {
-    is_tripped = 1;
-    Handlers[SIGINT].tripped = 1;
-    Py_AddPendingCall((int (*)(void *))PyErr_CheckSignals, NULL);
+    trip_signal(SIGINT);
 }
 
 void

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list