跳到主要内容
知仓学习社ZHICANG

rust-async-task-design

Design and review Rust async code around task ownership, suspension points, cancellation, blocking work, and runtime boundaries. Use when writing, r…

不碰外部(只输出文字)无严重或高危命中hashgraph-online/awesome-codex-plugins

它会碰到什么

扫了多少3 个文本文件,5 KB
它会碰到什么不碰外部(只输出文字)
命中总数0 处
命中统计严重 0 · 高 0 · 中 0 · 低 0

这一栏是扫描器报的事实,不是结论。命中多不等于有毒(安全工具、规则库、示例脚本本来就会包含危险写法),命中少也不等于干净。它和你手上的凭据、文件、网络有什么关系,需要你自己看。

技能内容

Rust Async Task Design

Use this skill to make Rust async code explicit about task ownership, suspension

points, blocking work, and cancellation. Async is a concurrency model, not a

default replacement for threads or a performance guarantee.

Core Workflow

  1. Identify the async boundary: runtime entrypoint, handler, client call,

stream, background task, or trait method.

  1. Mark every .await as a possible suspension point. Check what state is held

across it.

  1. Use tokio::spawn(async move { ... }) only when the task can own everything

it needs and its future is Send + 'static on the selected runtime.

  1. Keep JoinHandles when task success, panic, cancellation, or shutdown

matters. Do not silently detach important work.

  1. Keep blocking IO and CPU-heavy work out of async tasks. Use async APIs,

spawn_blocking, Rayon, or dedicated threads as the workload requires.

  1. Do not hold a synchronous MutexGuard, RefCell borrow, or non-Send value

across .await.

  1. Add tests for timeout, cancellation, dropped channel, and failed task paths

when those outcomes affect behavior.

Runtime Rules

Read references/async-runtime-patterns.md when reviewing task spawning,

shared state in async code, or blocking work.

  • Use std::sync::Mutex in async code only for short, low-contention critical

sections that do not cross .await.

  • Use tokio::sync::Mutex when a lock must be held across .await, but first

consider moving the state behind a task and interacting by messages.

  • Use spawn_blocking for blocking operations that eventually finish. Limit

parallelism for CPU-heavy work; Tokio's blocking pool can grow large.

  • Use tokio::time::timeout or explicit cancellation tokens for bounded

operations.

  • Use select! carefully: dropping a future can cancel it. Check cancellation

safety before using it around IO or partially completed work.

Async Trait Rules

  • Native async fn in traits is fine for private traits and static dispatch

when callers do not need to add bounds to the returned future.

  • Public traits that need Send futures should usually spell the method as

fn name(...) -> impl Future<Output = T> + Send or use trait-variant to

offer local and Send variants.

  • Public traits that need dynamic dispatch usually need an explicit boxed

future return type, such as Pin<Box<dyn Future<Output = T> + Send + '_>>.

Use async-trait as an ergonomics layer only when the dependency and

allocation tradeoff are acceptable.

  • Avoid leaking executor-specific types from a trait unless the trait is

intentionally runtime-specific.

Review Checklist

  • No lock or borrow crosses .await accidentally.
  • Spawned tasks own their inputs with async move.
  • Important JoinHandles are awaited, monitored, or aborted on shutdown.
  • Blocking work is isolated and bounded.
  • Error paths preserve context instead of becoming JoinError or timeout noise.

想直接用这个技能?

本站把开放许可(MIT / Apache 等)的技能按仓库打包整理到网盘,点一下转存到你自己的网盘,不用一个个从 GitHub 拉。许可未声明的技能只给原始仓库链接,不打包。