In defense of lock poisoning in Rust (2025)

19 points by m0th 8 hours ago on lobsters | 5 comments

[OP] m0th | 8 hours ago

This was previously posted a year ago: https://sunshowers.io/posts/on-poisoning/

But I wanted to resubmit it because I found it a compelling counter-argument to https://lobste.rs/s/sj2pkv/abolish_lock_poisoning

mitsuhiko | an hour ago

Since you did post this as a response to my post, you probably won't be surprised that I disagree with most of this. But I do want to point out a one interesting thing now that I re-read that one:

The answer depends on a bunch of things, but in general (and especially in library code) that is indeed what I’m suggesting. For instance, this critical section in parity-db is quite large. Reasoning about whether it’s unwind-safe seems very difficult to me; this is exactly the kind of code that mutex poisoning does well to guard against.

The picking of parity-db's commit_raw function quite interesting here because it shows exactly the type of failure that I also mentioned in my blog post: early returns. That function uses ? after taking a critical section so you have early returns in that function while the mutex is held for error handling. In fact the only case I can find in this function of leaving broken state behind is from that error handling and not from panics!

realkc | 2 hours ago

I think we should both get Poison<T> and make std::sync::poison::Mutex's lock panic on poison tbh

I wonder if std::sync::Mutex should be removed in favour of users choosing explicitly if they want poisoning or not tho

mitsuhiko | an hour ago

I think we should both get Poison<T> and make std::sync::poison::Mutex's lock panic on poison tbh

See my post from a day ago about why this would be a pretty terrible outcome.

ssokolow | an hour ago

Identifiers in std/alloc/core cannot be removed (only marked as deprecated) because they need to remain accessible so code written against newer editions can name types needed to interoperate with code written against older editions.