When a thread panics while holding Rust's std::sync::Mutex, the mutex records that it has been "poisoned", and the next lock() returns Err(PoisonError). In mid-November the argument flared up again over whether to stabilize a non-poisoning std::sync::nonpoison::Mutex, and whether a future edition should swap the default. This piece sorts out what lock poisoning protects you from and what it doesn't.

Why now

So the question on the table isn't the name of a new API. It's a design philosophy: how should the standard library's locks treat panics by default?

The problem

The deal with a mutex is that you may temporarily break an invariant while you hold the lock, as long as you restore it before you release. Rain's example:

pub fn process(&self, id: &OperationId, message: Message) {
    let mut ops = self.ops.lock().unwrap();
    // Invariant: ops tracks every operation
    let Some(state) = ops.remove(id) else { return };   // <- temporarily broken here
    let next = state.process_message(message);          // <- what if this panics?
    ops.insert(id.clone(), next);                       // <- this was supposed to restore it
}

If process_message panics and the stack unwinds, the guard's Drop releases the lock, and ops is left missing an entry. Suppose the panic happened off the main thread, nobody checks the join result, and there's no catch_unwind. Then the corrupted state becomes silently visible to every other thread. Rain shows that a panic in a spawned thread can leave the process exiting with a success code, with nothing but a one-line message to show for it.

Poisoning is the detector that keeps anyone from seeing that half-finished state. The debate splits into three questions:

  1. Should poisoning be the default? (Safety versus cost and ergonomics.)
  2. Should poisoning be tied to locks at all? (Shouldn't it be a separate, composable piece, like Mutex<Poison<T>>?)
  3. Can the ergonomics problem (.lock().unwrap()) be fixed without dropping poisoning?

How it works

How poison is set and observed