On this page
faulthandler
Prev: errno | Up: stdlib_module | Next: fcntl
Rel: typing, annotationlib, pydoc, pydoc_data, doctest
Ref: docs.python.org — faulthandler
Tags: #public #python3 #stdlib
Import
import faulthandler
Overview
Dumps low-level Python tracebacks for all threads on fatal C crashes (SIGSEGV, SIGFPE, SIGABRT, SIGBUS), deadlocks (dump_traceback_later), or on-demand signals (register). Invaluable when debugging ctypes segfaults, C extensions, or hung multithreaded CI tests.
Quick Start
import faulthandler
faulthandler.enable()
assert faulthandler.is_enabled()
faulthandler.disable()
Implementation Walkthrough
1. Catching Deadlocked Tests or Hung Services (dump_traceback_later)
Automatically print stack traces of every thread if a test or critical section hangs longer than a timeout:
import faulthandler
faulthandler.dump_traceback_later(timeout=30, repeat=False)
try:
# potentially blocking operation
pass
finally:
faulthandler.cancel_dump_traceback_later()
2. Zero-Code Activation via CLI Flag
python3 -X faulthandler my_script.py
Gotchas & Boundaries
- Outputs directly to the underlying OS file descriptor (bypassing Python's
sys.stderrbuffer), so it still works during a C segfault. - Always call
cancel_dump_traceback_later()infinally:once the watched section finishes. PYTHONFAULTHANDLER=1or-X faulthandlerenables crash tracebacks globally without touching source code.
Common Functions
- faulthandler.cancel_dump_traceback_later() —
cancel_dump_traceback_later()— Cancel the previous call to dump_traceback_later(). - faulthandler.disable() —
disable()— Disable the fault handler. - faulthandler.dump_c_stack() —
dump_c_stack(...)— Dump the C stack of the current thread. - faulthandler.dump_traceback() —
dump_traceback(...)— Dump the traceback of the current thread, or of all threads if all_threads is True, into file. - faulthandler.dump_traceback_later() —
dump_traceback_later(...)— Dump the traceback of all threads in timeout seconds, or each timeout seconds if repeat is True. If exit is True, call _exit(1) which is not safe. - faulthandler.enable() —
enable(...)— Enable the fault handler. - faulthandler.is_enabled() —
is_enabled()— Check if the handler is enabled. - faulthandler.register() —
register(...)— Register a handler for the signal 'signum': dump the traceback of the current thread, or of all threads if all_threads is True, into file. - faulthandler.unregister() —
unregister(signum, /)— Unregister the handler of the signal 'signum' registered by register().
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/errno
- tuts/python3/stdlib_module/faulthandler/cancel_dump_traceback_later
- tuts/python3/stdlib_module/faulthandler/disable
- tuts/python3/stdlib_module/faulthandler/dump_c_stack
- tuts/python3/stdlib_module/faulthandler/dump_traceback
- tuts/python3/stdlib_module/faulthandler/dump_traceback_later
- tuts/python3/stdlib_module/faulthandler/enable
- tuts/python3/stdlib_module/faulthandler/is_enabled
- tuts/python3/stdlib_module/faulthandler/register
- tuts/python3/stdlib_module/faulthandler/unregister
- tuts/python3/stdlib_module/fcntl
- tuts/python3/stdlib_module/stdlib_module
Related notes
- tuts/python3/stdlib_module/errno Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/faulthandler/cancel_dump_traceback_later Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/faulthandler/disable Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/faulthandler/dump_c_stack Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/faulthandler/dump_traceback Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/faulthandler/dump_traceback_later Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/faulthandler/enable Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/faulthandler/is_enabled Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/faulthandler/register Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/faulthandler/unregister Linked here; links to this page; shared tags: #stdlib