On this page
  1. faulthandler
  2. Import
  3. Overview
  4. Quick Start
  5. Implementation Walkthrough
  6. 1. Catching Deadlocked Tests or Hung Services (dump_traceback_later)
  7. 2. Zero-Code Activation via CLI Flag
  8. Gotchas & Boundaries
  9. Common Functions

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

Common Functions

Linked from

Related notes