返回顶部
t

tokio-async-code-reviewTokio异步审查

Reviews tokio async runtime usage for task management, sync primitives, channel patterns, and runtime configuration. Covers Rust 2024 edition changes including async fn in traits, RPIT lifetime capture, LazyLock, and if-let temporary scoping. Use when reviewing Rust code that uses tokio, async/await patterns, spawn, channels, or async synchronization. Also covers tokio-util, tower, and hyper integration patterns.

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 1.0.1
安全检测
已通过
187
下载量
免费
免费
0
收藏
概述
安装方式
版本历史

tokio-async-code-review

Tokio 异步代码审查

审查工作流程

  1. 1. 检查 Cargo.toml — 注意 tokio 特性标志(full、rt-multi-thread、macros、sync 等)。缺少特性会导致令人困惑的编译错误。
  2. 检查运行时设置 — 使用 #[tokio::main] 还是手动构建运行时?多线程还是当前线程?
  3. 扫描阻塞操作 — 在异步函数中搜索 std::fs、std::net、std::thread::sleep、CPU 密集型循环。
  4. 检查通道使用 — 根据通信模式匹配通道类型(mpsc、broadcast、oneshot、watch)。
  5. 检查同步原语 — 验证正确的互斥锁类型、适当的守卫生命周期、无死锁风险。

输出格式

按以下格式报告发现:

text
[文件:行号] 问题标题
严重级别:严重 | 主要 | 次要 | 信息性
问题描述及其重要性说明。

快速参考

问题类型参考文档
任务生成、JoinHandle、结构化并发references/task-management.md
Mutex、RwLock、Semaphore、Notify、Barrier
references/sync-primitives.md | | mpsc、broadcast、oneshot、watch 通道模式 | references/channels.md | | Pin、取消、Future 内部机制、select!、阻塞桥接 | references/pinning-cancellation.md |

审查清单

运行时配置

  • - [ ] Cargo.toml 中的 Tokio 特性与实际使用匹配
  • [ ] 运行时风格与工作负载匹配(I/O 密集型用 multithread,简单情况用 currentthread)
  • [ ] 异步测试使用 #[tokio::test](而非手动构建运行时)
  • [ ] 工作线程数已针对生产环境适当配置

任务管理

  • - [ ] spawn 返回值(JoinHandle)被跟踪,而非静默丢弃
  • [ ] CPU 密集型或同步 I/O 操作使用 spawn_blocking
  • [ ] 任务尊重取消(通过 CancellationToken、select! 或关闭通道)
  • [ ] JoinError(任务恐慌或取消)已处理,而非仅 unwrap
  • [ ] tokio::select! 分支是取消安全的
  • [ ] 在可能的情况下使用 trait 中的原生 async fn 而非 async-trait crate(自 Rust 1.75 起稳定)
  • [ ] 在异步上下文中审查了 RPIT 生命周期捕获 — 在 2024 版中,-> impl Future 现在捕获所有作用域内的生命周期

同步原语

  • - [ ] 当锁在 .await 期间持有时使用 tokio::sync::Mutex;短的非异步代码段使用 std::sync::Mutex
  • [ ] 没有在等待点持有互斥锁守卫(死锁风险)
  • [ ] 使用 Semaphore 限制并发操作(而非临时计数器)
  • [ ] 读密集型工作负载(多读少写)使用 RwLock
  • [ ] 简单信号通知使用 Notify(而非通道开销)
  • [ ] 运行时初始化的单例使用 std::sync::LazyLock 而非 oncecell::sync::Lazy 或 lazystatic!(自 Rust 1.80 起稳定)
  • [ ] 审查了 2024 版临时作用域的 if let 锁守卫模式 — 临时变量提前释放,可能改变借用有效性

通道

  • - [ ] 通道类型匹配模式:mpsc 用于背压,broadcast 用于扇出,oneshot 用于请求-响应,watch 用于最新值
  • [ ] 有界通道具有适当的容量(太小 = 死锁,太大 = 内存问题)
  • [ ] 处理了 SendError / RecvError(表示另一端已丢弃)
  • [ ] 处理了广播 Lagged 错误(接收者落后)
  • [ ] 通道发送者在完成后丢弃以向接收者发出完成信号

定时器和睡眠

  • - [ ] 使用 tokio::time::sleep 而非 std::thread::sleep
  • [ ] tokio::time::timeout 包装可能挂起的操作
  • [ ] 正确使用 tokio::time::interval(.tick().await 用于周期性工作)

严重级别校准

严重

  • - 在异步上下文中未使用 spawn_blocking 进行阻塞 I/O(std::fs::read、std::net::TcpStream)
  • 在 .await 点持有互斥锁守卫(死锁风险)
  • 在异步函数中使用 std::thread::sleep(阻塞运行时线程)
  • 在需要背压时使用无界通道(OOM 风险)

主要

  • - JoinHandle 静默丢弃(丢失错误、僵尸任务)
  • 缺少 select! 取消安全性考虑
  • 针对用例使用了错误的互斥锁类型(std 与 tokio)
  • 网络/外部操作缺少超时

次要

  • - 对微不足道的小型异步块使用 tokio::spawn(开销大于收益)
  • 无正当理由使用过大的通道缓冲区
  • 在 #[tokio::main] 足够的情况下手动构建运行时
  • 在竞争程度高到足以受益于 tokio 异步互斥锁时使用 std::sync::Mutex

信息性

  • - 建议使用 tokio-util 工具(例如 CancellationToken)
  • 用于服务组合的 Tower 中间件模式
  • 使用 JoinSet 的结构化并发
  • 从 async-trait crate 迁移到 trait 中的原生 async fn
  • 从 oncecell / lazystatic 迁移到 std::sync::LazyLock
  • 使用 #[expect(lint)] 而非 #[allow(lint)] 实现自清理抑制

有效模式(不要标记)

提交发现前

在报告任何问题前,加载并遵循 beagle-rust:review-verification-protocol。

标签

skill ai
v1.0.1 最新 2026-4-17 16:22
- Added Rust 2024 support: checklist now covers native async fn in traits, RPIT lifetime capture, LazyLock, and if-let temporary scoping.
- Expanded reference materials: new pinning, cancellation, and Future internals quick reference section ([references/pinning-cancellation.md]).
- Updated guidance for migration from async-trait, once_cell/lazy_static, and for using self-cleaning lint suppressions with #[expect].
- Enhanced checklist for Rust 2024 edition migration and modern concurrency patterns.
- References and documentation updated for completeness and current Rust/tokio best practices.

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部