rust-async-patterns
Installation
SKILL.md
Rust async patterns (Tokio)
Use when building async Rust: HTTP servers (Axum), workers, sqlx, reqwest, streams, and concurrency.
Runtime and tasks
- Use
tokio::spawnfor fire-and-forget work only when errors are logged or joined; preferJoinSet/join!when you need all outcomes. - Don’t block the runtime: avoid
std::thread::sleep, heavysyncmutex work, or CPU-bound loops on async worker threads—offload tospawn_blockingorrayonwhen measured.
Cancellation and shutdown
- Prefer
tokio::select!for racing work vs shutdown signals; propagate cancellation to child tasks when libraries support it. JoinHandle::abortis a hammer; understand whether I/O or DB operations clean up correctly.
Locks in async
- Avoid holding
std::sync::Mutexacross.await—usetokio::sync::Mutexfor short critical sections in async contexts, or restructure to not hold locks over await points. RwLock: choosetokio::sync::RwLockwhen guards cross await;parking_lotonly when stays synchronous.