rust.uncancelled_tasks
Stability
Medium
Detects spawned tasks that are never cancelled, leading to resource leaks and runaway processes.
Why It Matters
Section titled “Why It Matters”Uncancelled tasks:
- Leak resources - Tasks run forever
- Consume memory - Accumulating state
- Prevent shutdown - Graceful termination fails
Example
Section titled “Example”// ❌ 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?;What Unfault Detects
Section titled “What Unfault Detects”- Spawned tasks with ignored handles
- Missing cancellation tokens
- Tasks without shutdown paths