Mutex poisoning is not for everyone bertptrs.nl · 7 points · 1 comments · HN thr...
HN Best 200 @best
Mutex poisoning is not for everyone bertptrs.nl · 7 points · 1 comments · HN thread
TL;DR The author argues that Rust's default mutex poisoning is the wrong choice for most code. He measures that poisoning adds size and instructions to every lock() call, pushes developers toward unwrap(), and solves only a narrow class of invariant violations. He concludes that std::sync::nonpoison::Mutex should be stabilised and std::sync::Mutex should not panic by default.
- Poisoning is implemented as an AtomicBool in every Mutex, which can add up to a pointer of size per mutex, which matters for many small guarded objects.
- Compiling a lock-then-unwrap function on x86 produced 147 lines of assembly, versus 68 with unwrap_or_else(PoisonError::into_inner) and 34 with the nightly nonpoison Mutex.
- Using unwrap() on lock results trains bad habits, and many operations such as indexing and integer division can also panic, so 'the code never panics' is hard to guarantee.
- Poisoning protects only against panics during invariant violations, but the same problem exists for plain &mut references, so poisoning is an inconsistent special case.
- Poisoning can be added as a wrapper but cannot be removed from a primitive, which is why the author favours a non-poisoning default with poisoning opt-in.
- The author notes that crates like parking_lot already fill the gap, which he sees as an ecosystem problem; he recommends stabilising nonpoison and avoiding making Mutex panic by default.
Discussion The comment thread is very small, with a single top-level comment. It asks a technical question about the design: why poisoning must cost anything on the uncontended fast path, suggesting that a bit within the futex word could make acquisition fail and route poisoned cases to a slow path. The mood is constructive and focused on implementation details rather than on the broader policy argument.
- Poisoning could be moved off the uncontended fast path (a few): A commenter suggests storing the poison state as a bit in the futex word so that a poisoned mutex makes the acquire fail and falls into a slow path. That would avoid the cost the article measures on every lock. The idea answers the author's central performance objection without changing the poisoning semantics.