use std::{
any::Any,
cell::Cell,
panic::{AssertUnwindSafe, catch_unwind},
sync::Mutex,
};
pub const MESSAGE_LIMIT: usize = 1024;
pub const QUARANTINE_LIMIT: usize = 256;
type Payload = Box<dyn Any + Send>;
static QUARANTINE: Mutex<Vec<Payload>> = Mutex::new(Vec::new());
thread_local! {
static OBSERVER: Cell<Option<fn(&CapturedPanic)>> = const { Cell::new(None) };
}
pub fn set_cleanup_observer(observer: fn(&CapturedPanic)) {
OBSERVER.with(|slot| slot.set(Some(observer)));
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CapturedPanic {
pub message: String,
pub truncated: bool,
pub cleanup_panicked: bool,
}
pub fn capture(payload: Payload) -> CapturedPanic {
if payload.is::<CapturedPanic>() {
return bounded(
*payload
.downcast::<CapturedPanic>()
.expect("captured panic type"),
);
}
let report = capture_without_observer(payload);
if report.cleanup_panicked
&& let Ok(Some(observer)) = OBSERVER.try_with(Cell::get)
{
observer(&report);
}
report
}
pub fn capture_without_observer(payload: Payload) -> CapturedPanic {
if payload.is::<CapturedPanic>() {
return bounded(
*payload
.downcast::<CapturedPanic>()
.expect("captured panic type"),
);
}
let text = payload
.downcast_ref::<String>()
.map(String::as_str)
.or_else(|| payload.downcast_ref::<&'static str>().copied())
.unwrap_or("non-string panic payload");
let mut end = text.len().min(MESSAGE_LIMIT);
while !text.is_char_boundary(end) {
end -= 1;
}
let mut report = CapturedPanic {
message: text[..end].to_owned(),
truncated: end < text.len(),
cleanup_panicked: false,
};
if let Err(secondary) = catch_unwind(AssertUnwindSafe(|| drop(payload))) {
report.cleanup_panicked = true;
if secondary.is::<String>() || secondary.is::<&'static str>() {
drop(secondary);
} else {
quarantine(secondary);
}
}
report
}
pub fn capture_for_join(payload: Payload) -> (CapturedPanic, bool) {
if payload.is::<CapturedPanic>() || payload.is::<String>() || payload.is::<&'static str>() {
return (capture_without_observer(payload), false);
}
quarantine(payload);
(
CapturedPanic {
message: "non-string panic payload".to_owned(),
truncated: false,
cleanup_panicked: false,
},
true,
)
}
fn bounded(mut report: CapturedPanic) -> CapturedPanic {
if report.message.len() > MESSAGE_LIMIT {
let mut end = MESSAGE_LIMIT;
while !report.message.is_char_boundary(end) {
end -= 1;
}
report.message.truncate(end);
report.truncated = true;
}
report.message = report.message.into_boxed_str().into_string();
report
}
fn quarantine(payload: Payload) {
let mut quarantine = QUARANTINE
.lock()
.unwrap_or_else(|poison| poison.into_inner());
if quarantine.len() == QUARANTINE_LIMIT {
std::process::abort();
}
quarantine.push(payload);
}
pub(crate) fn retain(first: &mut Option<CapturedPanic>, payload: Payload) {
let next = capture(payload);
if let Some(first) = first {
first.cleanup_panicked |= next.cleanup_panicked;
} else {
*first = Some(next);
}
}
pub(crate) fn dispose<T>(value: T, failure: &mut Option<CapturedPanic>) {
if let Err(payload) = catch_unwind(AssertUnwindSafe(|| drop(value))) {
retain(failure, payload);
}
}
#[cfg(test)]
#[path = "panic_payload_test.rs"]
mod panic_payload_test;