Debian Codegraph @

Lib/test/test_faulthandler.py

raw
1 from contextlib import contextmanager
2 import datetime
3 import faulthandler
4 import os
5 import re
6 import signal
7 import subprocess
8 import sys
9 from test import support
10 from test.support import os_helper, script_helper, is_android, MS_WINDOWS, threading_helper
11 import tempfile
12 import unittest
13 from textwrap import dedent
14
15 try:
16 import _testcapi
17 except ImportError:
18 _testcapi = None
19
20 if not support.has_subprocess_support:
21 raise unittest.SkipTest("test module requires subprocess")
22
23 TIMEOUT = 0.5
24
25 STACK_HEADER_STR = r'Stack (most recent call first):'
26
27 # Regular expressions
28 STACK_HEADER = re.escape(STACK_HEADER_STR)
29 THREAD_NAME = r'( \[.*\])?'
30 THREAD_ID = fr'Thread 0x[0-9a-f]+{THREAD_NAME}'
31 THREAD_HEADER = fr'{THREAD_ID} \(most recent call first\):'
32 CURRENT_THREAD_ID = fr'Current thread 0x[0-9a-f]+{THREAD_NAME}'
33 CURRENT_THREAD_HEADER = fr'{CURRENT_THREAD_ID} \(most recent call first\):'
34
35
36 def expected_traceback(lineno1, lineno2, header, min_count=1):
37 regex = header
38 regex += ' File "<string>", line %s in func\n' % lineno1
39 regex += ' File "<string>", line %s in <module>' % lineno2
40 if 1 < min_count:
41 return '^' + (regex + '\n') * (min_count - 1) + regex
42 else:
43 return '^' + regex + '$'
44
45 def skip_segfault_on_android(test):
46 # gh-76319: Raising SIGSEGV on Android may not cause a crash.
47 return unittest.skipIf(is_android,
48 'raising SIGSEGV on Android is unreliable')(test)
49
50 @contextmanager
51 def temporary_filename():
52 filename = tempfile.mktemp()
53 try:
54 yield filename
55 finally:
56 os_helper.unlink(filename)
57
58
59 ADDRESS_EXPR = "0x[0-9a-f]+"
60 C_STACK_REGEX = [
61 r"Current thread's C stack trace \(most recent call first\):",
62 fr'( Binary file ".+"(, at .*(\+|-){ADDRESS_EXPR})? \[{ADDRESS_EXPR}\])|(<.+>)'
63 ]
64
65 class FaultHandlerTests(unittest.TestCase):
66
67 def get_output(self, code, filename=None, fd=None):
68 """
69 Run the specified code in Python (in a new child process) and read the
70 output from the standard error or from a file (if filename is set).
71 Return the output lines as a list.
72
73 Strip the reference count from the standard error for Python debug
74 build, and replace "Current thread 0x00007f8d8fbd9700" by "Current
75 thread XXX".
76 """
77 code = dedent(code).strip()
78 pass_fds = []
79 if fd is not None:
80 pass_fds.append(fd)
81 env = dict(os.environ)
82
83 # Sanitizers must not handle SIGSEGV (ex: for test_enable_fd())
84 option = 'handle_segv=0'
85 support.set_sanitizer_env_var(env, option)
86
87 with support.SuppressCrashReport():
88 process = script_helper.spawn_python('-c', code,
89 pass_fds=pass_fds,
90 env=env)
91 with process:
92 output, stderr = process.communicate()
93 exitcode = process.wait()
94 output = output.decode('ascii', 'backslashreplace')
95 if filename:
96 self.assertEqual(output, '')
97 with open(filename, "rb") as fp:
98 output = fp.read()
99 output = output.decode('ascii', 'backslashreplace')
100 elif fd is not None:
101 self.assertEqual(output, '')
102 os.lseek(fd, os.SEEK_SET, 0)
103 with open(fd, "rb", closefd=False) as fp:
104 output = fp.read()
105 output = output.decode('ascii', 'backslashreplace')
106 return output.splitlines(), exitcode
107
108 def check_error(self, code, lineno, fatal_error, *,
109 filename=None, all_threads=True, other_regex=None,
110 fd=None, know_current_thread=True,
111 py_fatal_error=False,
112 garbage_collecting=False,
113 c_stack=True,
114 function='<module>'):
115 """
116 Check that the fault handler for fatal errors is enabled and check the
117 traceback from the child process output.
118
119 Raise an error if the output doesn't match the expected format.
120 """
121 all_threads_disabled = (
122 all_threads
123 and (not sys._is_gil_enabled())
124 )
125 if all_threads and not all_threads_disabled:
126 if know_current_thread:
127 header = CURRENT_THREAD_HEADER
128 else:
129 header = THREAD_HEADER
130 else:
131 header = STACK_HEADER
132 regex = [f'^{fatal_error}']
133 if py_fatal_error:
134 regex.append("Python runtime state: initialized")
135 regex.append('')
136 if all_threads_disabled and not py_fatal_error:
137 regex.append("<Cannot show all threads while the GIL is disabled>")
138 regex.append(fr'{header}')
139 if support.Py_GIL_DISABLED and py_fatal_error and not know_current_thread:
140 regex.append(" <tstate is freed>")
141 else:
142 if garbage_collecting and not all_threads_disabled:
143 regex.append(' Garbage-collecting')
144 regex.append(fr' File "<string>", line {lineno} in {function}')
145 if c_stack:
146 regex.extend(C_STACK_REGEX)
147 regex = '\n'.join(regex)
148
149 if other_regex:
150 regex = f'(?:{regex}|{other_regex})'
151
152 # Enable MULTILINE flag
153 regex = f'(?m){regex}'
154 output, exitcode = self.get_output(code, filename=filename, fd=fd)
155 output = '\n'.join(output)
156 self.assertRegex(output, regex)
157 self.assertNotEqual(exitcode, 0)
158
159 def check_fatal_error(self, code, line_number, name_regex, func=None, **kw):
160 if func:
161 name_regex = '%s: %s' % (func, name_regex)
162 fatal_error = 'Fatal Python error: %s' % name_regex
163 self.check_error(code, line_number, fatal_error, **kw)
164
165 def check_windows_exception(self, code, line_number, name_regex, **kw):
166 fatal_error = 'Windows fatal exception: %s' % name_regex
167 self.check_error(code, line_number, fatal_error, **kw)
168
169 @unittest.skipIf(sys.platform.startswith('aix'),
170 "the first page of memory is a mapped read-only on AIX")
171 def test_read_null(self):
172 if not MS_WINDOWS:
173 self.check_fatal_error("""
174 import faulthandler
175 faulthandler.enable()
176 faulthandler._read_null()
177 """,
178 3,
179 # Issue #12700: Read NULL raises SIGILL on Mac OS X Lion
180 '(?:Segmentation fault'
181 '|Bus error'
182 '|Illegal instruction)')
183 else:
184 self.check_windows_exception("""
185 import faulthandler
186 faulthandler.enable()
187 faulthandler._read_null()
188 """,
189 3,
190 'access violation')
191
192 @skip_segfault_on_android
193 def test_sigsegv(self):
194 self.check_fatal_error("""
195 import faulthandler
196 faulthandler.enable()
197 faulthandler._sigsegv()
198 """,
199 3,
200 'Segmentation fault')
201
202 @skip_segfault_on_android
203 def test_gc(self):
204 # bpo-44466: Detect if the GC is running
205 self.check_fatal_error("""
206 import faulthandler
207 import gc
208 import sys
209
210 faulthandler.enable()
211
212 class RefCycle:
213 def __del__(self):
214 faulthandler._sigsegv()
215
216 # create a reference cycle which triggers a fatal
217 # error in a destructor
218 a = RefCycle()
219 b = RefCycle()
220 a.b = b
221 b.a = a
222
223 # Delete the objects, not the cycle
224 a = None
225 b = None
226
227 # Break the reference cycle: call __del__()
228 gc.collect()
229
230 # Should not reach this line
231 print("exit", file=sys.stderr)
232 """,
233 9,
234 'Segmentation fault',
235 function='__del__',
236 garbage_collecting=True)
237
238 def test_fatal_error_c_thread(self):
239 self.check_fatal_error("""
240 import faulthandler
241 faulthandler.enable()
242 faulthandler._fatal_error_c_thread()
243 """,
244 3,
245 'in new thread',
246 know_current_thread=False,
247 func='faulthandler_fatal_error_thread',
248 py_fatal_error=True)
249
250 @support.skip_if_sanitizer("TSAN itercepts SIGABRT", thread=True)
251 def test_sigabrt(self):
252 self.check_fatal_error("""
253 import faulthandler
254 faulthandler.enable()
255 faulthandler._sigabrt()
256 """,
257 3,
258 'Aborted')
259
260 @unittest.skipIf(sys.platform == 'win32',
261 "SIGFPE cannot be caught on Windows")
262 @support.skip_if_sanitizer("TSAN itercepts SIGFPE", thread=True)
263 def test_sigfpe(self):
264 self.check_fatal_error("""
265 import faulthandler
266 faulthandler.enable()
267 faulthandler._sigfpe()
268 """,
269 3,
270 'Floating-point exception')
271
272 @unittest.skipIf(_testcapi is None, 'need _testcapi')
273 @unittest.skipUnless(hasattr(signal, 'SIGBUS'), 'need signal.SIGBUS')
274 @support.skip_if_sanitizer("TSAN itercepts SIGBUS", thread=True)
275 @skip_segfault_on_android
276 def test_sigbus(self):
277 self.check_fatal_error("""
278 import faulthandler
279 import signal
280
281 faulthandler.enable()
282 signal.raise_signal(signal.SIGBUS)
283 """,
284 5,
285 'Bus error')
286
287 @unittest.skipIf(_testcapi is None, 'need _testcapi')
288 @unittest.skipUnless(hasattr(signal, 'SIGILL'), 'need signal.SIGILL')
289 @support.skip_if_sanitizer("TSAN itercepts SIGILL", thread=True)
290 @skip_segfault_on_android
291 def test_sigill(self):
292 self.check_fatal_error("""
293 import faulthandler
294 import signal
295
296 faulthandler.enable()
297 signal.raise_signal(signal.SIGILL)
298 """,
299 5,
300 'Illegal instruction')
301
302 @unittest.skipIf(_testcapi is None, 'need _testcapi')
303 def check_fatal_error_func(self, release_gil):
304 # Test that Py_FatalError() dumps a traceback
305 with support.SuppressCrashReport():
306 self.check_fatal_error(f"""
307 import _testcapi
308 _testcapi.fatal_error(b'xyz', {release_gil})
309 """,
310 2,
311 'xyz',
312 func='_testcapi_fatal_error_impl',
313 py_fatal_error=True)
314
315 def test_fatal_error(self):
316 self.check_fatal_error_func(False)
317
318 def test_fatal_error_without_gil(self):
319 self.check_fatal_error_func(True)
320
321 @unittest.skipIf(sys.platform.startswith('openbsd'),
322 "Issue #12868: sigaltstack() doesn't work on "
323 "OpenBSD if Python is compiled with pthread")
324 @unittest.skipIf(not hasattr(faulthandler, '_stack_overflow'),
325 'need faulthandler._stack_overflow()')
326 def test_stack_overflow(self):
327 self.check_fatal_error("""
328 import faulthandler
329 faulthandler.enable()
330 faulthandler._stack_overflow()
331 """,
332 3,
333 '(?:Segmentation fault|Bus error)',
334 other_regex='unable to raise a stack overflow')
335
336 @skip_segfault_on_android
337 def test_gil_released(self):
338 self.check_fatal_error("""
339 import faulthandler
340 faulthandler.enable()
341 faulthandler._sigsegv(True)
342 """,
343 3,
344 'Segmentation fault')
345
346 @skip_segfault_on_android
347 def test_enable_file(self):
348 with temporary_filename() as filename:
349 self.check_fatal_error("""
350 import faulthandler
351 output = open({filename}, 'wb')
352 faulthandler.enable(output)
353 faulthandler._sigsegv()
354 """.format(filename=repr(filename)),
355 4,
356 'Segmentation fault',
357 filename=filename)
358
359 @unittest.skipIf(sys.platform == "win32",
360 "subprocess doesn't support pass_fds on Windows")
361 @skip_segfault_on_android
362 def test_enable_fd(self):
363 with tempfile.TemporaryFile('wb+') as fp:
364 fd = fp.fileno()
365 self.check_fatal_error("""
366 import faulthandler
367 import sys
368 faulthandler.enable(%s)
369 faulthandler._sigsegv()
370 """ % fd,
371 4,
372 'Segmentation fault',
373 fd=fd)
374
375 @skip_segfault_on_android
376 def test_enable_single_thread(self):
377 self.check_fatal_error("""
378 import faulthandler
379 faulthandler.enable(all_threads=False)
380 faulthandler._sigsegv()
381 """,
382 3,
383 'Segmentation fault',
384 all_threads=False)
385
386 @skip_segfault_on_android
387 def test_disable(self):
388 code = """
389 import faulthandler
390 faulthandler.enable()
391 faulthandler.disable()
392 faulthandler._sigsegv()
393 """
394 not_expected = 'Fatal Python error'
395 stderr, exitcode = self.get_output(code)
396 stderr = '\n'.join(stderr)
397 self.assertTrue(not_expected not in stderr,
398 "%r is present in %r" % (not_expected, stderr))
399 self.assertNotEqual(exitcode, 0)
400
401 @skip_segfault_on_android
402 def test_dump_ext_modules(self):
403 code = """
404 import faulthandler
405 import sys
406 # Don't filter stdlib module names
407 sys.stdlib_module_names = frozenset()
408 faulthandler.enable()
409 faulthandler._sigsegv()
410 """
411 stderr, exitcode = self.get_output(code)
412 stderr = '\n'.join(stderr)
413 match = re.search(r'^Extension modules:(.*) \(total: [0-9]+\)$',
414 stderr, re.MULTILINE)
415 if not match:
416 self.fail(f"Cannot find 'Extension modules:' in {stderr!r}")
417 modules = set(match.group(1).strip().split(', '))
418 for name in ('sys', 'faulthandler'):
419 self.assertIn(name, modules)
420
421 def test_is_enabled(self):
422 orig_stderr = sys.stderr
423 try:
424 # regrtest may replace sys.stderr by io.StringIO object, but
425 # faulthandler.enable() requires that sys.stderr has a fileno()
426 # method
427 sys.stderr = sys.__stderr__
428
429 was_enabled = faulthandler.is_enabled()
430 try:
431 faulthandler.enable()
432 self.assertTrue(faulthandler.is_enabled())
433 faulthandler.disable()
434 self.assertFalse(faulthandler.is_enabled())
435 finally:
436 if was_enabled:
437 faulthandler.enable()
438 else:
439 faulthandler.disable()
440 finally:
441 sys.stderr = orig_stderr
442
443 @support.requires_subprocess()
444 def test_disabled_by_default(self):
445 # By default, the module should be disabled
446 code = "import faulthandler; print(faulthandler.is_enabled())"
447 args = (sys.executable, "-E", "-c", code)
448 # don't use assert_python_ok() because it always enables faulthandler
449 output = subprocess.check_output(args)
450 self.assertEqual(output.rstrip(), b"False")
451
452 @support.requires_subprocess()
453 def test_sys_xoptions(self):
454 # Test python -X faulthandler
455 code = "import faulthandler; print(faulthandler.is_enabled())"
456 args = filter(None, (sys.executable,
457 "-E" if sys.flags.ignore_environment else "",
458 "-X", "faulthandler", "-c", code))
459 env = os.environ.copy()
460 env.pop("PYTHONFAULTHANDLER", None)
461 # don't use assert_python_ok() because it always enables faulthandler
462 output = subprocess.check_output(args, env=env)
463 self.assertEqual(output.rstrip(), b"True")
464
465 @support.requires_subprocess()
466 def test_env_var(self):
467 # empty env var
468 code = "import faulthandler; print(faulthandler.is_enabled())"
469 args = (sys.executable, "-c", code)
470 env = dict(os.environ)
471 env['PYTHONFAULTHANDLER'] = ''
472 env['PYTHONDEVMODE'] = ''
473 # don't use assert_python_ok() because it always enables faulthandler
474 output = subprocess.check_output(args, env=env)
475 self.assertEqual(output.rstrip(), b"False")
476
477 # non-empty env var
478 env = dict(os.environ)
479 env['PYTHONFAULTHANDLER'] = '1'
480 env['PYTHONDEVMODE'] = ''
481 output = subprocess.check_output(args, env=env)
482 self.assertEqual(output.rstrip(), b"True")
483
484 def check_dump_traceback(self, *, filename=None, fd=None):
485 """
486 Explicitly call dump_traceback() function and check its output.
487 Raise an error if the output doesn't match the expected format.
488 """
489 code = """
490 import faulthandler
491
492 filename = {filename!r}
493 fd = {fd}
494
495 def funcB():
496 if filename:
497 with open(filename, "wb") as fp:
498 faulthandler.dump_traceback(fp, all_threads=False)
499 elif fd is not None:
500 faulthandler.dump_traceback(fd,
501 all_threads=False)
502 else:
503 faulthandler.dump_traceback(all_threads=False)
504
505 def funcA():
506 funcB()
507
508 funcA()
509 """
510 code = code.format(
511 filename=filename,
512 fd=fd,
513 )
514 if filename:
515 lineno = 9
516 elif fd is not None:
517 lineno = 11
518 else:
519 lineno = 14
520 expected = [
521 f'{STACK_HEADER_STR}',
522 ' File "<string>", line %s in funcB' % lineno,
523 ' File "<string>", line 17 in funcA',
524 ' File "<string>", line 19 in <module>'
525 ]
526 trace, exitcode = self.get_output(code, filename, fd)
527 self.assertEqual(trace, expected)
528 self.assertEqual(exitcode, 0)
529
530 def test_dump_traceback(self):
531 self.check_dump_traceback()
532
533 def test_dump_traceback_file(self):
534 with temporary_filename() as filename:
535 self.check_dump_traceback(filename=filename)
536
537 @unittest.skipIf(sys.platform == "win32",
538 "subprocess doesn't support pass_fds on Windows")
539 def test_dump_traceback_fd(self):
540 with tempfile.TemporaryFile('wb+') as fp:
541 self.check_dump_traceback(fd=fp.fileno())
542
543 def test_truncate(self):
544 maxlen = 500
545 func_name = 'x' * (maxlen + 50)
546 truncated = 'x' * maxlen + '...'
547 code = """
548 import faulthandler
549
550 def {func_name}():
551 faulthandler.dump_traceback(all_threads=False)
552
553 {func_name}()
554 """
555 code = code.format(
556 func_name=func_name,
557 )
558 expected = [
559 f'{STACK_HEADER_STR}',
560 ' File "<string>", line 4 in %s' % truncated,
561 ' File "<string>", line 6 in <module>'
562 ]
563 trace, exitcode = self.get_output(code)
564 self.assertEqual(trace, expected)
565 self.assertEqual(exitcode, 0)
566
567 def check_dump_traceback_threads(self, filename):
568 """
569 Call explicitly dump_traceback(all_threads=True) and check the output.
570 Raise an error if the output doesn't match the expected format.
571 """
572 code = """
573 import faulthandler
574 from threading import Thread, Event
575 import time
576
577 def dump():
578 if {filename}:
579 with open({filename}, "wb") as fp:
580 faulthandler.dump_traceback(fp, all_threads=True)
581 else:
582 faulthandler.dump_traceback(all_threads=True)
583
584 class Waiter(Thread):
585 # avoid blocking if the main thread raises an exception.
586 daemon = True
587
588 def __init__(self):
589 Thread.__init__(self)
590 self.running = Event()
591 self.stop = Event()
592
593 def run(self):
594 self.running.set()
595 self.stop.wait()
596
597 waiter = Waiter()
598 waiter.start()
599 waiter.running.wait()
600 dump()
601 waiter.stop.set()
602 waiter.join()
603 """
604 code = code.format(filename=repr(filename))
605 output, exitcode = self.get_output(code, filename)
606 output = '\n'.join(output)
607 if filename:
608 lineno = 8
609 else:
610 lineno = 10
611 # When the traceback is dumped, the waiter thread may be in the
612 # `self.running.set()` call or in `self.stop.wait()`.
613 regex = fr"""
614 ^{THREAD_HEADER}
615 (?: File ".*threading.py", line [0-9]+ in [_a-z]+
616 ){{1,3}} File "<string>", line (?:22|23) in run
617 File ".*threading.py", line [0-9]+ in _bootstrap_inner
618 File ".*threading.py", line [0-9]+ in _bootstrap
619
620 {CURRENT_THREAD_HEADER}
621 File "<string>", line {lineno} in dump
622 File "<string>", line 28 in <module>$
623 """
624 regex = dedent(regex).strip()
625 self.assertRegex(output, regex)
626 self.assertEqual(exitcode, 0)
627
628 def test_dump_traceback_threads(self):
629 self.check_dump_traceback_threads(None)
630
631 def test_dump_traceback_threads_file(self):
632 with temporary_filename() as filename:
633 self.check_dump_traceback_threads(filename)
634
635 def check_dump_traceback_later(self, repeat=False, cancel=False, loops=1,
636 *, filename=None, fd=None):
637 """
638 Check how many times the traceback is written in timeout x 2.5 seconds,
639 or timeout x 3.5 seconds if cancel is True: 1, 2 or 3 times depending
640 on repeat and cancel options.
641
642 Raise an error if the output doesn't match the expect format.
643 """
644 timeout_str = str(datetime.timedelta(seconds=TIMEOUT))
645 code = """
646 import faulthandler
647 import time
648 import sys
649
650 timeout = {timeout}
651 repeat = {repeat}
652 cancel = {cancel}
653 loops = {loops}
654 filename = {filename!r}
655 fd = {fd}
656
657 def func(timeout, repeat, cancel, file, loops):
658 for loop in range(loops):
659 faulthandler.dump_traceback_later(timeout, repeat=repeat, file=file)
660 if cancel:
661 faulthandler.cancel_dump_traceback_later()
662 time.sleep(timeout * 5)
663 faulthandler.cancel_dump_traceback_later()
664
665 if filename:
666 file = open(filename, "wb")
667 elif fd is not None:
668 file = sys.stderr.fileno()
669 else:
670 file = None
671 func(timeout, repeat, cancel, file, loops)
672 if filename:
673 file.close()
674 """
675 code = code.format(
676 timeout=TIMEOUT,
677 repeat=repeat,
678 cancel=cancel,
679 loops=loops,
680 filename=filename,
681 fd=fd,
682 )
683 trace, exitcode = self.get_output(code, filename)
684 trace = '\n'.join(trace)
685
686 if not cancel:
687 count = loops
688 if repeat:
689 count *= 2
690 header = (fr'Timeout \({timeout_str}\)!\n'
691 fr'{THREAD_HEADER}\n')
692 regex = expected_traceback(17, 26, header, min_count=count)
693 self.assertRegex(trace, regex)
694 else:
695 self.assertEqual(trace, '')
696 self.assertEqual(exitcode, 0)
697
698 def test_dump_traceback_later(self):
699 self.check_dump_traceback_later()
700
701 def test_dump_traceback_later_repeat(self):
702 self.check_dump_traceback_later(repeat=True)
703
704 def test_dump_traceback_later_cancel(self):
705 self.check_dump_traceback_later(cancel=True)
706
707 def test_dump_traceback_later_file(self):
708 with temporary_filename() as filename:
709 self.check_dump_traceback_later(filename=filename)
710
711 @unittest.skipIf(sys.platform == "win32",
712 "subprocess doesn't support pass_fds on Windows")
713 def test_dump_traceback_later_fd(self):
714 with tempfile.TemporaryFile('wb+') as fp:
715 self.check_dump_traceback_later(fd=fp.fileno())
716
717 @support.requires_resource('walltime')
718 def test_dump_traceback_later_twice(self):
719 self.check_dump_traceback_later(loops=2)
720
721 @unittest.skipIf(not hasattr(faulthandler, "register"),
722 "need faulthandler.register")
723 def check_register(self, filename=False, all_threads=False,
724 unregister=False, chain=False, fd=None):
725 """
726 Register a handler displaying the traceback on a user signal. Raise the
727 signal and check the written traceback.
728
729 If chain is True, check that the previous signal handler is called.
730
731 Raise an error if the output doesn't match the expected format.
732 """
733 signum = signal.SIGUSR1
734 code = """
735 import faulthandler
736 import os
737 import signal
738 import sys
739
740 all_threads = {all_threads}
741 signum = {signum:d}
742 unregister = {unregister}
743 chain = {chain}
744 filename = {filename!r}
745 fd = {fd}
746
747 def func(signum):
748 os.kill(os.getpid(), signum)
749
750 def handler(signum, frame):
751 handler.called = True
752 handler.called = False
753
754 if filename:
755 file = open(filename, "wb")
756 elif fd is not None:
757 file = sys.stderr.fileno()
758 else:
759 file = None
760 if chain:
761 signal.signal(signum, handler)
762 faulthandler.register(signum, file=file,
763 all_threads=all_threads, chain={chain})
764 if unregister:
765 faulthandler.unregister(signum)
766 func(signum)
767 if chain and not handler.called:
768 if file is not None:
769 output = file
770 else:
771 output = sys.stderr
772 print("Error: signal handler not called!", file=output)
773 exitcode = 1
774 else:
775 exitcode = 0
776 if filename:
777 file.close()
778 sys.exit(exitcode)
779 """
780 code = code.format(
781 all_threads=all_threads,
782 signum=signum,
783 unregister=unregister,
784 chain=chain,
785 filename=filename,
786 fd=fd,
787 )
788 trace, exitcode = self.get_output(code, filename)
789 trace = '\n'.join(trace)
790 if not unregister:
791 if all_threads:
792 regex = fr'{CURRENT_THREAD_HEADER}\n'
793 else:
794 regex = fr'{STACK_HEADER}\n'
795 regex = expected_traceback(14, 32, regex)
796 self.assertRegex(trace, regex)
797 else:
798 self.assertEqual(trace, '')
799 if unregister:
800 self.assertNotEqual(exitcode, 0)
801 else:
802 self.assertEqual(exitcode, 0)
803
804 def test_register(self):
805 self.check_register()
806
807 def test_unregister(self):
808 self.check_register(unregister=True)
809
810 def test_register_file(self):
811 with temporary_filename() as filename:
812 self.check_register(filename=filename)
813
814 @unittest.skipIf(sys.platform == "win32",
815 "subprocess doesn't support pass_fds on Windows")
816 def test_register_fd(self):
817 with tempfile.TemporaryFile('wb+') as fp:
818 self.check_register(fd=fp.fileno())
819
820 def test_register_threads(self):
821 self.check_register(all_threads=True)
822
823 @support.skip_if_sanitizer("gh-129825: hangs under TSAN", thread=True)
824 def test_register_chain(self):
825 self.check_register(chain=True)
826
827 @contextmanager
828 def check_stderr_none(self):
829 stderr = sys.stderr
830 try:
831 sys.stderr = None
832 with self.assertRaises(RuntimeError) as cm:
833 yield
834 self.assertEqual(str(cm.exception), "sys.stderr is None")
835 finally:
836 sys.stderr = stderr
837
838 def test_stderr_None(self):
839 # Issue #21497: provide a helpful error if sys.stderr is None,
840 # instead of just an attribute error: "None has no attribute fileno".
841 with self.check_stderr_none():
842 faulthandler.enable()
843 with self.check_stderr_none():
844 faulthandler.dump_traceback()
845 with self.check_stderr_none():
846 faulthandler.dump_traceback_later(1e-3)
847 if hasattr(faulthandler, "register"):
848 with self.check_stderr_none():
849 faulthandler.register(signal.SIGUSR1)
850
851 @unittest.skipUnless(MS_WINDOWS, 'specific to Windows')
852 def test_raise_exception(self):
853 for exc, name in (
854 ('EXCEPTION_ACCESS_VIOLATION', 'access violation'),
855 ('EXCEPTION_INT_DIVIDE_BY_ZERO', 'int divide by zero'),
856 ('EXCEPTION_STACK_OVERFLOW', 'stack overflow'),
857 ):
858 self.check_windows_exception(f"""
859 import faulthandler
860 faulthandler.enable()
861 faulthandler._raise_exception(faulthandler._{exc})
862 """,
863 3,
864 name)
865
866 @unittest.skipUnless(MS_WINDOWS, 'specific to Windows')
867 def test_ignore_exception(self):
868 for exc_code in (
869 0xE06D7363, # MSC exception ("Emsc")
870 0xE0434352, # COM Callable Runtime exception ("ECCR")
871 ):
872 code = f"""
873 import faulthandler
874 faulthandler.enable()
875 faulthandler._raise_exception({exc_code})
876 """
877 code = dedent(code)
878 output, exitcode = self.get_output(code)
879 self.assertEqual(output, [])
880 self.assertEqual(exitcode, exc_code)
881
882 @unittest.skipUnless(MS_WINDOWS, 'specific to Windows')
883 def test_raise_nonfatal_exception(self):
884 # These exceptions are not strictly errors. Letting
885 # faulthandler display the traceback when they are
886 # raised is likely to result in noise. However, they
887 # may still terminate the process if there is no
888 # handler installed for them (which there typically
889 # is, e.g. for debug messages).
890 for exc in (
891 0x00000000,
892 0x34567890,
893 0x40000000,
894 0x40001000,
895 0x70000000,
896 0x7FFFFFFF,
897 ):
898 output, exitcode = self.get_output(f"""
899 import faulthandler
900 faulthandler.enable()
901 faulthandler._raise_exception(0x{exc:x})
902 """
903 )
904 self.assertEqual(output, [])
905 # On Windows older than 7 SP1, the actual exception code has
906 # bit 29 cleared.
907 self.assertIn(exitcode,
908 (exc, exc & ~0x10000000))
909
910 @unittest.skipUnless(MS_WINDOWS, 'specific to Windows')
911 def test_disable_windows_exc_handler(self):
912 code = dedent("""
913 import faulthandler
914 faulthandler.enable()
915 faulthandler.disable()
916 code = faulthandler._EXCEPTION_ACCESS_VIOLATION
917 faulthandler._raise_exception(code)
918 """)
919 output, exitcode = self.get_output(code)
920 self.assertEqual(output, [])
921 self.assertEqual(exitcode, 0xC0000005)
922
923 def test_cancel_later_without_dump_traceback_later(self):
924 # bpo-37933: Calling cancel_dump_traceback_later()
925 # without dump_traceback_later() must not segfault.
926 code = dedent("""
927 import faulthandler
928 faulthandler.cancel_dump_traceback_later()
929 """)
930 output, exitcode = self.get_output(code)
931 self.assertEqual(output, [])
932 self.assertEqual(exitcode, 0)
933
934 @threading_helper.requires_working_threading()
935 @unittest.skipUnless(support.Py_GIL_DISABLED, "only meaningful if the GIL is disabled")
936 def test_free_threaded_dump_traceback(self):
937 # gh-128400: Other threads need to be paused to invoke faulthandler
938 code = dedent("""
939 import faulthandler
940 from threading import Thread, Event
941
942 class Waiter(Thread):
943 def __init__(self):
944 Thread.__init__(self)
945 self.running = Event()
946 self.stop = Event()
947
948 def run(self):
949 self.running.set()
950 self.stop.wait()
951
952 for _ in range(100):
953 waiter = Waiter()
954 waiter.start()
955 waiter.running.wait()
956 faulthandler.dump_traceback(all_threads=True)
957 waiter.stop.set()
958 waiter.join()
959 """)
960 _, exitcode = self.get_output(code)
961 self.assertEqual(exitcode, 0)
962
963 def check_c_stack(self, output):
964 starting_line = output.pop(0)
965 self.assertRegex(starting_line, C_STACK_REGEX[0])
966 self.assertGreater(len(output), 0)
967
968 for line in output:
969 with self.subTest(line=line):
970 if line != '': # Ignore trailing or leading newlines
971 self.assertRegex(line, C_STACK_REGEX[1])
972
973
974 def test_dump_c_stack(self):
975 code = dedent("""
976 import faulthandler
977 faulthandler.dump_c_stack()
978 """)
979 output, exitcode = self.get_output(code)
980 self.assertEqual(exitcode, 0)
981 self.check_c_stack(output)
982
983
984 def test_dump_c_stack_file(self):
985 import tempfile
986
987 with tempfile.TemporaryFile("w+") as tmp:
988 faulthandler.dump_c_stack(file=tmp)
989 tmp.flush() # Just in case
990 tmp.seek(0)
991 self.check_c_stack(tmp.read().split("\n"))
992
993 if __name__ == "__main__":
994 unittest.main()