]> granicus.if.org Git - python/commitdiff
faulthandler: Restore the old sigaltstack during teardown (GH-777) (GH-797)
authorChristophe Zeitouny <tich@users.noreply.github.com>
Fri, 24 Mar 2017 11:20:40 +0000 (04:20 -0700)
committerMariatta <Mariatta@users.noreply.github.com>
Fri, 24 Mar 2017 11:20:40 +0000 (04:20 -0700)
(cherry picked from commit 20fbf8accd494fd15b0fc4c84928178c71ead4d1)

Misc/ACKS
Misc/NEWS
Modules/faulthandler.c

index 03afeb8f3876ee0dfee203f273cc69d32ccfbf67..2b23702f22910c4cd0703ae09434efbfcd9d0569 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1709,6 +1709,7 @@ Artur Zaprzala
 Mike Zarnstorff
 Yury V. Zaytsev
 Siebren van der Zee
+Christophe Zeitouny
 Nickolai Zeldovich
 Yuxiao Zeng
 Uwe Zessin
index 1835d1e389d7258d345f1c01e35a0172c580c4a6..687c5c06ef9bdbc8a602c5e568c3f20c2060e14b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -27,6 +27,9 @@ Core and Builtins
 Library
 -------
 
+- bpo-29884: faulthandler: Restore the old sigaltstack during teardown.
+  Patch by Christophe Zeitouny.
+
 - bpo-25455: Fixed crashes in repr of recursive buffered file-like objects.
 
 - bpo-29800: Fix crashes in partial.__repr__ if the keys of partial.keywords
index 1c1e4fb7d177600e8535ea28f26df88f2bf8d334..2f8b624fd160d9db7a8bae2e45e0c17371d6fa4f 100644 (file)
@@ -124,6 +124,7 @@ static const size_t faulthandler_nsignals = \
 
 #ifdef HAVE_SIGALTSTACK
 static stack_t stack;
+static stack_t old_stack;
 #endif
 
 
@@ -1310,7 +1311,7 @@ int _PyFaulthandler_Init(void)
     stack.ss_size = SIGSTKSZ;
     stack.ss_sp = PyMem_Malloc(stack.ss_size);
     if (stack.ss_sp != NULL) {
-        err = sigaltstack(&stack, NULL);
+        err = sigaltstack(&stack, &old_stack);
         if (err) {
             PyMem_Free(stack.ss_sp);
             stack.ss_sp = NULL;
@@ -1366,6 +1367,20 @@ void _PyFaulthandler_Fini(void)
     faulthandler_disable();
 #ifdef HAVE_SIGALTSTACK
     if (stack.ss_sp != NULL) {
+        /* Fetch the current alt stack */
+        stack_t current_stack;
+        if (sigaltstack(NULL, &current_stack) == 0) {
+            if (current_stack.ss_sp == stack.ss_sp) {
+                /* The current alt stack is the one that we installed.
+                 It is safe to restore the old stack that we found when
+                 we installed ours */
+                sigaltstack(&old_stack, NULL);
+            } else {
+                /* Someone switched to a different alt stack and didn't
+                   restore ours when they were done (if they're done).
+                   There's not much we can do in this unlikely case */
+            }
+        }
         PyMem_Free(stack.ss_sp);
         stack.ss_sp = NULL;
     }