Skip printing the backtrace for RecvError/SendError

We currently get tons of useless backtraces clogging up the output when we have a panic cascade. This adds a handler that outputs a single line when a thread panics due to a sender or receiver hanging up, since this is almost always due to a panic cascade.
This commit is contained in:
Manish Goregaokar 2016-04-01 07:34:59 +05:30
parent 7518c4de93
commit ed3f7f5818
2 changed files with 28 additions and 2 deletions

View file

@ -5,16 +5,41 @@
use ipc_channel::ipc::IpcSender;
use serde::Serialize;
use std::borrow::ToOwned;
use std::io::{Write, stderr};
use std::panic::{PanicInfo, take_handler, set_handler};
use std::sync::mpsc::Sender;
use std::thread;
use std::thread::Builder;
use thread_state;
#[allow(unused_must_use)]
pub fn spawn_named<F>(name: String, f: F)
where F: FnOnce() + Send + 'static
{
let builder = thread::Builder::new().name(name);
builder.spawn(f).unwrap();
let f_with_handler = move || {
let hook = take_handler();
let new_handler = move |info: &PanicInfo| {
let payload = info.payload();
if let Some(s) = payload.downcast_ref::<String>() {
if s.contains("SendError") {
write!(stderr(), "Thread \"{}\" panicked with an unwrap of `SendError` (backtrace skipped)\n",
thread::current().name().unwrap_or("<unknown thread>"));
return;
} else if s.contains("RecvError") {
write!(stderr(), "Thread \"{}\" panicked with an unwrap of `RecvError` (backtrace skipped)\n",
thread::current().name().unwrap_or("<unknown thread>"));
return;
}
}
hook(&info);
};
set_handler(new_handler);
f();
};
builder.spawn(f_with_handler).unwrap();
}
/// An abstraction over `Sender<T>` and `IpcSender<T>`, for use in