Skip to content

Commit 3a8f8ea

Browse files
authored
bpo-30125: Fix faulthandler.disable() on Windows (#1243)
On Windows, faulthandler.disable() now removes the exception handler installed by faulthandler.enable().
1 parent 1ccbe60 commit 3a8f8ea

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

Lib/test/test_faulthandler.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,18 @@ def test_raise_exception(self):
755755
3,
756756
name)
757757

758+
@unittest.skipUnless(MS_WINDOWS, 'specific to Windows')
759+
def test_disable_windows_exc_handler(self):
760+
code = dedent("""
761+
import faulthandler
762+
faulthandler.enable()
763+
faulthandler.disable()
764+
code = faulthandler._EXCEPTION_ACCESS_VIOLATION
765+
faulthandler._raise_exception(code)
766+
""")
767+
output, exitcode = self.get_output(code)
768+
self.assertEqual(output, [])
769+
self.assertEqual(exitcode, 0xC0000005)
758770

759771

760772
if __name__ == "__main__":

Modules/faulthandler.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ static struct {
5555
int fd;
5656
int all_threads;
5757
PyInterpreterState *interp;
58+
#ifdef MS_WINDOWS
59+
void *exc_handler;
60+
#endif
5861
} fatal_error = {0, NULL, -1, 0};
5962

6063
#ifdef FAULTHANDLER_LATER
@@ -462,7 +465,8 @@ faulthandler_enable(void)
462465
}
463466

464467
#ifdef MS_WINDOWS
465-
AddVectoredExceptionHandler(1, faulthandler_exc_handler);
468+
assert(fatal_error.exc_handler == NULL);
469+
fatal_error.exc_handler = AddVectoredExceptionHandler(1, faulthandler_exc_handler);
466470
#endif
467471
return 0;
468472
}
@@ -514,7 +518,12 @@ faulthandler_disable(void)
514518
faulthandler_disable_fatal_handler(handler);
515519
}
516520
}
517-
521+
#ifdef MS_WINDOWS
522+
if (fatal_error.exc_handler != NULL) {
523+
RemoveVectoredExceptionHandler(fatal_error.exc_handler);
524+
fatal_error.exc_handler = NULL;
525+
}
526+
#endif
518527
Py_CLEAR(fatal_error.file);
519528
}
520529

0 commit comments

Comments
 (0)