From 36359150a7f2b65c4eafcc771c5f9f98997ca6c3 Mon Sep 17 00:00:00 2001 From: Igor Katson Date: Fri, 21 Jun 2024 15:12:51 +0100 Subject: [PATCH] Remove annoying error message when task is cancelled --- crates/librqbit_core/src/spawn_utils.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/crates/librqbit_core/src/spawn_utils.rs b/crates/librqbit_core/src/spawn_utils.rs index 7479ca35..c20793bb 100644 --- a/crates/librqbit_core/src/spawn_utils.rs +++ b/crates/librqbit_core/src/spawn_utils.rs @@ -1,6 +1,15 @@ use anyhow::bail; use tokio_util::sync::CancellationToken; -use tracing::{error, trace, Instrument}; +use tracing::{debug, error, trace, Instrument}; + +#[derive(Debug)] +struct CancelledError {} +impl std::error::Error for CancelledError {} +impl std::fmt::Display for CancelledError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str("cancelled") + } +} /// Spawns a future with tracing instrumentation. pub fn spawn( @@ -23,7 +32,12 @@ pub fn spawn( trace!("finished"); } Err(e) => { - error!("finished with error: {:#}", e) + if e.is::() { + debug!("task cancelled") + } else { + error!("finished with error: {:#}", e) + } + } } return; @@ -43,7 +57,7 @@ pub fn spawn_with_cancel( spawn(span, async move { tokio::select! { _ = cancellation_token.cancelled() => { - bail!("cancelled"); + bail!(CancelledError{}) }, r = fut => r }