Skip to content

Commit 6a45811

Browse files
tichMariatta
authored andcommitted
faulthandler: Restore the old sigaltstack during teardown (GH-777) (GH-796)
(cherry picked from commit 20fbf8a)
1 parent a6a856f commit 6a45811

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
@@ -1679,6 +1679,7 @@ Artur Zaprzala
16791679
Mike Zarnstorff
16801680
Yury V. Zaytsev
16811681
Siebren van der Zee
1682+
Christophe Zeitouny
16821683
Nickolai Zeldovich
16831684
Yuxiao Zeng
16841685
Uwe Zessin

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ Extension Modules
4646
Library
4747
-------
4848

49+
- bpo-29884: faulthandler: Restore the old sigaltstack during teardown.
50+
Patch by Christophe Zeitouny.
51+
4952
- bpo-25455: Fixed crashes in repr of recursive buffered file-like objects.
5053

5154
- 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 unsigned char faulthandler_nsignals = \
124124

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

129130

@@ -1148,7 +1149,7 @@ int _PyFaulthandler_Init(void)
11481149
stack.ss_size = SIGSTKSZ;
11491150
stack.ss_sp = PyMem_Malloc(stack.ss_size);
11501151
if (stack.ss_sp != NULL) {
1151-
err = sigaltstack(&stack, NULL);
1152+
err = sigaltstack(&stack, &old_stack);
11521153
if (err) {
11531154
PyMem_Free(stack.ss_sp);
11541155
stack.ss_sp = NULL;
@@ -1204,6 +1205,20 @@ void _PyFaulthandler_Fini(void)
12041205
faulthandler_disable();
12051206
#ifdef HAVE_SIGALTSTACK
12061207
if (stack.ss_sp != NULL) {
1208+
/* Fetch the current alt stack */
1209+
stack_t current_stack;
1210+
if (sigaltstack(NULL, &current_stack) == 0) {
1211+
if (current_stack.ss_sp == stack.ss_sp) {
1212+
/* The current alt stack is the one that we installed.
1213+
It is safe to restore the old stack that we found when
1214+
we installed ours */
1215+
sigaltstack(&old_stack, NULL);
1216+
} else {
1217+
/* Someone switched to a different alt stack and didn't
1218+
restore ours when they were done (if they're done).
1219+
There's not much we can do in this unlikely case */
1220+
}
1221+
}
12071222
PyMem_Free(stack.ss_sp);
12081223
stack.ss_sp = NULL;
12091224
}

0 commit comments

Comments
 (0)