◐ Shell
clean mode source ↗

Faulthandler by youknowone · Pull Request #6400 · RustPython/RustPython

Walkthrough

Replaces the minimal Rust faulthandler with a full multi-threaded implementation: global enable/FD state, watchdog for delayed tracebacks, cross-platform fatal-signal/exception handlers (Unix and Windows), per-signal registration, signal-safe I/O/formatting utilities, many new public APIs and diagnostic entry points; also renames RunMode::Command source label to "".

Changes

Cohort / File(s) Summary
Faulthandler Core Rewrite
crates/stdlib/src/faulthandler.rs
Full reimplementation: added global state (ENABLED, FATAL_ERROR_FD), watchdog primitives (WatchdogState, WatchdogHandle, WATCHDOG) with scheduling (dump_traceback_later, cancel_dump_traceback_later), cross-platform fatal handlers (Unix signals and Windows exceptions), per-signal user registration/unregistration and chaining, signal-safe I/O/formatting helpers (write_str_noraise, truncate_name, collect_frame_info, format_timeout), revamped public API (dump_traceback with DumpTracebackArgs, enable with EnableArgs, disable, is_enabled, register, unregister), numerous test/diagnostic entry points (_sigsegv, _sigabrt, _sigfpe, _fatal_error_c_thread, _read_null, Windows _raise_exception), and platform-specific previous-handler plumbing (PREVIOUS_HANDLERS, user_signals).
Run Mode Source Label
src/lib.rs
Changed the source identifier for RunMode::Command from "<stdin>" to "<string>".
Windows Cargo Feature
crates/stdlib/Cargo.toml
Added Win32_System_Diagnostics_Debug to Windows-specific windows-sys feature list.

Sequence Diagram

sequenceDiagram
    participant OS as Operating System
    participant Handler as Faulthandler (signal/exception)
    participant VM as Virtual Machine
    participant Frame as Frame Inspector
    participant Output as Signal-safe Writer
    participant Prev as Previous Handler

    OS->>Handler: Deliver fatal signal/exception
    activate Handler
    Handler->>VM: Snapshot execution state
    activate VM
    VM->>Frame: Collect frames & locals
    activate Frame
    Frame-->>VM: Frame data
    deactivate Frame
    VM-->>Handler: Traceback data
    deactivate VM
    Handler->>Output: Write header and frames (async-signal-safe)
    Output-->>Handler: Write complete
    alt Chain to previous handler
        Handler->>Prev: Invoke previous handler
        Prev-->>Handler: Return
    end
    deactivate Handler
Loading
sequenceDiagram
    participant API as dump_traceback_later()
    participant Watchdog as Watchdog Thread
    participant Cond as Condvar/Mutex
    participant Timer as Timeout
    participant Handler as Faulthandler Trigger
    participant Output as Writer

    API->>Watchdog: Start/notify with timeout
    Watchdog->>Cond: Wait (with timeout)
    activate Watchdog
    Timer->>Watchdog: Timeout elapses
    Watchdog->>Handler: Request delayed traceback dump
    Handler->>Output: Write delayed traceback
    deactivate Watchdog
Loading

Estimated code review effort

🎯 5 (Critical) | ⏱️ ~120 minutes

  • Review signal-safety and async-signal-safe calls in handler paths (faulthandler_fatal_error, faulthandler_user_signal).
  • Unix vs Windows handler registration/unregistration and PREVIOUS_HANDLERS correctness.
  • Watchdog concurrency: WatchdogState, WATCHDOG Mutex/Condvar interactions, cancellation semantics.
  • Global mutable/atomic state correctness (ENABLED, FATAL_ERROR_FD) and FD handling.
  • Public API argument parsing/types (DumpTracebackArgs, EnableArgs, RegisterArgs) and VM interactions.
  • Diagnostic/test entry points that intentionally trigger crashes require careful review.

Poem

🐰 I nibbled logs and chased the thread,
Signals calm where chaos spread,
Watchdogs hum and trace the night,
Frames unfold in careful light,
"" now sings — hop, debug, delight 🥕

Pre-merge checks and finishing touches

❌ Failed checks (1 warning, 1 inconclusive)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 78.38% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
Title check ❓ Inconclusive The title 'Faulthandler' is generic and does not clearly summarize the main changes. While it relates to the subject matter, it lacks specificity about what was implemented or changed. Consider using a more descriptive title that highlights the key change, such as 'Implement comprehensive multi-threaded faulthandler with cross-platform signal handling' or 'Add faulthandler module with watchdog-based traceback scheduling'.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.