diff --git a/substrate/bin/node-template/node/src/service.rs b/substrate/bin/node-template/node/src/service.rs index e8578ab5b5..e330c17b24 100644 --- a/substrate/bin/node-template/node/src/service.rs +++ b/substrate/bin/node-template/node/src/service.rs @@ -142,7 +142,7 @@ pub fn new_full(config: Configuration) -> Result Result> + Send + Unpin + S /// The task name is a `&'static str` as opposed to a `String`. The reason for that is that /// in order to avoid memory consumption issues with the Prometheus metrics, the set of /// possible task names has to be bounded. + #[deprecated(note = "Use `spawn_task_handle().spawn() instead.")] fn spawn_task(&self, name: &'static str, task: impl Future + Send + 'static); /// Spawns a task in the background that runs the future passed as /// parameter. The given task is considered essential, i.e. if it errors we /// trigger a service exit. + #[deprecated(note = "Use `spawn_essential_task_handle().spawn() instead.")] fn spawn_essential_task(&self, name: &'static str, task: impl Future + Send + 'static); + /// Returns a handle for spawning essential tasks. Any task spawned through this handle is + /// considered essential, i.e. if it errors we trigger a service exit. + fn spawn_essential_task_handle(&self) -> SpawnEssentialTaskHandle; + /// Returns a handle for spawning tasks. fn spawn_task_handle(&self) -> SpawnTaskHandle; @@ -269,13 +275,20 @@ where let _ = essential_failed.send(()); }); - let _ = self.spawn_task(name, essential_task); + let _ = self.spawn_task_handle().spawn(name, essential_task); } fn spawn_task_handle(&self) -> SpawnTaskHandle { self.task_manager.spawn_handle() } + fn spawn_essential_task_handle(&self) -> SpawnEssentialTaskHandle { + SpawnEssentialTaskHandle::new( + self.essential_failed_tx.clone(), + self.task_manager.spawn_handle(), + ) + } + fn rpc_query(&self, mem: &RpcSession, request: &str) -> Pin> + Send>> { Box::pin( self.rpc_handlers.handle_request(request, mem.metadata.clone()) diff --git a/substrate/client/service/src/task_manager.rs b/substrate/client/service/src/task_manager.rs index 9cd92538e3..5a400f70df 100644 --- a/substrate/client/service/src/task_manager.rs +++ b/substrate/client/service/src/task_manager.rs @@ -28,6 +28,7 @@ use prometheus_endpoint::{ CounterVec, HistogramOpts, HistogramVec, Opts, Registry, U64 }; use sc_client_api::CloneableSpawn; +use sp_utils::mpsc::TracingUnboundedSender; use crate::config::TaskType; mod prometheus_future; @@ -149,6 +150,64 @@ impl futures01::future::Executor for SpawnTaskHandle { } } +/// A wrapper over `SpawnTaskHandle` that will notify a receiver whenever any +/// task spawned through it fails. The service should be on the receiver side +/// and will shut itself down whenever it receives any message, i.e. an +/// essential task has failed. +pub struct SpawnEssentialTaskHandle { + essential_failed_tx: TracingUnboundedSender<()>, + inner: SpawnTaskHandle, +} + +impl SpawnEssentialTaskHandle { + /// Creates a new `SpawnEssentialTaskHandle`. + pub fn new( + essential_failed_tx: TracingUnboundedSender<()>, + spawn_task_handle: SpawnTaskHandle, + ) -> SpawnEssentialTaskHandle { + SpawnEssentialTaskHandle { + essential_failed_tx, + inner: spawn_task_handle, + } + } + + /// Spawns the given task with the given name. + /// + /// See also [`SpawnTaskHandle::spawn`]. + pub fn spawn(&self, name: &'static str, task: impl Future + Send + 'static) { + self.spawn_inner(name, task, TaskType::Async) + } + + /// Spawns the blocking task with the given name. + /// + /// See also [`SpawnTaskHandle::spawn_blocking`]. + pub fn spawn_blocking( + &self, + name: &'static str, + task: impl Future + Send + 'static, + ) { + self.spawn_inner(name, task, TaskType::Blocking) + } + + fn spawn_inner( + &self, + name: &'static str, + task: impl Future + Send + 'static, + task_type: TaskType, + ) { + use futures::sink::SinkExt; + let mut essential_failed = self.essential_failed_tx.clone(); + let essential_task = std::panic::AssertUnwindSafe(task) + .catch_unwind() + .map(move |_| { + log::error!("Essential task `{}` failed. Shutting down service.", name); + let _ = essential_failed.send(()); + }); + + let _ = self.inner.spawn_inner(name, essential_task, task_type); + } +} + /// Helper struct to manage background/async tasks in Service. pub struct TaskManager { /// A future that resolves when the service has exited, this is useful to