Skip to content
Back to skills

Rust Tokio

ASecurity

Ownership. Use when writing or changing async Rust with Tokio: tasks, select! loops, timers, channels, locks, spawn_blocking, or shutdown cancellation.

  • 2 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added November 15, 2026
developmentrustgo

Works with

  • terminal
  • cli

Security analysis

A100/100

Pro scans all 3 files and shows the line behind each finding

Scanned November 27, 2026

npx -y skills add Dankosik/rust-service-template-rest --skill rust-tokio --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Rust Tokio?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Rust Tokio
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/dankosik-rust-tokio/badge)](https://www.skillsdirectory.com/skills/dankosik-rust-tokio)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
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.

Files in this skill

  • LICENSE1 KB
  • SKILL.md4.5 KB
  • references/execution-lifetime.md1.3 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.