From: Gregory P. Smith <greg@krypto.org>
Date: Sun, 11 Nov 2012 04:33:07 +0000 (-0800)
Subject: Fixes issue #9535: Fix pending signals that have been received but not
X-Git-Tag: v3.3.1rc1~653^2
X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9463e3ac8b230efa2f35e08859cb3db5c6d192b7;p=python

Fixes issue #9535: Fix pending signals that have been received but not
yet handled by Python to not persist after os.fork() in the child process.
---

diff --git a/Misc/NEWS b/Misc/NEWS
index ab18f032c5..6a97cbcf8a 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.2.4
 Core and Builtins
 -----------------
 
+- Issue #9535: Fix pending signals that have been received but not yet
+  handled by Python to not persist after os.fork() in the child process.
+
 - Issue #15001: fix segfault on "del sys.module['__main__']". Patch by Victor
   Stinner.
 
diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c
index 32cd8bbece..9cae454194 100644
--- a/Modules/signalmodule.c
+++ b/Modules/signalmodule.c
@@ -987,9 +987,25 @@ PyOS_InterruptOccurred(void)
     return 0;
 }
 
+static void
+_clear_pending_signals(void)
+{
+    int i;
+    if (!is_tripped)
+        return;
+    is_tripped = 0;
+    for (i = 1; i < NSIG; ++i) {
+        Handlers[i].tripped = 0;
+    }
+}
+
 void
 PyOS_AfterFork(void)
 {
+    /* Clear the signal flags after forking so that they aren't handled
+     * in both processes if they came in just before the fork() but before
+     * the interpreter had an opportunity to call the handlers.  issue9535. */
+    _clear_pending_signals();
 #ifdef WITH_THREAD
     /* PyThread_ReInitTLS() must be called early, to make sure that the TLS API
      * can be called safely. */