Ownership. Use when writing or changing async Rust with Tokio: tasks, select! loops, timers, channels, locks, spawn_blocking, or shutdown cancellation.
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Is that skill safe to install?
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
Are you the author of Rust Tokio?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/dankosik-rust-tokio)
---
name: rust-tokio
description: "Ownership. Use when writing or changing async Rust with Tokio: tasks, select! loops, timers, channels, locks, spawn_blocking, or shutdown cancellation."
---
# Rust Tokio
**Ownership.** Identify who admits work, owns its execution, observes failure, requests cancellation, and awaits completion across future, task, thread, and resource boundaries; a caller's wait and the operation's lifetime can diverge.
**Supervision.** Observe meaningful task outcomes while their parent serves traffic as well as during shutdown, separating unexpected completion from a requested drain (a join winning `select!` can reflect normal drain). Retain spawn handles so tasks stay attached and results stay observable. `crates/service/src/bootstrap` owns admission, `Background` registration, root cancellation, and joining. Treat cancellation propagation as cooperative rather than proof that child work or external effects have stopped.
**Cancellation safety.** Because suspension points allow dropping a future, separate state mutation from `.await` and identify which progress, queue slot, or owned payload may be discarded. `select!` drops losing futures: reserve a channel permit (`mpsc::Sender::reserve().await` then synchronous `permit.send(msg)`) instead of awaiting `send` inside `select!`, pin timers (`tokio::pin!(sleep)`) outside `loop { select! }` or use `interval` with explicit `MissedTickBehavior` so events never starve timeouts, and bind rollback to `std::mem::DropGuard` leases; see [cancellation and lease examples](references/cancel-and-leases.md). Use `CancellationToken::run_until_cancelled` for clean shutdown loops, and reserve `biased;` for deliberate priority paired with admission control.
**Locks, actors, and ordering.** Prefer a short synchronous `parking_lot::Mutex` for low-contention state that never crosses `.await`, keeping coupled mutable fields (such as a map and its event log) under one `Mutex<Inner>` rather than splitting them across two locks that can race, and releasing the guard before dropping freed collections or notifying waiters; move to an Actor handle/task split only when autonomous background work justifies channel overhead. When using `std::sync::Mutex`, recover via `.unwrap_or_else(PoisonError::into_inner)` only if invariants survive unwind, otherwise quarantine partial state. Use `Relaxed` for independent counters, `Acquire`/`Release` to publish state, `SeqCst` only when global order across multiple atomics is required, and `Condvar::wait` inside `while !cond`.
**Blocking boundary.** Move complete blocking operations (including synchronous transactions and lock scopes) to the blocking owner or a supported async adapter (`clippy.toml` bans direct std blocking I/O), while keeping cheap bounded computation inline. Bound per-poll CPU work and tight ready-future loops, bound concurrent fan-out with `buffer_unordered(limit)` or `JoinSet` + `Semaphore` instead of unbounded `join_all`, and register a wakeup before returning `Poll::Pending`.
**Admission lifetime.** Acquire admission before submitting blocking or CPU work and move `let _capacity = permit;` (`OwnedSemaphorePermit`, keeping a named binding so the permit lives through closure execution) inside `spawn_blocking`, since handle abort or waiter timeout leaves a started closure running; see the [execution-lifetime example](references/execution-lifetime.md). Supervise termination and panics, request cooperative cancellation where supported (using `JoinSet::shutdown` when aborting and draining an owned task set whose individual results go uninspected), and bound queued memory alongside task count.
**Timers and lifecycle.** Prefer state-change notifications over polling: carry shared state and its wakeup together in `watch` (`Receiver::wait_for`), reserving `Notify` for stateless edge or permit wakeups with a race-free state check, and bound external polling with an owner, cadence, and terminal condition. Pair `timeout` with resource reclamation that works even when consumers stop polling, use Tokio time for paused-clock tests, keep signal streams alive across their full lifetime, join all test-owned tasks on exit, and synchronize tests on observable state changes within the existing lifecycle owner.