From 6afe3f8e6c1c1cbfe875749a975af58a6e0597b8 Mon Sep 17 00:00:00 2001 From: Mariusz Klochowicz Date: Wed, 17 Nov 2021 15:27:26 +1030 Subject: [PATCH] Disallow calls to tokio::spawn outside dedicated extension trait Prevent accidentally spawning a task that could outlive the actor system and become a zombie. Any long-lived task should be tied to its owner by keeping RemoteHandle around. --- daemon/clippy.toml | 3 +++ daemon/src/lib.rs | 1 - daemon/src/tokio_ext.rs | 4 ++++ 3 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 daemon/clippy.toml diff --git a/daemon/clippy.toml b/daemon/clippy.toml new file mode 100644 index 0000000..1e152c1 --- /dev/null +++ b/daemon/clippy.toml @@ -0,0 +1,3 @@ +disallowed-methods = [ + "tokio::spawn", # tasks can outlive the actor system, prefer spawn_with_handle() +] diff --git a/daemon/src/lib.rs b/daemon/src/lib.rs index 7530aec..3f31aa0 100644 --- a/daemon/src/lib.rs +++ b/daemon/src/lib.rs @@ -1,6 +1,5 @@ #![cfg_attr(not(test), warn(clippy::unwrap_used))] #![warn(clippy::disallowed_method)] - use crate::db::load_all_cfds; use crate::maker_cfd::{FromTaker, NewTakerOnline}; use crate::model::cfd::{Cfd, Order, UpdateCfdProposals}; diff --git a/daemon/src/tokio_ext.rs b/daemon/src/tokio_ext.rs index f5783b2..c004af6 100644 --- a/daemon/src/tokio_ext.rs +++ b/daemon/src/tokio_ext.rs @@ -10,6 +10,8 @@ where F: Future> + Send + 'static, E: fmt::Display, { + // we want to disallow calls to tokio::spawn outside FutureExt + #[allow(clippy::disallowed_method)] tokio::spawn(async move { if let Err(e) = future.await { tracing::warn!("Task failed: {:#}", e); @@ -40,6 +42,8 @@ where Self: Future + Send + 'static, { let (future, handle) = self.remote_handle(); + // we want to disallow calls to tokio::spawn outside FutureExt + #[allow(clippy::disallowed_method)] tokio::spawn(future); handle }