Skip to content

Commit

Permalink
Remove annoying error message when task is cancelled
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatson committed Jun 21, 2024
1 parent ace4bed commit 3635915
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions crates/librqbit_core/src/spawn_utils.rs
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -23,7 +32,12 @@ pub fn spawn(
trace!("finished");
}
Err(e) => {
error!("finished with error: {:#}", e)
if e.is::<CancelledError>() {
debug!("task cancelled")
} else {
error!("finished with error: {:#}", e)
}

}
}
return;
Expand All @@ -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
}
Expand Down

0 comments on commit 3635915

Please sign in to comment.