tokio-task-supervisor

Crates.iotokio-task-supervisor
lib.rstokio-task-supervisor
version0.1.1
created_at2025-10-12 03:15:10.971994+00
updated_at2025-10-12 18:58:42.484165+00
descriptionTokio TaskTracker with built-in cancellation token management and coordinated shutdown.
homepage
repositoryhttps://github.com/juliuslipp/tokio-task-supervisor
max_upload_size
id1878818
size37,721
Julius Lipp (juliuslipp)

documentation

https://docs.rs/tokio-task-supervisor

README

tokio-task-supervisor

A wrapper around tokio_util::task::TaskTracker that adds coordinated shutdown via a shared cancellation token.

What it does

  • Tracks running tasks (same as TaskTracker)
  • Provides a shared CancellationToken for shutdown
  • Spawns tasks that automatically get cancellation tokens
  • Handles graceful shutdown (close tracker + cancel token + wait)

Usage

use tokio_task_supervisor::TaskSupervisor;
use tokio::time::{sleep, Duration};

#[tokio::main]
async fn main() {
    let supervisor = TaskSupervisor::new();

    // Spawn tasks that can be cancelled
    let handle = supervisor.spawn_with_token(|token| async move {
        loop {
            tokio::select! {
                _ = token.cancelled() => break,
                _ = sleep(Duration::from_millis(100)) => {}
            }
        }
    });

    // Shutdown all tasks
    supervisor.shutdown().await;
    handle.await.expect("task finished");
}

API

  • spawn_with_token() - spawn task with cancellation token
  • spawn_with_cancel() - spawn task that races against cancellation
  • shutdown() - cancel all tasks and wait for completion

License

MIT

Commit count: 0

cargo fmt