Mutex poisoning is not for everyone
32 points by polywolf
32 points by polywolf
Couldn't disagree harder with this post. It basically makes two claims:
For 1, yeah, but that's consistent with the rest of the standard library. HashMap defaults to SipHash even though it's slower than something like AHash.. because they consider collision unpredictability to be more important than best-case performance. If your use case is hot enough for this to be a problem and it actually is safe under panicking and you've put in the work to prove that.. you can still use parking_lot! It's good that the option is there.. but it's also good it's easier to use the seatbelt than to turn off the warning.
For 2.. no? We don't do On Error Resume Next all over the place anymore either. I'm sure there are cases where it might make sense, but if silencing the error in those cases is the hard part... I have to doubt that you're actually bothering with the hard part.
I think there are realistically only 3 types of non-trivial rust programs:
Type 1 obviously prefers nonpoison.
Type 2 is the only case where poisoning might be good. But realistically, if a program goes to great lengths to handle panics, it probably don’t want to panic on poisoned mutexes. That kind of hardening is all about using drop guards / RAII to ensure everything stays consistent, especially in async programs where cancel safety matters even more than panic safety.
Type 3 programs have already made the “uptime is more important than correctness” choice. I doubt removing poisoning from type 3 programs would meaningfully increase exposure to bugs for most programs. I also don’t see why Mutex should be a special case that narrowly acts like panic=abort. If you really care about correctness over uptime as a default, you don’t actually want poison Mutex, you want uncaught panics to abort.
I realize that rust is no longer a young language where change can rapidly happen, but it's a shame that people are so afraid to introduce edition breaking changes, this is what editions are for. The issues with poisoning mutexes are well documented, and I think it would be a shame to keep it as the default going forwards.
code in later editions must work together with code in older editions. That would make these changes very challenging.
Is it such a big deal to use another Mutex from a library?
pkolloch 28 minutes ago (unread) |
I don't think it's a big deal to use a Mutex from another library, it's more a matter of a large part of the community saying there's a problem with this implementation, and instead of fixing it, the solution presented is to use something else because it would break compatibility to change the implementation, so we should just stick with this forever?
I'm not aware of specifics with rust editions, I may have been wrong on the point that it could just be changed from one edition to the next without breaking old edition crates. What if std::sync::Mutex just exists as an alias to std::sync::poison::Mutex in edition A and to std::sync::nonpoison::Mutex in edition B?
EDIT: In fact, this appears to be what the rust team plans to do https://github.com/rust-lang/rust/issues/134645#issuecomment-3324577500
EDIT: In fact, this appears to be what the rust team plans to do https://github.com/rust-lang/rust/issues/134645#issuecomment-3324577500
No, it's just an open proposal, which is quite strongly opposed.
I disagree very strongly (the post links to my blog about it, and my position hasn't changed) and will use whatever influence I have to make sure poisoning mutexes stay the default.
It's funny, we disagree about so many things within my workplace. The one thing there is absolute unanimity on is that mutexes should poison by default. I've been on the minority side of plenty of debates and I straight up didn't believe how lopsided the opinion was at first.
I agree with the post.
If you don't trust yourself that you won't have non-unwind-safe code in the critical section - I have bad news for you: unwind safety affects everything outside of critical sections too. In Rust everything can panic, and mutable state can also be external (files, sockets, databases). The classic problem of a counter drifting typically happens with Atomic that can't track poisoning.
These problems aren't limited to panics either. It's super easy to make a bug like:
*locked_state += bad;
op()?;
*locked_state -= bad;
and leave a bad state due to an early return from ?. It's the same bug as an early return due to a panic, but in that case poisoning doesn't trigger!
Poisoning is an incomplete solution to an imprecise assumption about a potential problem. It's the fallacy of "we need something, this is something, therefore we need this".
The real solution is to hunt down the non-transactional state that needs being restored and use drop guards (now in std). This has to be done everywhere, so poisoning won't save you.
In my experience, poisoning is just an outage amplifier for servers. It doesn't prevent errors, it only escalates panics that would be rare and affect only one bad request into a permanent failure that breaks all requests. And if that particular lock isn't checked by your healthcheck, you now have a serious problem. lock().unwrap() is a domino of poison. Do lock().unwrap_or_else(|_| abort()) to at least avoid leaving a server running that panics on every request.
Thanks, this is very well put. I've been advocating for avoiding poisoning in the current form for this reason, and I think correctness is a better argument than performance that this post puts up front.
One thing that gives me pause is that I've just realized that, while I do audit code for unwind safety in critical places, I think I never explicitly considered it whenever I used std::mem::take to workaround borrowck/aliasing model limitations. This pattern was so ingrained for me that I never did a double take. Now I have to worry if I have latent bugs of that sort somewhere; and if I do, whether poisoning mutexes would save me, or if their existence is what caused this frivolousness in the first place.
For whatever reason I've bounced off of explanations of poisoning and why it's a problem in the past, but this was super clear and well organised. I'm now aware of std::sync::nonpoison in nightly and finally understand why parking_lot is a transitive dependency of all my projects. Kudos!