]> granicus.if.org Git - python/commitdiff
faulthandler: Restore the old sigaltstack during teardown (#777)
authorChristophe Zeitouny <tich@users.noreply.github.com>
Thu, 23 Mar 2017 17:14:29 +0000 (10:14 -0700)
committerVictor Stinner <victor.stinner@gmail.com>
Thu, 23 Mar 2017 17:14:29 +0000 (18:14 +0100)
Misc/ACKS
Misc/NEWS
Modules/faulthandler.c

index c99aeaa335e971339c1c15b9cc3f69b0ab38eda7..2056efe8fb036248f218f59f0ab2ebd0624ab3db 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1716,6 +1716,7 @@ Artur Zaprzala
 Mike Zarnstorff
 Yury V. Zaytsev
 Siebren van der Zee
+Christophe Zeitouny
 Nickolai Zeldovich
 Yuxiao Zeng
 Uwe Zessin
index 97c45d4776bba83a7a5c26a5f9bcc279f1549b8a..8cc8b9f971f2bcb054ca2533ee06d341693f16ba 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -287,6 +287,9 @@ Extension Modules
 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 4a03eaf96a8b4e48e5533f674c91552df8e750eb..fc9490d0d44889c3131b354ed479c88248a97a90 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
 
 
@@ -1317,7 +1318,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;
@@ -1373,6 +1374,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;
     }