After some recent discussion on the topic of mutex poisoning and notably a blog post that I thought was interesting, thoughtful, exhaustive, yet completely disagreed with, I wanted to comment on the debate myself. To me, poisoning-by-default is one of biggest mistakes that were made in the language, next to some others that are a story for a different time.
I’m having trouble finding any popular alternative implementations of
Mutexthat support poisoning.parking_lot,tokio, andfuturesall have non-poisoning (possibly async) mutexes.In fact, there’s almost no argument for requiring poisoning. There are some fringe cases where poisoning is useful, and in those cases, you could just as easily create a
Poisoning<T>pointer that implements it.As the article says, no other built-in pointer type in Rust support poisoning, yet the same “invalid state” argument can be made for all of them. A
&mut Tcan become temporarily “invalid” (in a sound manner) during a critical block that panics, for example. That pointer could be mutating data that’s eventually dropped during unwinding, does not hold its invariants at this time, and triggers aDropimpl that’s unsound as a result. At the end of the day, the correct way to handle this is to somehow either fix the state during unwinding so that it no longer violates an invariant, or mark it as poisoned, regardless of pointer type.The only real argument for poisoning by default is because it already is the default. It’s a strong argument of course (it would likely be a breaking change to remove it), but it’s not a very solid argument for why it should be a default to begin with.
https://docs.rs/saa/5.6.4/saa/lock/struct.Lock.html