diff options
author | bors-servo <lbergstrom+bors@mozilla.com> | 2016-04-14 19:55:17 +0530 |
---|---|---|
committer | bors-servo <lbergstrom+bors@mozilla.com> | 2016-04-14 19:55:17 +0530 |
commit | 2b910678db8b461dc50919832044bd95cdecb53e (patch) | |
tree | 54a935ee9f1b6dbdcf3f85904c7b4198a6212335 /components/util/thread.rs | |
parent | 3368565b3c6c76e5ce2d170d5c5cfb2f52a56956 (diff) | |
parent | 8c0fa01884bfb4dcf7d591aa06f48f78f7970766 (diff) | |
download | servo-2b910678db8b461dc50919832044bd95cdecb53e.tar.gz servo-2b910678db8b461dc50919832044bd95cdecb53e.zip |
Auto merge of #10587 - asajeffrey:add-failure-panic-message, r=Manishearth
Added panic message to failures.
Added the panic message to failures. This is a step towards #10334, since it gives us access to the panic error message when we fire a `mozbrowsererror` event. The remaining steps are also to record the backtrace, and to report the failure in the event.
<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/10587)
<!-- Reviewable:end -->
Diffstat (limited to 'components/util/thread.rs')
-rw-r--r-- | components/util/thread.rs | 35 |
1 files changed, 27 insertions, 8 deletions
diff --git a/components/util/thread.rs b/components/util/thread.rs index 9451d0a434f..b8fba1aa773 100644 --- a/components/util/thread.rs +++ b/components/util/thread.rs @@ -5,6 +5,7 @@ use ipc_channel::ipc::IpcSender; use opts; use serde::Serialize; +use std::any::Any; use std::borrow::ToOwned; use std::io::{Write, stderr}; use std::panic::{PanicInfo, take_hook, set_hook}; @@ -13,6 +14,8 @@ use std::thread; use std::thread::Builder; use thread_state; +pub type PanicReason = Option<String>; + pub fn spawn_named<F>(name: String, f: F) where F: FnOnce() + Send + 'static { @@ -52,6 +55,17 @@ pub fn spawn_named<F>(name: String, f: F) builder.spawn(f_with_hook).unwrap(); } +pub trait AddFailureDetails { + fn add_panic_message(&mut self, message: String); + fn add_panic_object(&mut self, object: Box<Any>) { + if let Some(message) = object.downcast_ref::<String>() { + self.add_panic_message(message.to_owned()); + } else if let Some(&message) = object.downcast_ref::<&'static str>() { + self.add_panic_message(message.to_owned()); + } + } +} + /// An abstraction over `Sender<T>` and `IpcSender<T>`, for use in /// `spawn_named_with_send_on_failure`. pub trait SendOnFailure { @@ -62,14 +76,16 @@ pub trait SendOnFailure { impl<T> SendOnFailure for Sender<T> where T: Send + 'static { type Value = T; fn send_on_failure(&mut self, value: T) { - self.send(value).unwrap(); + // Discard any errors to avoid double-panic + let _ = self.send(value); } } impl<T> SendOnFailure for IpcSender<T> where T: Send + Serialize + 'static { type Value = T; fn send_on_failure(&mut self, value: T) { - self.send(value).unwrap(); + // Discard any errors to avoid double-panic + let _ = self.send(value); } } @@ -77,11 +93,13 @@ impl<T> SendOnFailure for IpcSender<T> where T: Send + Serialize + 'static { pub fn spawn_named_with_send_on_failure<F, T, S>(name: String, state: thread_state::ThreadState, f: F, - msg: T, + mut msg: T, mut dest: S) - where F: FnOnce() + Send + 'static, - T: Send + 'static, - S: Send + SendOnFailure<Value=T> + 'static { + where F: FnOnce() + Send + 'static, + T: Send + AddFailureDetails + 'static, + S: Send + SendOnFailure + 'static, + S::Value: From<T>, +{ let future_handle = thread::Builder::new().name(name.to_owned()).spawn(move || { thread_state::initialize(state); f() @@ -91,9 +109,10 @@ pub fn spawn_named_with_send_on_failure<F, T, S>(name: String, Builder::new().name(watcher_name).spawn(move || { match future_handle.join() { Ok(()) => (), - Err(..) => { + Err(err) => { debug!("{} failed, notifying constellation", name); - dest.send_on_failure(msg); + msg.add_panic_object(err); + dest.send_on_failure(S::Value::from(msg)); } } }).unwrap(); |