Skip to content

Commit

Permalink
Fix custom panic handler (pantsbuild#4884)
Browse files Browse the repository at this point in the history
The custom panic handler was previously assuming that the payload of a
panic was of type &str, and calling .unwrap() on the result of
downcast_ref::<&str>. However, depending on how panic! was invoked, the
type of the payload may be &str, String, or in principle some other
type. Since there are cases in the pants codebase where panic! is
invoked with format!-style macros, resuling in a String rather than &str
payload, these panics induced an additional panic in the .unwrap(),
which displayed "thread panicked while processing panic" and aborted,
rather than showing the proper error message. This is fixed by removing
the .unwrap() and explicitly handling all possible cases.
  • Loading branch information
gshuflin committed Aug 28, 2019
1 parent df6a2fc commit 0bd2fcd
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/rust/engine/engine_cffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,15 +649,18 @@ pub extern "C" fn rule_subgraph_visualize(
})
}

// TODO(#4884): This produces "thread panicked while processing panic. aborting." on my linux
// laptop, requiring me to set RUST_BACKTRACE=1 (or "full") to get the panic message.
#[no_mangle]
pub extern "C" fn set_panic_handler() {
panic::set_hook(Box::new(|panic_info| {
let mut panic_str = format!(
"panic at '{}'",
panic_info.payload().downcast_ref::<&str>().unwrap()
);
let payload = panic_info.payload();

let mut panic_str = match payload.downcast_ref::<&str>() {
Some(s) => format!("panic at '{}'", s),
None => match payload.downcast_ref::<String>() {
Some(s) => format!("panic at '{}'", s),
None => "Non-string panic payload at unknown location".to_string(),
},
};

if let Some(location) = panic_info.location() {
let panic_location_str = format!(", {}:{}", location.file(), location.line());
Expand Down

0 comments on commit 0bd2fcd

Please sign in to comment.