faulthandler.py


Overview

The [faulthandler.py](/projects/286/67477) file is a plugin module for the pytest testing framework that integrates Python's built-in `faulthandler` module to improve debugging of test timeouts and crashes. Its primary purpose is to enable automatic dumping of Python traceback information for all threads when a test exceeds a configured timeout. This helps developers quickly identify deadlocks, infinite loops, or other issues causing tests to hang.

Key functionalities include:

This integration enhances pytest's ability to diagnose problematic tests by leveraging faulthandler's capability to dump tracebacks of all threads to help uncover deadlocks or stuck states.


Detailed Component Descriptions

Constants / Keys


Functions

pytest_addoption(parser: Parser) -> None

Registers a pytest ini configuration option:


pytest_configure(config: Config) -> None

Configures the faulthandler plugin during pytest startup:


pytest_unconfigure(config: Config) -> None

Restores faulthandler state during pytest teardown:


get_stderr_fileno() -> int

Safely obtains a usable file descriptor for `sys.stderr` for faulthandler output:


get_timeout_config_value(config: Config) -> float

Retrieves the configured faulthandler timeout value:


Pytest Hook Implementations

pytest_runtest_protocol(item: Item) -> Generator[None, object, object]

Wraps the execution of a single test item to enable faulthandler timeout dumping:


pytest_enter_pdb() -> None

Cancels any pending faulthandler traceback dumps when entering the interactive debugger:


pytest_exception_interact() -> None

Cancels any pending faulthandler tracebacks when pytest enters exception interaction mode:


Important Implementation Details


Interaction with Other Components


Usage Example

Assuming you have this plugin enabled in your pytest environment, you can configure a timeout in your `pytest.ini`:

[pytest]
faulthandler_timeout = 10.0

When you run `pytest`, any test running longer than 10 seconds will trigger faulthandler dumping stack traces of all threads to stderr, aiding in diagnosing hangs or deadlocks.


Visual Diagram

classDiagram
    class FaulthandlerPlugin {
        <<pytest plugin>>
        +pytest_addoption(parser: Parser) void
        +pytest_configure(config: Config) void
        +pytest_unconfigure(config: Config) void
        +pytest_runtest_protocol(item: Item) Generator
        +pytest_enter_pdb() void
        +pytest_exception_interact() void
        +get_stderr_fileno() int
        +get_timeout_config_value(config: Config) float
    }

    FaulthandlerPlugin : +fault_handler_original_stderr_fd_key: StashKey[int]
    FaulthandlerPlugin : +fault_handler_stderr_fd_key: StashKey[int]

    FaulthandlerPlugin ..> "faulthandler" : uses
    FaulthandlerPlugin ..> "pytest.config.Config" : interacts
    FaulthandlerPlugin ..> "sys.stderr" : reads fileno
    FaulthandlerPlugin ..> "os" : duplicates and closes fds
    FaulthandlerPlugin ..> "pytest.nodes.Item" : receives test item

Summary

The [faulthandler.py](/projects/286/67477) file is a pytest plugin module that leverages Python's `faulthandler` to improve test debugging by dumping thread tracebacks on test timeouts. It carefully manages file descriptors to ensure faulthandler output reliability even under complex test environments and integrates tightly with pytest's lifecycle hooks to enable, disable, and cancel faulthandler tracebacks as appropriate.

This plugin significantly aids developers in diagnosing stuck or hanging tests by providing detailed runtime traceback information automatically.