2016-02-09 16:47:21 +01:00
|
|
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
//! Panic utilities
|
|
|
|
|
|
|
|
use std::thread;
|
|
|
|
use std::ops::DerefMut;
|
2016-02-10 12:50:27 +01:00
|
|
|
use std::any::Any;
|
|
|
|
use std::sync::{Arc, Mutex};
|
2016-02-09 16:47:21 +01:00
|
|
|
|
2016-02-10 15:28:43 +01:00
|
|
|
/// Thread-safe closure for handling possible panics
|
|
|
|
pub trait OnPanicListener: Send + Sync + 'static {
|
|
|
|
/// Invoke listener
|
|
|
|
fn call(&mut self, arg: &str);
|
2016-02-09 16:47:21 +01:00
|
|
|
}
|
|
|
|
|
2016-02-10 16:35:52 +01:00
|
|
|
/// Forwards panics from child
|
|
|
|
pub trait ForwardPanic {
|
|
|
|
/// Attach `on_panic` listener to `child` and rethrow all panics
|
|
|
|
fn forward_from<S>(&self, child: &S) where S : MayPanic;
|
|
|
|
}
|
|
|
|
|
2016-02-10 15:28:43 +01:00
|
|
|
/// Trait indicating that the structure catches some of the panics (most probably from spawned threads)
|
|
|
|
/// and it's possbile to be notified when one of the threads panics.
|
|
|
|
pub trait MayPanic {
|
|
|
|
/// `closure` will be invoked whenever panic in thread is caught
|
2016-02-10 16:35:52 +01:00
|
|
|
fn on_panic<F>(&self, closure: F) where F: OnPanicListener;
|
2016-02-09 16:47:21 +01:00
|
|
|
}
|
|
|
|
|
2016-02-18 23:29:41 +01:00
|
|
|
struct PanicGuard<'a> {
|
|
|
|
handler: &'a PanicHandler,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Drop for PanicGuard<'a> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if thread::panicking() {
|
|
|
|
self.handler.notify_all("Panic!".to_owned());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-10 15:28:43 +01:00
|
|
|
/// Structure that allows to catch panics and notify listeners
|
|
|
|
pub struct PanicHandler {
|
|
|
|
listeners: Mutex<Vec<Box<OnPanicListener>>>
|
2016-02-10 12:50:27 +01:00
|
|
|
}
|
2016-02-09 16:47:21 +01:00
|
|
|
|
2016-02-10 15:28:43 +01:00
|
|
|
impl PanicHandler {
|
|
|
|
/// Creates new `PanicHandler` wrapped in `Arc`
|
2016-02-10 16:35:52 +01:00
|
|
|
pub fn new_in_arc() -> Arc<PanicHandler> {
|
2016-02-10 15:28:43 +01:00
|
|
|
Arc::new(Self::new())
|
2016-02-09 16:47:21 +01:00
|
|
|
}
|
|
|
|
|
2016-02-10 15:28:43 +01:00
|
|
|
/// Creates new `PanicHandler`
|
|
|
|
pub fn new() -> PanicHandler {
|
|
|
|
PanicHandler {
|
2016-02-09 16:47:21 +01:00
|
|
|
listeners: Mutex::new(vec![])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-10 15:28:43 +01:00
|
|
|
/// Invoke closure and catch any possible panics.
|
|
|
|
/// In case of panic notifies all listeners about it.
|
2016-02-10 12:50:27 +01:00
|
|
|
#[allow(deprecated)]
|
|
|
|
// TODO [todr] catch_panic is deprecated but panic::recover has different bounds (not allowing mutex)
|
2016-02-10 15:28:43 +01:00
|
|
|
pub fn catch_panic<G, R>(&self, g: G) -> thread::Result<R> where G: FnOnce() -> R + Send + 'static {
|
2016-02-18 23:29:41 +01:00
|
|
|
let guard = PanicGuard { handler: self };
|
|
|
|
let result = g();
|
|
|
|
Ok(result)
|
2016-02-10 12:50:27 +01:00
|
|
|
}
|
|
|
|
|
2016-02-10 16:35:52 +01:00
|
|
|
fn notify_all(&self, r: String) {
|
2016-02-10 12:50:27 +01:00
|
|
|
let mut listeners = self.listeners.lock().unwrap();
|
|
|
|
for listener in listeners.deref_mut() {
|
2016-02-10 15:28:43 +01:00
|
|
|
listener.call(&r);
|
2016-02-09 16:47:21 +01:00
|
|
|
}
|
2016-02-10 12:50:27 +01:00
|
|
|
}
|
2016-02-09 16:47:21 +01:00
|
|
|
}
|
|
|
|
|
2016-02-10 15:28:43 +01:00
|
|
|
impl MayPanic for PanicHandler {
|
2016-02-10 16:35:52 +01:00
|
|
|
fn on_panic<F>(&self, closure: F) where F: OnPanicListener {
|
2016-02-09 16:47:21 +01:00
|
|
|
self.listeners.lock().unwrap().push(Box::new(closure));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-10 16:35:52 +01:00
|
|
|
impl ForwardPanic for Arc<PanicHandler> {
|
|
|
|
fn forward_from<S>(&self, child: &S) where S : MayPanic {
|
|
|
|
let p = self.clone();
|
|
|
|
child.on_panic(move |t| p.notify_all(t));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-10 15:28:43 +01:00
|
|
|
impl<F> OnPanicListener for F
|
|
|
|
where F: FnMut(String) + Send + Sync + 'static {
|
|
|
|
fn call(&mut self, arg: &str) {
|
|
|
|
self(arg.to_owned())
|
2016-02-10 12:50:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-10 15:28:43 +01:00
|
|
|
fn convert_to_string(t: &Box<Any + Send>) -> Option<String> {
|
2016-02-15 00:51:50 +01:00
|
|
|
let as_str = t.downcast_ref::<&'static str>().cloned().map(|t| t.to_owned());
|
2016-02-10 15:28:43 +01:00
|
|
|
let as_string = t.downcast_ref::<String>().cloned();
|
2016-02-10 12:50:27 +01:00
|
|
|
|
2016-02-10 15:28:43 +01:00
|
|
|
as_str.or(as_string)
|
2016-02-10 12:50:27 +01:00
|
|
|
}
|
|
|
|
|
2016-02-09 16:47:21 +01:00
|
|
|
#[test]
|
2016-02-19 11:05:39 +01:00
|
|
|
#[ignore] // panic forwarding doesnt work on the same thread in beta
|
2016-02-09 16:47:21 +01:00
|
|
|
fn should_notify_listeners_about_panic () {
|
2016-02-10 12:50:27 +01:00
|
|
|
use std::sync::RwLock;
|
2016-02-09 16:47:21 +01:00
|
|
|
// given
|
|
|
|
let invocations = Arc::new(RwLock::new(vec![]));
|
|
|
|
let i = invocations.clone();
|
2016-02-10 15:28:43 +01:00
|
|
|
let p = PanicHandler::new();
|
|
|
|
p.on_panic(move |t| i.write().unwrap().push(t));
|
2016-02-09 16:47:21 +01:00
|
|
|
|
|
|
|
// when
|
2016-02-10 12:50:27 +01:00
|
|
|
p.catch_panic(|| panic!("Panic!")).unwrap_err();
|
2016-02-09 16:47:21 +01:00
|
|
|
|
|
|
|
// then
|
|
|
|
assert!(invocations.read().unwrap()[0] == "Panic!");
|
|
|
|
}
|
|
|
|
|
2016-02-10 12:50:27 +01:00
|
|
|
#[test]
|
2016-02-19 11:05:39 +01:00
|
|
|
#[ignore] // panic forwarding doesnt work on the same thread in beta
|
2016-02-10 12:50:27 +01:00
|
|
|
fn should_notify_listeners_about_panic_when_string_is_dynamic () {
|
|
|
|
use std::sync::RwLock;
|
|
|
|
// given
|
|
|
|
let invocations = Arc::new(RwLock::new(vec![]));
|
|
|
|
let i = invocations.clone();
|
2016-02-10 15:28:43 +01:00
|
|
|
let p = PanicHandler::new();
|
|
|
|
p.on_panic(move |t| i.write().unwrap().push(t));
|
2016-02-10 12:50:27 +01:00
|
|
|
|
|
|
|
// when
|
|
|
|
p.catch_panic(|| panic!("Panic: {}", 1)).unwrap_err();
|
|
|
|
|
|
|
|
// then
|
|
|
|
assert!(invocations.read().unwrap()[0] == "Panic: 1");
|
|
|
|
}
|
|
|
|
|
2016-02-09 16:47:21 +01:00
|
|
|
#[test]
|
|
|
|
fn should_notify_listeners_about_panic_in_other_thread () {
|
|
|
|
use std::thread;
|
2016-02-10 12:50:27 +01:00
|
|
|
use std::sync::RwLock;
|
2016-02-09 16:47:21 +01:00
|
|
|
|
|
|
|
// given
|
|
|
|
let invocations = Arc::new(RwLock::new(vec![]));
|
|
|
|
let i = invocations.clone();
|
2016-02-10 15:28:43 +01:00
|
|
|
let p = PanicHandler::new();
|
|
|
|
p.on_panic(move |t| i.write().unwrap().push(t));
|
2016-02-09 16:47:21 +01:00
|
|
|
|
|
|
|
// when
|
|
|
|
let t = thread::spawn(move ||
|
2016-02-10 12:50:27 +01:00
|
|
|
p.catch_panic(|| panic!("Panic!")).unwrap()
|
2016-02-09 16:47:21 +01:00
|
|
|
);
|
2016-02-10 12:50:27 +01:00
|
|
|
t.join().unwrap_err();
|
|
|
|
|
|
|
|
// then
|
|
|
|
assert!(invocations.read().unwrap()[0] == "Panic!");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-02-19 11:05:39 +01:00
|
|
|
#[ignore] // panic forwarding doesnt work on the same thread in beta
|
2016-02-10 12:50:27 +01:00
|
|
|
fn should_forward_panics () {
|
|
|
|
use std::sync::RwLock;
|
|
|
|
// given
|
|
|
|
let invocations = Arc::new(RwLock::new(vec![]));
|
|
|
|
let i = invocations.clone();
|
2016-02-10 16:35:52 +01:00
|
|
|
let p = PanicHandler::new_in_arc();
|
2016-02-10 15:28:43 +01:00
|
|
|
p.on_panic(move |t| i.write().unwrap().push(t));
|
2016-02-10 12:50:27 +01:00
|
|
|
|
2016-02-10 15:28:43 +01:00
|
|
|
let p2 = PanicHandler::new();
|
2016-02-10 16:35:52 +01:00
|
|
|
p.forward_from(&p2);
|
2016-02-10 12:50:27 +01:00
|
|
|
|
|
|
|
// when
|
|
|
|
p2.catch_panic(|| panic!("Panic!")).unwrap_err();
|
2016-02-09 16:47:21 +01:00
|
|
|
|
|
|
|
// then
|
|
|
|
assert!(invocations.read().unwrap()[0] == "Panic!");
|
|
|
|
}
|