Skip to content

Avoid locking registration state in unchained faulthandler signals - #9902

Merged
youknowone merged 1 commit into
RustPython:mainfrom
youknowdot:fix/faulthandler-signal-snapshot-20261117
Nov 17, 2026
Merged

youknowone merged 1 commit into
RustPython:mainfrom
youknowdot:fix/faulthandler-signal-snapshot-20261117

Conversation

@youknowdot

Copy link
Copy Markdown
Contributor

Summary

Fix the unchained user-signal registration deadlock on Unix targets with native 64-bit atomics, without changing the descriptor number or adding allocation/reclamation to signal handlers.

get_user_signal() currently locks the same mutex used by registration. A signal delivered while that thread holds the mutex can wait on itself. Publish the small (fd, enabled, all_threads, chain) configuration in a single atomic word instead; registration bookkeeping and previous dispositions remain separate. Restore entry errno on all normal user-handler exits.

This is a narrowly scoped safety fix, not completion of max_threads/C-stack/all-thread parity. No expected-failure markers are removed, dependencies changed, or CI configuration altered.

Scope and remaining limitations

  • Applies to Unix target_has_atomic="64". Other Unix targets retain the old implementation and its limitation; Windows is unchanged.
  • chain=True still uses the existing mutex-based disposition path and can deadlock. Its restore/raise/reinstall versus unregister race is not solved. CPython v3.15.2 also uses restore/raise/reinstall; that is not evidence of race-free behavior in RustPython.
  • Original descriptor identity is preserved, including dup2 redirection. This does not fix existing Python-file keepalive or concurrent descriptor-close/reuse lifetime issues.
  • First installation has a narrow publication window: a handler may observe disabled state and return before registration finishes. This is not advertised as delivery guarantees during concurrent installation.
  • Post-fork normal registration/writer-lock behavior remains unchanged. No new fork restriction or descriptor duplication is introduced.

The previously explored heap snapshots/duplicated descriptors are not used: duplicated descriptors would change observable redirection semantics, while reclamation introduces unrelated lifetime/fork problems.

Validation

  • Fresh Linux release build passed; all 32 host_env unit tests passed, including the new packed-config and interrupted-bookkeeping checks.
  • Targeted host_env release Clippy with -D warnings passed. Full workspace/C-API tests and Clippy were not run.
  • Canonical test_faulthandler: 51 tests, 11 skips, 4 existing expected failures, passed twice. Registration/file/fd/unregister/thread-label/chaining tests ran successfully.
  • Skips cover Windows-only cases, unavailable _testcapi, disabled walltime resource, unavailable stack-overflow hook and the existing GIL-disabled fatal-all-thread restriction. Existing extension-module/C-stack/native-thread-fatal/max-thread failures remain marked unchanged.
  • Actual RustPython no-chain stress: baseline 3/3 timed out after 8 seconds; candidate 3/3 completed in 0.26–0.34 seconds, registering 100,000 times while another thread sent SIGUSR1. The equivalent CPython 3.15.2 probe completed 3/3. ptrace was unavailable, so exact lock-site attribution is source-supported rather than debugger-proven.
  • Original-fd dup2 redirection and ordinary post-fork unregister/re-register/dump probes match baseline behavior. These do not prove race-free concurrent descriptor closure or fork while a writer holds its mutex.
  • Native 64-bit-atomic target configuration checked for Linux/macOS; non-atomic64 armv5te fallback inspected. Other OS execution/cross-compilation remains for CI. Draft only.

Performance review needed

Seven alternating local release-binary pairs, same Lib and setup:

Case Baseline median Candidate median Change
100,000 registrations 0.1683 s 0.1796 s +6.7%
200,000 delivered signals 0.4523 s 0.4726 s +4.5%

A shorter 20,000-signal measurement also showed +8.3%. These are narrow microbenchmarks, not zero-regression evidence. The available baseline has identical pre-change faulthandler code and build features but includes unrelated sampling-profiler changes; binary layout/noise and the separate errno correction prevent attributing all timing differences solely to the atomic load. Treat this as a safety improvement with performance still requiring review, not merge-ready.

Publish coherent fd and flag snapshots on atomic64 Unix targets. Preserve descriptor identity and existing chaining bookkeeping; restore errno on user-handler exits. This is a limited no-chain deadlock fix, not a solution for chained disposition races or descriptor ownership.

Assisted-by: Codex:model-version-unavailable
@coderabbitai

coderabbitai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Caution

Review failed

The pull request is closed.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@youknowone
youknowone marked this pull request as ready for review November 17, 2026 04:53
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T04:57:46.986921Z d0009ac Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@youknowone
youknowone merged commit 7abccf0 into RustPython:main Nov 17, 2026
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants