Skip to content

Commit 90eafdb

Browse files
tichMariatta
authored andcommitted
faulthandler: Restore the old sigaltstack during teardown (GH-777) (GH-797)
(cherry picked from commit 20fbf8a)
1 parent ee51327 commit 90eafdb

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,6 +1709,7 @@ Artur Zaprzala
17091709
Mike Zarnstorff
17101710
Yury V. Zaytsev
17111711
Siebren van der Zee
1712+
Christophe Zeitouny
17121713
Nickolai Zeldovich
17131714
Yuxiao Zeng
17141715
Uwe Zessin

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ Core and Builtins
2727
Library
2828
-------
2929

30+
- bpo-29884: faulthandler: Restore the old sigaltstack during teardown.
31+
Patch by Christophe Zeitouny.
32+
3033
- bpo-25455: Fixed crashes in repr of recursive buffered file-like objects.
3134

3235
- bpo-29800: Fix crashes in partial.__repr__ if the keys of partial.keywords

Modules/faulthandler.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ static const size_t faulthandler_nsignals = \
124124

125125
#ifdef HAVE_SIGALTSTACK
126126
static stack_t stack;
127+
static stack_t old_stack;
127128
#endif
128129

129130

@@ -1310,7 +1311,7 @@ int _PyFaulthandler_Init(void)
13101311
stack.ss_size = SIGSTKSZ;
13111312
stack.ss_sp = PyMem_Malloc(stack.ss_size);
13121313
if (stack.ss_sp != NULL) {
1313-
err = sigaltstack(&stack, NULL);
1314+
err = sigaltstack(&stack, &old_stack);
13141315
if (err) {
13151316
PyMem_Free(stack.ss_sp);
13161317
stack.ss_sp = NULL;
@@ -1366,6 +1367,20 @@ void _PyFaulthandler_Fini(void)
13661367
faulthandler_disable();
13671368
#ifdef HAVE_SIGALTSTACK
13681369
if (stack.ss_sp != NULL) {
1370+
/* Fetch the current alt stack */
1371+
stack_t current_stack;
1372+
if (sigaltstack(NULL, &current_stack) == 0) {
1373+
if (current_stack.ss_sp == stack.ss_sp) {
1374+
/* The current alt stack is the one that we installed.
1375+
It is safe to restore the old stack that we found when
1376+
we installed ours */
1377+
sigaltstack(&old_stack, NULL);
1378+
} else {
1379+
/* Someone switched to a different alt stack and didn't
1380+
restore ours when they were done (if they're done).
1381+
There's not much we can do in this unlikely case */
1382+
}
1383+
}
13691384
PyMem_Free(stack.ss_sp);
13701385
stack.ss_sp = NULL;
13711386
}

0 commit comments

Comments
 (0)