知识卡片
持锁回调扩散等待风险
持锁时调用外部闭包,或把MutexGuard交给不受控调用者,会把持锁时长和后续取锁行为扩散到边界外,增加死锁风险。尽量让保护操作短小明确,把必须一致的状态共同保护。锁层级、检测工具和测试可辅助,不能靠要求所有人永不犯错维持正确性。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 3. Concepts Advice · 查看原书依据
Avoid invoking closures with locks held; this puts the code at the mercy of whatever closure gets added to the codebase in the future. Similarly, avoid returning a MutexGuard to a caller: it’s like handing out a loaded gun, from a deadlock perspective. Include deadlock detection tools in your CI system (Item 32), such as no_deadlocks, ThreadSanitizer, or par
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第3章《Advice》(spine7;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch03.html;u105-ch03-041)。
- 结论依据:原文在“Avoid invoking closures”附近的定义、例子或代码支撑本卡核心说明:持锁时调用外部闭包,或把MutexGuard交给不受控调用者,会把持锁时长和后续取锁行为扩散到边界外,增加死锁风险。边界按已读前提解释;示例未执行。
- 原始内容:Avoid invoking closures with locks held; this puts the code at the mercy of whatever closure gets added to the codebase in the future. Similarly, avoid returning a MutexGuard to a caller: it’s like handing out a loaded gun, from a deadlock perspective. Include deadlock detection tools in your CI system (Item 32), such as no_deadlocks, ThreadSanitizer, or par