Abolish Lock Poisoning
The feature known as mutex poisoning in Rust has been a topic of much debate within the language community. When a std::sync::Mutex is held during a Rust code panic, the mutex becomes poisoned, preventing further calls to lock() and returning a PoisonError. The purpose behind this feature is to maintain internal invariants and prevent objects from ending up in a broken state due to panics occurring at the wrong moment.
However, the author of this account expresses their disdain for this feature, stating that they have experienced numerous issues with it and cannot recall any instances where it has been helpful.
The issue with poisoned locks is that they are often mishandled in real-world code, leading to worse performance than locks without poisoning. Josh Triplett highlighted this concern in a Hacker News comment, stating that there has never been a single report of anyone attempting to recover from poison in the entire history of the standard library.
When a panic occurs while a lock is held, there are two possible reactions: shutting down the process or continuing and ignoring the flag. With panic set to "abort," the former occurs, and poisoning never comes into play. However, using the idiomatic lock().unwrap() with unwinding leads to the worst possible outcome, as it neither stops the process nor allows it to continue, turning subsequent lock calls into panics that can have disastrous effects.
A subtle aspect of poisoning is that the PoisonError needs to hold the guard, as fetching the guard from the error object is required to ignore the poison flag. This can result in deadlocks when common code that handles errors wraps or converts them, keeping the mutex locked as long as the error lives. The issue can also manifest in situations where an error is handled using if let, as seen in the cc-switch test, which became hung due to a fallback locked the same mutex again while the PoisonError from the condition still owned the guard.
Another problem with poisoning is that it fails to fail fast and instead remains dormant, leading to prolonged issues. Once the original panic happens on one thread, subsequent lock().unwrap() calls on that lock panic as well, keeping the process running but rendering it useless. This problem has been observed in test suites, cargo, Deno, Polars, and even Rust itself.
Additionally, the author notes that standard I/O has ignored poisoning since March 2015, two months before Rust 1.0, as they believed "any poison must have been the result of user code which the lock isn't protecting."
The ecosystem has largely come to the conclusion that poisoning is an anti-feature, with Tokio's locks, Wasmtime, Crossbeam-channel, tracing-subscriber, and even rust-analyzer opting to ignore locking poisoning. The parking_lot library, which does not support poisoning, has been adopted by many as a more reliable alternative. The author argues that poisoning should either be entirely removed or made an exception, highlighting the numerous issues it has caused in the Rust community.
Rewritten by urgent.news; may contain errors — check the original.
Source: lucumr.pocoo.org