Login
You're viewing the code4lib.social public feed.
  • Nov 27, 2026, 1:09 AM

    I tried to make a flowchart to clarify my thinking about #rustlang lock poisoning.

    I started it to illustrate the hazards of nonpoison Mutexes, but it also made me realize how much we should prefer the offramp to panic="abort", because any kind of unwinding means we need to clean up after a PoisonError or risk broken behavior.

    Are you using threads and Mutexes? If no, no problem!
Do your data structures contain invariants that are maintained by critical sections? If no, can you guarantee that nobody will change that in the future? If you can, then you can use nonpoison Mutexes.
Are you willing to sacrifice a little performance to ensure correctness? If no, your program results may be incorrect, but you can use nonpoison Mutexes.
Do you need panic to unwind the call stack, to keep a service running even when e.g. a route handler panics? Or because Drop impls are required for correctness? If no to both, you can use panic="abort" and choose any Mutex implementation.
If you're still with us, choose the poisoning Mutex (currently the default in std).
Do you have a way of recovering from a PoisonError in every code path that calls Mutex::lock()? If no, your program may get stuck in a panic loop due to unhandled PoisonError.
If yes, awesome! You should write up how you did it, so that others can learn how.
    💬 2🔄 2⭐ 3

Replies

  • Nov 27, 2026, 1:19 AM

    A few things that would make this chart easier to swallow:
    1. Embed "no invariants maintained by critical sections" into the type system, to enforce which types are safe for nonpoisoning Mutexes.
    2. Prove "no panics in the critical section", making those paths safe for nonpoisoning Mutexes.
    3. Make recovery easier and more idiomatic, maybe by requiring a rollback or recovery function, or treating every critical section as a transaction, or restarting entire subsystems on panic.

    💬 0🔄 0⭐ 1
  • Nov 27, 2026, 1:46 AM

    @ericseppanen The more experience I get with Rust, the more I become convinced unwinding should never have been added to the language. Recovery from catastrophic program errors is what process isolation is for; fork is your friend, not some ill-considered hack from the 1970s.

    💬 0🔄 0⭐ 0
  • Nov 27, 2026, 2:02 AM

    @ericseppanen A possible compromise solution is to wrap the mutex in a type that makes it transactional - gives read access via a & reference, and write access only by replacing the entire value when unlocking, so you get all-or-nothing semantics regardless of panics and other divergence. That's usable for small Copy payloads.

    💬 0🔄 0⭐ 0