Recent findings suggest that there’s recently been some discussion about the benefits and downsides of lock (mutex) poisoning in Rust, spurred by a recent proposal to make the default mutex non-poisoned, i.e. silently unlock on panic (see also, recent discussion on Hacker News). As a passionate defender of lock poisoning, I thought I’d gather and write about my thoughts on this matter.
According to recent reports, rust, like most multithreaded languages, has mutexes: a construct to ensure that a particular piece of data can only be accessed by one thread at a time. The way mutexes work in Rust is particularly well-considered:.
It has been revealed that this is all quite reasonable! Let’s look at an example that processes incoming messages for a set of tracked operations. Let’s assume that multiple threads could be processing messages, so we have to guard the internal state with a mutex. (We’ll discuss alternative approaches to this problem later.).
This is a very typical use of mutexes: to guard one or more invariants or properties of some kind. These invariants are upheld while the mutex is unlocked. In this case, the invariant being guarded is that Operations::ops has complete and up-to-date tracking of all in-progress and completed operations.
Of equal importance is the fact that, while the mutex is held, the invariant is temporarily violated. In order to process the message, we have to remove the state from the map, create a new state, then put it back into the map. During this period, Operations::ops is missing this one operation, so it no longer tracks all operations. But this temporary violation is okay, because no other threads see this in-between state. Before the mutex is released, this code is responsible for putting the operation back into the map.
Recent findings suggest that is it always true that the operation is put back into the map? Unfortunately not always, in the presence of what I think of as unexpected errors. Many practitioners draw a separation between two different kinds of errors that a system can have. The terms recoverable and unrecoverable are sometimes used for them, but I tend to prefer the following terms (see also some discussion by Andrew Gallant):. (Related: How to generate text: using different decoding methods ...)
In a recent development, generally, in Rust, expected errors are handled via the Result type, and unexpected errors are handled by panicking. Now, there isn’t a firm requirement that things be this way.
But in typical production-grade Rust, expected errors are Results while unexpected errors (and only unexpected errors) are panics. Lock poisoning is built around this assumption.
Consider what happens if a panic occurs in OperationState::process_message. This depends in part on the build flags and surrounding code, so let’s look closely at all the possibilities. In Rust, there are two ways to configure build flags on panic:.
Some real-world applications (such as most of what we ship at Oxide) abort on panic, but most of this post is actually moot for aborts. So in the rest of this post, we’re going to focus on the default unwind behavior.
If a panic is invoked in the context of a catch_unwind, an Err(E) is returned, where the value E is whatever message or other payload the panic occurred with.
If there’s no catch_unwind, and the panic occurs on the main thread, then a message is printed out and the program exits with an error.
and the program exits with a non-success exit code.
If there’s no catch_unwind, and the panic occurs on a different thread, then a message is printed out, and the panic message is returned as the result of JoinHandle::join.
If you run this slightly more complex program:.
and the program exits with a non-success exit code.
According to recent reports, an interesting thing to note here is that there were two panics: one in the spawned child thread with the panic! message, and one in the main thread when the expect was called. The panic responsible for producing the non-success exit code was the one that occurred in the main thread, not the child thread.
This raises the question: what if a non-main thread panics and the thread is not joined? With this program:.
And the program exits with a successful exit code! There’s no indication that a panic occurred other than the message printed out, which can easily be missed. (Related: Industry News: Deploy Hugging Face models easily with A...)
The upshot of all this is that panics in non-main threads are not magic. In order for the system to make decisions based on whether a panic occurred, it must process that, either with catch_unwind or via JoinHandle::join, and it’s all too easy to just ignore panics.
Coming back to our Operations example above, what does that mean for our mutex’s critical section?
This might look like a lot of ifs, but they’re more common than you might think: they’re all either the default in Rust or a very common way to write code.
Rust’s designers had the foresight to see this matter, and introduce lock poisoning as a detection mechanism for this failure mode. The way poisoning works is that:.
According to recent reports, almost all code immediately panics on seeing a PoisonError via .lock().unwrap(): this is often called propagating panics. But PoisonError can be handled more explicitly than that. Note that PoisonError, and poisoning more generally, is purely advisory: you can retrieve the data underneath, and even clear the poison bit in Rust 1.77 and above.
It has been revealed that the fact is, though, that anything other than .lock().unwrap() is rare in practice. This is emphatically not a reason to remove poisoning, and in fact is a strong argument to retain poisoning while making the ergonomics better (see below). What is important is detection, not recovery.
According to recent reports, so, putting it all together: if a child thread panics in a critical section, then it is quite possible that the data is in an inconsistent or logically corrupt state. To indicate this, the mutex is marked poisoned. If the child thread is not waited on by the parent, this might be the only indication that a panic previously occurred in a critical section!
It is precisely this confluence of factors that makes lock poisoning such an vital feature.
Is the problem of inconsistent mutex-guarded state limited to panic unwinding? I’d argue that it is a property of unexpected cancellations more generally: you start executing a critical section thinking that it will be run to completion, but something causes that process to be interrupted.
In Rust, there are two sources of unexpected cancellations, with strong parallels between them:.
As documented in Oxide RFD 397 and RFD 400, unexpected future cancellations have resulted in so many mutex invariant violations that we now avoid Tokio mutexes entirely1. My perspective here comes from much pain dealing with this concern in async Rust, and wanting very much for this footgun to not make its way to synchronous Rust.
See the appendix for more details.
In other words, is poisoning often too conservative? My answer to this is that panics do not always cause invariant violations, but they’re so common, and the downsides of corrupt state so unbounded, that it is still valuable to have lock poisoning as a strong heuristic.
In a recent development, firstly, if all you’re doing is reading data that just happens to be guarded by a mutex (maybe because some other function writes to that data), a panic in the critical section can’t cause invariant violations. (But also, you may wish to use an RwLock.).
Secondly, some simple kinds of writes can also avoid causing invariant violations. For example if all you’re doing is updating some counters2:.
Finally, it is sometimes possible to carefully architect code to be unwind safe, such that if a panic occurs, either:.
Sources indicate that for example, the standard library’s HashMap and BTreeMap are architected this way. In our Operations example, we could, rather than removing the operation from the map entirely, replace it with an Invalid sentinel state.
In these cases, it is true that a panic in a critical section is not harmful, and that the typical .lock().unwrap() approach will reduce system availability. But the crucial thing to keep in mind is that code changes over time. One of the things I like about Rust is how resilient it is to changes over time: by encoding properties like mutable access into the type system, Rust makes it that much harder for new team members (or even yourself six months from now) to screw up. However, like async cancel safety, unwind safety is not encoded in Rust’s type system3, so it’s easy for code that’s fine today to be wrong tomorrow.
The main downside to a .lock().unwrap() that misfires is reduced availability and denial of service. But the downsides to an undetected panic are unbounded, and can range from denial of service all the way to “(part of) an HTTP request ending up sent to a party it should not have been sent to,” or in other words personal information leakage.
A downside that (while potentially serious) is bounded, versus the kind of flaw that can kill an organization—I know which default I want.
What about writing panic-free code? You can carefully write your critical sections to not have panics. But that is a property that’s especially hard to maintain as code changes over time. Even something as simple as a println! can panic. Also, if the critical section can’t panic, then it doesn’t matter whether the mutex poisons or not.
In a recent development, A bit of history here: in Rust 1.0, panics could only be detected at thread boundaries via JoinHandle::join. This meant that back then, the only way for panics to cause invariant violations was for:.
Since then, two Rust features were added:.
With both of these, you can operate on arbitrary data (i.e. not just data guarded by a mutex) and leave it in an inconsistent state. To see how, let’s rewrite the Operations example above to not have a mutex inside of it, and to require exclusive access to make any modifications to it.
In a recent development, since there are no mutexes involved any more, this is no longer a critical section in the classical sense. But note that we still have the invariant that ops tracks all operations. This invariant is temporarily violated, with the idea that it’ll be restored before the function returns. Since &mut means nothing else has any kind of access (read or write) to this data, we know that this in-between state is not seen by anybody else.
Sources indicate that but just like with mutexes, this breaks down with unwinding. With catch_unwind, you can do:.
And with scoped threads, you can do:.
Sources indicate that if a panic occurs in process_message, Operations is logically corrupted. This failure mode has resulted in a proposal to have a Poison wrapper that poisons on panicking. That absolutely makes sense and is worth pursuing.
According to recent reports, but, along with the Poison wrapper, there are some suggestions to go further and suggest that the current std::sync::Mutex type should be changed in the next Rust edition to, instead of poisoning on panic, silently unlock. (And also, as a followup, that the current Mutex should instead become Mutex>.).
(It’s worth noting that there’s another non-poisoning option: the mutex stays locked forever, as C programmers might expect. This option is somewhat appealing because it is safe by default, in a sense. But once a thread is stuck waiting on the mutex, there’s no easy way to recover. So it’s not panics that propagate, it’s stuck threads. This seems strictly worse than a poisoning mutex to me, so I’ll assume the proposal means silent unlocking.).
I first want to give credit to this proposal: it is quite beautiful.
While all of these are true, I keep coming back to how unbounded the downside of an undetected panic is, and how easy it is to get wedged in this state. Mutexes and poisoning have value separate from each other, but I don’t think they are as independent as they seem at first. My understanding from writing Rust code over many years is that almost all uses of mutexes benefit from poisoning, and almost all instances of poisoning one needs to care about are with mutex-guarded data.
Recent findings suggest that why are mutexes special? As we’ve established above, poisoning can make sense in non-mutex contexts too. What makes mutexes a particularly valuable place to have it? The reason has to do with what I like to call the cancellation blast radius. Corrupted data only matters if it is shared mutable state: changed in a way that resulted in an invariant violation, and visible outside of where the failure occurred. If a typical &mut reference panics in the middle of an operation, it’s likely that the data it is referring to will also be torn down, in which case those invariant violations don’t matter4. But the whole point of mutexes is that they’re shared across threads! So data corruption is almost certain to be externally visible.
There are some use cases that would benefit from non-poisoning mutexes, like metrics and best-effort logging, but those cases shouldn’t drive the default.
More specifically, I am worried that a common complaint about lock poisoning (see below) is that it has too much friction. Having to use Mutex> instead of Mutex adds even more friction, so people are going to opt for non-poisoning mutexes more of the time. This is going to lead to grave mistakes in production.
This is a spot where zero-cost abstractions and safety by default are seemingly at odds with each other. I would like to see performance numbers to quantify this better, but if I may hazard a guess, the incremental cost of checking the poison flag (a single atomic load with relaxed ordering) is minimal compared to the cost of acquiring the lock in the first place.
Industry observers note that I mentioned earlier that parking_lot’s mutexes silently unlock on panic. A large chunk of the Rust ecosystem uses parking_lot today, often for performance reasons. Does that mean that code using parking_lot has these unbounded downsides?
Sources indicate that 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.
According to recent reports, in this case, the binary is configured to abort on panic, so it’s fine. But reusable Rust libraries cannot require panic = 'abort', and if this code were in a library on crates.io, it would be a real cause for concern.
According to recent reports, A common response to this class of glitch is to not bother with any of this unwinding stuff, and always abort on panic. This comes back to the idea of the cancellation blast radius described above: aborting the process on panic guarantees that in-memory state is torn down.
Sources indicate that I have a lot of sympathy for this idea! This is what we do at Oxide. (Why am I writing this post if it doesn’t affect my workplace? Well, first, I care about the health of Rust more generally. Second, libraries must work with unwinds. But most importantly, we have seen the pain of unexpected async cancellations at Oxide, so we know how bad it can be.).
But also, that works fine with the current approach: .lock().unwrap() always succeeds. Whether mutexes poison or not only matters with panic = 'unwind'.
This leads to what I think is driving a lot of discussion here:.
Recent findings suggest that I get this complaint. I really do. Having to write .lock().unwrap() everywhere sucks. It’s extra characters in a language already filled with syntax noise. It can cause rustfmt to format your line of code across multiple lines.
Sources indicate that these are all valid points. But there is a much better solution for them, one that doesn’t give up the very important benefits of poisoning: in the next Rust edition, make lock() automatically panic if the mutex is poisoned! (And add a lock_or_poison method for the current behavior5).
It’s worth comparing the different options here:.
Based on this table, I believe the answer is clear: if a breaking change is going to happen, it’s much better to make lock automatically panic than to make panics silently unlock.
According to recent reports, concurrent programming is very difficult. Rust makes it easier than most other languages, and lock poisoning is an crucial part of the story. Let’s avoid introducing any regressions here.
Sources indicate that providing a Poison wrapper makes a lot of sense. Making the default std::sync::Mutex silently unlock on panic would, however, be a mistake.
In a recent development, should Rust’s standard library even provide non-poisoning mutexes? That’s a harder question. I’m worried that their mere presence in the standard library will lower the barrier to people doing the wrong thing, particularly in libraries where panic = 'abort' cannot be assumed. But I think non-poisoning mutexes have some legitimate uses, so I don’t object too strenuously if the tradeoffs are carefully documented.
Writing all this out was very helpful to me in getting my thoughts straight, and I hope it’s helpful to you too.
Recent findings suggest that cover photo by Karen Rustad Tolva, used with permission. Thanks to Fiona and several of my colleagues at Oxide for reviewing drafts of this post. Any errors in it are my own.
Discuss on Hacker News, Lobsters, and /r/rust.
Updated 2025-12-06: Added a section to Separating mutexes from poisoning on what makes poisoning particularly valuable for mutexes, more than in other contexts.
Unlike panics, the standard library’s mutex does not poison on future cancellations. (I believe it’s not possible to poison on future cancellations with the RAII pattern.).
It has been revealed that but wait! Tokio provides its own Mutex type, which is sendable to another thread. This means that it is possible to put an await point in a critical section, and so the issue of unexpected cancellations within a critical section rears its ugly head with Tokio mutexes.
Tokio mutexes also do not poison if the critical section panics. At Oxide we abort on panic so this matters less as a reason to avoid them, but is yet another asterisk that pushes me away from them. ↩︎.
Industry observers note that A more typical way to manage simple counters is with an atomic, but if the data is more complex than just an integer, a mutex may be necessary. ↩︎.
In a recent development, rust does have some rudimentary support for unwind safety in the type system, but it is more of a suggestion that most consumers ignore, and because of that there’s a proposal to remove it from Rust. ↩︎.
The exception is Drop implementations, which may have to be written robustly to guard against temporary invariant violations. ↩︎.
The more obvious try_lock is already taken. ↩︎.
