Skip to content

fix(ffi): install one chaining panic hook instead of per-call swaps - #966

Open
snehabandla wants to merge 5 commits into
microsoft:mainfrom
snehabandla:fix/ffi-chaining-panic-hook
Open

snehabandla wants to merge 5 commits into
microsoft:mainfrom
snehabandla:fix/ffi-chaining-panic-hook

Conversation

@snehabandla

Copy link
Copy Markdown
Collaborator

Summary

Fix a race in the FFI panic guard: every guarded call swapped the process-global panic hook on entry and restored it on exit, so concurrent host calls could lose panic details or leave a stale hook installed in place of the host's hook. The guard now installs one chaining hook, once.

Details

  • with_unwind_guard no longer calls take_hook/set_hook per call. The first guarded call installs a single hook that chains to the hook installed before it.
  • A thread-local depth counter marks threads that are inside a guarded call (guards may nest). Panics on those threads are recorded for the returned Panic message, in the same panic hook observed: … + backtrace format as before, and are not forwarded to the previous hook, matching the previous behaviour. The recorded details are cleared when a guarded call starts, so a call never reports details from an earlier one.
  • Panics on threads outside a guarded call are forwarded to the previous hook, so the host's hook keeps working.
  • Installation is skipped while the calling thread is unwinding, since changing the hook would itself panic. The hook uses try_with/try_borrow_mut, so thread-local access inside it cannot panic.
  • The catch logic moved into a small catch_panic helper so it can be tested without touching the poison flag.

Compatibility

No C ABI, status code, or poisoning changes. Panic messages for guarded calls have the same format. Limitations, documented in the module docs:

  • If the host installs its own panic hook after the first FFI call, that hook replaces the chaining hook. Guarded panics are still caught and poison regorus, but the message then contains only the panic payload, and the host hook also sees those panics. Hosts should install their hook before the first FFI call.
  • Replacing the hook takes two calls (take_hook, then set_hook), and stable Rust has no atomic alternative, so a hook the host sets concurrently with the first guarded call can be lost.

Testing

  • New test panic_guard::tests::concurrent_guarded_panics_keep_their_messages_and_the_host_hook:
    • 8 threads × 40 concurrent guarded calls, half of them panicking; each panic message must contain its own payload and the hook's details, and no guarded panic may reach the host hook.
    • An unguarded panic on another thread while a guarded call is in progress, and one on a thread after nested guarded calls, must both reach the host hook.
    • A guarded call made after an earlier call left recorded details must not report them.
    • It fails on every run against the previous swapping implementation and passed 50/50 repeated runs with the fix.
  • cargo test in bindings/ffi: 79 passed.
  • cargo fmt --check (root and bindings/ffi) and cargo clippy --all-targets -- -D warnings in bindings/ffi: passed.
  • cargo xtask pre-push: passed, including the OPA conformance suite (2869 passed, 0 failed, 6 skipped).

@anakrish

Copy link
Copy Markdown
Collaborator

snehabandla Rebase to latest main to fix the dependency audit errors.

with_unwind_guard replaced the process-global panic hook on entry and
restored the saved hook on exit. Concurrent FFI calls race on that
global: one call can restore its saved hook while another call is
still running, so a guarded panic loses its location and backtrace
details, and the last call to exit can leave a stale capture hook
installed in place of the host's hook, which then never sees later
panics.

The first guarded call now installs a single hook that chains to the
hook installed before it. A thread-local depth marks threads inside a
guarded call (guards may nest); panics on those threads are recorded
for the returned Panic message, in the same format as before, and are
not forwarded, matching the previous behaviour. All other panics are
forwarded to the previous hook. Installation is skipped while the
thread is unwinding, and the hook uses try_with/try_borrow_mut so it
cannot itself panic on thread-local access. Poisoning, statuses and
the C ABI are unchanged.

Add a test that runs concurrent panicking and non-panicking guarded
calls across threads and checks that each panic keeps its own details.
It also checks that unguarded panics reach the host's hook, both while
another thread is inside a guarded call and on a thread that has made
nested guarded calls, and that a guarded call does not report details
left over from an earlier one. The test failed on every run with the
swapping implementation.

Co-authored-by: Copilot <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Nested guarded calls during unwinding can erase outer panic diagnostics, and the test’s process-wide hook replacement can disrupt diagnostics and guarded behavior in concurrent tests.

2 open issues
What changed in this PR

This PR replaces per-call panic-hook swapping in the FFI layer with one chained hook, addressing races between concurrent host calls without changing the C ABI, status codes, or poisoning behavior.

Changes:

  • Install the hook once and use thread-local depth tracking to identify guarded panics.
  • Extract panic-catching logic and add concurrent, nested-call, host-hook, and stale-detail checks.
  • Document hook behavior and installation limitations, and add an FFI changelog entry.
File Description
bindings/​ffi/​src/​panic_guard.rs Implements the chained hook, scoped guard tracking, and regression tests.
bindings/​ffi/​CHANGELOG.md Records the concurrent panic-hook race fix.

Review effort: Max

Comment thread bindings/ffi/src/panic_guard.rs Resolved
Comment thread bindings/ffi/src/panic_guard.rs Resolved
snehabandla and others added 3 commits November 20, 2026 04:52
…ng unwinding

A guarded call entered by a destructor while an outer guarded panic unwinds
cleared the outer panic's recorded location and backtrace. Nested scopes now
save the enclosing details and restore them on exit.

Co-authored-by: Copilot <[email protected]>
The test swaps the process-global panic hook, which disturbed other tests
running concurrently in the same binary. It now re-executes the test binary
to run only itself.

Co-authored-by: Copilot <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

When the host and FFI library share a Rust panic runtime, the installed hook can outlive the library and call unloaded code on a later host panic, causing a crash.

2 open issues
1 previously missed issue

In code that hasn't changed since last review

Medium severity Qualify hook guarantees for shared panic runtimes

bindings/​ffi/​src/​panic_guard.rs:15

The host-hook guarantee applies only when the host and Regorus share the same Rust panic runtime; a normally linked cdylib has a separate copy of std. Qualify these statements so consumers do not expect a host hook to control guarded panics in a separately linked FFI library.

2 resolved since last review

Review effort: Max

Comment thread bindings/ffi/src/panic_guard.rs Resolved
Comment thread bindings/ffi/src/panic_guard.rs Resolved
… hook test under Miri

Explain that the chaining panic hook belongs to the copy of std this library
is linked with. A default cdylib build has its own copy, so host panics never
reach the hook. Other Rust code can share that copy, for example Rust
libraries that also link std dynamically (-C prefer-dynamic) in any host, or a
Rust program that links the staticlib. Its panics then reach the hook, the
hook ordering notes apply to the hooks it installs, and because the hook is
never removed, the library must not be unloaded while that std stays loaded.
Add the unload rule to the FFI CHANGELOG entry.

Mark the child-process hook test #[cfg_attr(miri, ignore)], like the other
subprocess tests, because Miri cannot spawn processes.

Co-authored-by: Copilot <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The re-entry test omits the uninstalled-hook path during unwinding, leaving a potential regression that could abort a first FFI call from a host destructor untested.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Test first guarded call during unwinding before hook installation

bindings/​ffi/​src/​panic_guard.rs:376

The re-entry test installs the hook before unwinding, so it never covers ensure_installed with INSTALLED == false during an unwind. A regression there can abort a first FFI call from a host destructor. Exercise that case here before the workers start; their existing checks can then verify that a later ordinary call installs the hook.

2 resolved since last review

Review effort: Max

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