Skip to content

Commit 33b2e0f

Browse files
committed
faulthandler: Restore the old sigaltstack during teardown
1 parent 1e2147b commit 33b2e0f

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
@@ -1716,6 +1716,7 @@ Artur Zaprzala
17161716
Mike Zarnstorff
17171717
Yury V. Zaytsev
17181718
Siebren van der Zee
1719+
Christophe Zeitouny
17191720
Nickolai Zeldovich
17201721
Yuxiao Zeng
17211722
Uwe Zessin

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ Extension Modules
287287
Library
288288
-------
289289

290+
- bpo-29884: faulthandler: Restore the old sigaltstack during teardown.
291+
Patch by Christophe Zeitouny.
292+
290293
- bpo-25455: Fixed crashes in repr of recursive buffered file-like objects.
291294

292295
- 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

@@ -1317,7 +1318,7 @@ int _PyFaulthandler_Init(void)
13171318
stack.ss_size = SIGSTKSZ;
13181319
stack.ss_sp = PyMem_Malloc(stack.ss_size);
13191320
if (stack.ss_sp != NULL) {
1320-
err = sigaltstack(&stack, NULL);
1321+
err = sigaltstack(&stack, &old_stack);
13211322
if (err) {
13221323
PyMem_Free(stack.ss_sp);
13231324
stack.ss_sp = NULL;
@@ -1373,6 +1374,20 @@ void _PyFaulthandler_Fini(void)
13731374
faulthandler_disable();
13741375
#ifdef HAVE_SIGALTSTACK
13751376
if (stack.ss_sp != NULL) {
1377+
/* Fetch the current alt stack */
1378+
stack_t current_stack;
1379+
if (sigaltstack(NULL, &current_stack) == 0) {
1380+
if (current_stack.ss_sp == stack.ss_sp) {
1381+
/* The current alt stack is the one that we installed.
1382+
It is safe to restore the old stack that we found when
1383+
we installed ours */
1384+
sigaltstack(&old_stack, NULL);
1385+
} else {
1386+
/* Someone switched to a different alt stack and didn't
1387+
restore ours when they were done (if they're done).
1388+
There's not much we can do in this unlikely case */
1389+
}
1390+
}
13761391
PyMem_Free(stack.ss_sp);
13771392
stack.ss_sp = NULL;
13781393
}

0 commit comments

Comments
 (0)