threadpool: add panic_handler (#1052)

This commit is contained in:
Ryan Dahl
2019-04-21 16:26:09 -07:00
committed by Carl Lerche
parent 712ca84033
commit fea1f780bc
4 changed files with 55 additions and 0 deletions
+30
View File
@@ -6,6 +6,7 @@ use shutdown::ShutdownTrigger;
use thread_pool::ThreadPool;
use worker::{self, Worker, WorkerId};
use std::any::Any;
use std::cmp::max;
use std::error::Error;
use std::fmt;
@@ -106,6 +107,7 @@ impl Builder {
around_worker: None,
after_start: None,
before_stop: None,
panic_handler: None,
},
new_park,
}
@@ -199,6 +201,34 @@ impl Builder {
self
}
/// Sets a callback to be triggered when a panic during a future bubbles up
/// to Tokio. By default Tokio catches these panics, and they will be
/// ignored. The parameter passed to this callback is the same error value
/// returned from std::panic::catch_unwind(). To abort the process on
/// panics, use std::panic::resume_unwind() in this callback as shown
/// below.
///
/// # Examples
///
/// ```
/// # extern crate tokio_threadpool;
/// # extern crate futures;
/// # use tokio_threadpool::Builder;
///
/// # pub fn main() {
/// let thread_pool = Builder::new()
/// .panic_handler(|err| std::panic::resume_unwind(err))
/// .build();
/// # }
/// ```
pub fn panic_handler<F>(&mut self, f: F) -> &mut Self
where
F: Fn(Box<Any + Send>) + Send + Sync + 'static,
{
self.config.panic_handler = Some(Arc::new(f));
self
}
/// Set name prefix of threads spawned by the scheduler
///
/// Thread name prefix is used for generating thread names. For example, if