Skip to content

rust.uncancelled_tasks

Stability Medium

Detects spawned tasks that are never cancelled, leading to resource leaks and runaway processes.

Uncancelled tasks:

  • Leak resources - Tasks run forever
  • Consume memory - Accumulating state
  • Prevent shutdown - Graceful termination fails
// ❌ Before (task never cancelled)
let _handle = tokio::spawn(background_work());
// handle ignored, no way to cancel
// ✅ After (with cancellation)
use tokio_util::sync::CancellationToken;
let token = CancellationToken::new();
let cloned = token.clone();
let handle = tokio::spawn(async move {
loop {
tokio::select! {
_ = cloned.cancelled() => break,
_ = do_work() => {}
}
}
});
// When shutting down:
token.cancel();
handle.await?;
  • Spawned tasks with ignored handles
  • Missing cancellation tokens
  • Tasks without shutdown paths