faulthandler prints Python tracebacks after fatal errors, including some violations detected at the native level. It is valuable when a process stops without an ordinary Python exception.
Practical example
import faulthandler
import sys
faulthandler.enable(file=sys.stderr, all_threads=True)
print("fatal error tracebacks enabled")
## Command-line alternative: python -X faulthandler app.py
Enable it before the failure
Use -X faulthandler or PYTHONFAULTHANDLER to enable it during startup. In a service, send output to a descriptor that remains open for the process lifetime.
Investigate hangs
dump_traceback_later() can emit thread stacks after a timeout and help locate a deadlock. Cancel the timer when the operation completes to avoid a misleading report.
Operational security
Tracebacks expose paths, function names, and internal flow. Protect diagnostic files, limit retention, and never return them directly in HTTP responses.
Keep learning
Strengthen the foundation with Python logging. The official Python documentation, accessed July 22, 2026, documents the API, limitations, and version differences.