-
Notifications
You must be signed in to change notification settings - Fork 69
starknet_transaction_prover: global panic hook + graceful SIGTERM shutdown #14166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
avi-starkware
wants to merge
1
commit into
avi/prover-v3/request-logs
Choose a base branch
from
avi/prover-v3/panic-shutdown
base: avi/prover-v3/request-logs
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| //! Process-wide panic hook for the prover. | ||
| //! | ||
| //! Without an explicit hook, panics in `tokio::spawn`ed work hit the runtime's | ||
| //! default handler and print to stderr in an ad-hoc format. We want one | ||
| //! structured `tracing` event with location + backtrace so log aggregators | ||
| //! can index it. The hook only emits a log line — runtime abort-on-panic | ||
| //! behavior is preserved. | ||
|
|
||
| use std::backtrace::Backtrace; | ||
| use std::panic::PanicHookInfo; | ||
|
|
||
| use tracing::error; | ||
|
|
||
| #[cfg(test)] | ||
| #[path = "panic_test.rs"] | ||
| mod panic_test; | ||
|
|
||
| pub fn install_panic_hook() { | ||
| std::panic::set_hook(Box::new(panic_hook)); | ||
| } | ||
|
|
||
| fn panic_hook(info: &PanicHookInfo<'_>) { | ||
| let message = extract_payload(info); | ||
| let location = info | ||
| .location() | ||
| .map(|loc| format!("{}:{}:{}", loc.file(), loc.line(), loc.column())) | ||
| .unwrap_or_else(|| "<unknown>".to_string()); | ||
| let backtrace = Backtrace::force_capture(); | ||
| error!( | ||
| event = "panic", | ||
| location = %location, | ||
| message = %message, | ||
| backtrace = %backtrace, | ||
| "Service panicked", | ||
| ); | ||
| } | ||
|
|
||
| /// Best-effort extraction of the panic payload — supports the common | ||
| /// `panic!("string literal")` and `panic!("{fmt}", ...)` cases. Returns | ||
| /// `"<non-string panic payload>"` for arbitrary types. | ||
| /// | ||
| /// Replace with `PanicHookInfo::payload_as_str()` once the pinned toolchain | ||
| /// (nightly-2025-07-14) ships it as stable (gated behind `panic_payload_as_str`). | ||
| pub(crate) fn extract_payload(info: &PanicHookInfo<'_>) -> String { | ||
| let payload = info.payload(); | ||
| if let Some(s) = payload.downcast_ref::<&'static str>() { | ||
| return (*s).to_string(); | ||
| } | ||
| if let Some(s) = payload.downcast_ref::<String>() { | ||
| return s.clone(); | ||
| } | ||
| "<non-string panic payload>".to_string() | ||
| } | ||
24 changes: 24 additions & 0 deletions
24
crates/starknet_transaction_prover/src/server/panic_test.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| use std::sync::{Arc, Mutex}; | ||
|
|
||
| use crate::server::panic::extract_payload; | ||
|
|
||
| fn capture_payload<F: FnOnce() + std::panic::UnwindSafe>(f: F) -> String { | ||
| let captured: Arc<Mutex<Option<String>>> = Arc::new(Mutex::new(None)); | ||
| let prev_hook = std::panic::take_hook(); | ||
| let writer = Arc::clone(&captured); | ||
| std::panic::set_hook(Box::new(move |info| { | ||
| *writer.lock().unwrap() = Some(extract_payload(info)); | ||
| })); | ||
| let _ = std::panic::catch_unwind(f); | ||
| std::panic::set_hook(prev_hook); | ||
| let value = captured.lock().unwrap().clone().unwrap_or_default(); | ||
| value | ||
| } | ||
|
|
||
| // Panic-capturing tests share global state (the panic hook), so they must | ||
| // run serially. Keep as a single `#[test]` so ordering is explicit. | ||
| #[test] | ||
| fn extracts_static_str_and_formatted_payloads() { | ||
| assert_eq!(capture_payload(|| panic!("static literal")), "static literal"); | ||
| assert_eq!(capture_payload(|| panic!("formatted {}", 42)), "formatted 42"); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reimplements already-stable standard library method
Low Severity
The
extract_payloadfunction manually reimplementsPanicHookInfo::payload_as_str(), which was stabilized in Rust 1.91.0. The project'srust-toolchain.tomlspecifies channel1.95, so the standard library method is already available. The doc comment onextract_payloadreferences anightly-2025-07-14toolchain that no longer matches the project's actual pinned toolchain, making the stated reason for the workaround stale.Reviewed by Cursor Bugbot for commit c31ce65. Configure here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, the crate
starknet_transaction_proveruses the referenced nightly toolchain, so the doc comment is not stale