News for hackers. Stuff that matters.
Re:Inside Rust's std and parking_lot mutexes – who wins? by JoshTriplett on Monday November 24, 2025 @02:24PM (#46038090)
We're currently working on separating poison from mutexes, such that the default mutexes won't have poisoning (no more `.lock().unwrap()`), and if you want poisoning you can use something like `Mutex<Poison<T>>`.
Re:Inside Rust's std and parking_lot mutexes – who wins? by kouteiheika on Monday November 24, 2025 @02:53PM (#46038437)
Yeah, I'm looking forward to it!

While we're at it, another thing that'd be nice to get rid of is `AssertUnwindSafe`, which I find even more pointless.

Re:Inside Rust's std and parking_lot mutexes – who wins? by JoshTriplett on Monday November 24, 2025 @11:07PM (#46042258)
Speaking only for myself (though several other people have expressed the same sentiment), I wish we could get rid of unwinding. That would be a massive challenge to do while preserving capabilities people care about, such as the ability to handle panics in http request handlers without exiting. I think it would be possible, though.
Re:Inside Rust's std and parking_lot mutexes – who wins? by questioner8216 on Monday November 24, 2025 @11:33PM (#46042364)
That sounds really interesting, whether it is done in Rust, some Rust 2.0, or a successor or experimental language. I do not know whether it is possible, though. If one does not unwind, what should actually happen instead? How would for instance partial computations, and resources on the stack, be handled? Some partial or constrained unwinding? I have not given it a lot of thought, though. How do languages without exceptions handle it? How does C handle it? Error codes all the way? Maybe something with arenas or regions?

I do not have a good grasp on panics in Rust, but panics in Rust being able to either unwind or abort dependent on configuration, seems complex, and that design happened for historical reasons, from what I have read elsewhere.

Re:Inside Rust's std and parking_lot mutexes – who wins? by JoshTriplett on Tuesday November 25, 2025 @12:03PM (#46047910)
Vague sketch: imagine if we had panic hooks, and scoped panic hooks (unhooked via RAII). So, for use cases that today use unwinding for cleanup (e.g. "switch the terminal back out of curses mode"), you do that cleanup in a panic hook instead.

The hard use case to handle without unwinding is an HTTP server that wants to allow for panics in a request handler without panicking the entire process. Unwinding is a janky way to handle that, and creates issues in code that doesn't expect unwinding (e.g. half-modified states), and poisoning in particular seems likely to cascade and bring down other parts of the process anyway if some needed resource gets poisoned. But we need a reasonable alternative to propose for that use case, in order to seriously evaluate eliminating unwinding.

Re:Inside Rust's std and parking_lot mutexes – who wins? by questioner8216 on Tuesday November 25, 2025 @12:25PM (#46048160)
I am not sure that I understand what scoped panic hooks would or might look like. Are they maybe similar to something like try-catch-finally in Java? Would the language force the programmer to include them in certain cases somehow?

If a request handler for example has at some point in time 7 nested calls, in call no. 2 and call no. 6 have resources and partial computation that needs clean-up somehow and somewhere, and call no. 7 panics, I wonder what the code would look like in the different calls, and what would happen and when, and what the compiler would require, and what other relevant code would look like.

Re:Inside Rust's std and parking_lot mutexes – who wins? by JoshTriplett on Tuesday November 25, 2025 @05:27PM (#46051577)
For the simple case, suppose that you're writing a TUI application that takes over the terminal. When it exits, even by panic, you want to clean up the terminal state so the user doesn't have to blindly type "reset".

Today, people sometimes do that by using `panic = "unwind"`, and writing a `catch_unwind` around their program, and using that to essentially implement a "finally" block. Or, they do it by having an RAII type that cleans up on `Drop`, and then they count on unwinding to ensure their `Drop` gets called even on panic. (That leaves aside the issue that something called from `Drop` is not allowed to fail or panic itself.) The question is, how would you do that without unwinding?

We have a panic hook mechanism, where on panic the standard library will call a user-supplied function. However, there is only one panic hook; if you set it, it replaces the old hook. If you have only one cleanup to do, that works fine. For more than one, you can follow the semantic of having your panic hook call the previous hook, but that does not allow unregistering hooks out of order; it only really works if you register a panic hook once for the whole program and never unregister it (e.g. "here's the hook for cleaning up tracing", "here's the hook for cleaning up the terminal state").

Suppose, instead, we had a mechanism that allowed registering arbitrary panic hooks, and unregistering them when no longer needed, in any order. Then, we could do RAII-style resource handling: you could have a `CursesTerminal` type, which is responsible for cleaning up the terminal, and it cleans up the terminal on `Drop` and on panic. To do the latter, it would register a panic hook, and deregister that hook on `Drop`.

With such a mechanism, panic hooks could replace anything that uses `catch_unwind` to do cleanup before going on to exit the program. That wouldn't fully solve the problem of doing cleanup and then swallowing the panic and continuing, but it'd be a useful component for that.

Re:Inside Rust's std and parking_lot mutexes – who wins? by questioner8216 on Tuesday November 25, 2025 @08:55PM (#46053297)
I have not given it much thought, but it would primarily be for the subset of Rust programs that do not need zero-cost abstractions as much, right? Since, even in the case of no panics, one would be paying at runtime for registering panic hooks, if I understand correctly.
Re:Inside Rust's std and parking_lot mutexes – who wins? by JoshTriplett on Wednesday November 26, 2025 @10:48AM (#46058593)
I can imagine ways to reduce that cost substantially. And the cost would be a key input into the design, since it's important to optimize for the success path and not have the success path pay cost for the failure path.
Re:Inside Rust's std and parking_lot mutexes – who wins? by Rusky on Thursday November 27, 2025 @06:03PM (#46073964)
> Suppose, instead, we had a mechanism that allowed registering arbitrary panic hooks, and unregistering them when no longer needed, in any order. Then, we could do RAII-style resource handling: you could have a `CursesTerminal` type, which is responsible for cleaning up the terminal, and it cleans up the terminal on `Drop` and on panic. To do the latter, it would register a panic hook, and deregister that hook on `Drop`.

This doesn't get rid of unwinding at all- it's an inefficient reimplementation of it. There's a reason language implementations have switched away from having the main execution path register and unregister destructors and finally blocks, to storing them in a side table and recovering them at the time of the throw.

Re:Inside Rust's std and parking_lot mutexes – who wins? by JoshTriplett on Friday November 28, 2025 @02:10PM (#46081753)
The difference is that unwinding unwinds code that isn't necessarily prepared for it, rather than only code that explicitly wants it.

And I would expect to turn it into an efficient solution, in part by doing the "store in a side table" approach for hook registration.

Re:Inside Rust's std and parking_lot mutexes – who wins? by Rusky on Saturday November 29, 2025 @02:52AM (#46085822)
Giving special treatment to code that "explicitly wants" to handle unwinding means two things:

* You have to know when an API can unwind, and you have to make it an error to unwind when the caller isn't expecting it. If this is done statically, you are getting into effect annotation territory. If this is done dynamically, are essentially just injecting drop bombs into code that doesn't expect unwinding. Either way, you are multiplying complexity for generic code. (Not to mention you have to invent a whole new set of idioms for panic-free code.)

* You still have to be able to clean up the resources held by a caller that does expect unwinding. So all your vocabulary/glue/library code (the stuff that can't just assume panic=abort) still needs these "scoped panic hooks" in all the same places it has any level of panic awareness in Drop today.

So for anyone to actually benefit from this, they have to be writing panic-free code with whatever new static or dynamic tools come with this, and they have to be narrowly scoped and purpose-specific enough that they could essentially already today afford panic=abort. Who is this even for?

Re:Inside Rust's std and parking_lot mutexes – who wins? by JoshTriplett on Tuesday December 02, 2025 @11:35AM (#46123031)
To be very explicit about something: these are all vague design handwaves, and until they become not only concrete but sufficiently clear to handle use cases people have, they're not going to go anywhere. They're vague ideas we're thinking about. Right now, panic unwind isn't going anywhere.
Re:Inside Rust's std and parking_lot mutexes – who wins? by sunshowers on Tuesday November 25, 2025 @12:50AM (#46042718)
(If unwinding goes away then, sure, mutex poisoning becomes moot.)
Re:Inside Rust's std and parking_lot mutexes – who wins? by sunshowers on Monday November 24, 2025 @03:55PM (#46039198)
I'm very disappointed at this. The path of least resistance ought to be the right thing to do.
Re:Inside Rust's std and parking_lot mutexes – who wins? by JoshTriplett on Monday November 24, 2025 @08:22PM (#46041342)
In the entire history of the standard library, we have never once seen a single report of anyone attempting to recover from poison.
Re:Inside Rust's std and parking_lot mutexes – who wins? by sunshowers on Monday November 24, 2025 @09:28PM (#46041760)
Oh, I don't think recovery from poison is why poisoning is good. The reason poisoning is good is that at the moment you've acquired a lock on a mutex, you should be able to assume that the invariants guarded by the mutex are upheld (and panic if not).
Re:Inside Rust's std and parking_lot mutexes – who wins? by simonask on Tuesday November 25, 2025 @04:47AM (#46044165)
Mutex doesn't promise to uphold any more invariants than `&mut T` does. If the state can be corrupted by a panic while holding `&mut T`, I don't think there's any good reason to expect that obtaining it through `MutexGuard` should make any difference.

Panic propagation is typically handled much better at thread `join()` boundaries.

Re:Inside Rust's std and parking_lot mutexes – who wins? by zozbot234 on Tuesday November 25, 2025 @05:11AM (#46044333)
A panic in single-threaded, non-parallel code will either terminate the program or be recovered cleanly, so the potential for side effects to be silently observed in a way that breaks invariants is unique to Mutex<>. This is the reason for mutex poisoning,
Re:Inside Rust's std and parking_lot mutexes – who wins? by simonask on Tuesday November 25, 2025 @09:29AM (#46046054)
I fail to see that there is any material difference. Whether you catch-unwind within a single thread or in a separate thread such that the panic can be resumed on join makes zero difference.

Heck, you can have Drop impls observing the state while unwinding.

A true panic-safe data structure requires serious thought, and mutex poisoning does nothing here - it is neither necessary nor sufficient.

Re:Inside Rust's std and parking_lot mutexes – who wins? by JoshTriplett on Tuesday November 25, 2025 @05:29PM (#46051602)
This is exactly the problem. Poison is enough to be painful but not enough to fully solve the problem.

> Heck, you can have Drop impls observing the state while unwinding.

Yeah, this is really painful and regularly forgotten. And one reason it'd be nice to not have unwinding.

Re:Inside Rust's std and parking_lot mutexes – who wins? by dap on Tuesday November 25, 2025 @11:14PM (#46054135)
This is a false dichotomy. Not every technique needs to work in all cases in order to be useful.

This seems analogous to arguing that because seat belts don't save the lives of all people involved in car crashes, and they're kind of annoying, then they shouldn't be factory-standard.

Re:Inside Rust's std and parking_lot mutexes – who wins? by simonask on Wednesday November 26, 2025 @02:34AM (#46055051)
This is a case of a feature that is actively harmful for the things it tries to prevent, because it increases the risk in practice of panics "spreading" throughout a system, even after the programmer thought she had finished handling it, and because it gives a false impression what kind of guarantee you actually have.
Re:Inside Rust's std and parking_lot mutexes – who wins? by sunshowers on Tuesday November 25, 2025 @01:01PM (#46048637)
I understand what you mean, but you're saying has not been true for me in practice. Mutexes absolutely are used to uphold invariants in a way that &mut T is much less often.

There's something to be said here about what I've sometimes called the cancellation blast radius. The issues with cancellation happen when the data corruption/invariant violation is externally visible (if the corrupt data is torn down, who cares.) Mutexes make data corruption externally visible very often.

Re:Inside Rust's std and parking_lot mutexes – who wins? by simonask on Tuesday November 25, 2025 @01:42PM (#46049155)
In projects I've worked on, this just hasn't been the case. Mutexes, especially in Rust, can grant you a `&mut T` when what you have is `&Mutex<T>`, and that's it - failing to uphold invariants in the API surface of `T` is a bug whether or not it lives inside a mutex.

Lots of data structures need to care about panic-safety. Inserting a node in a tree must leave the tree in a valid state if allocating memory for the new node fails, for example. All of that is completely orthogonal to whether or not the data structure is also observable from multiple threads behind a mutex, and I would argue especially in the case of mutex, whose purpose it is to make an object usable from multiple threads as-if they had ownership.

Re:Inside Rust's std and parking_lot mutexes – who wins? by sunshowers on Tuesday November 25, 2025 @08:40PM (#46053173)
Acknowledging that panic safety is a real issue with data structures that mutex poisoning does not solve, I don't think we're going to agree on anything else here, unfortunately. We probably have entirely different experiences writing software -- mutex poisoning is very valuable in higher-level code.
Re:Inside Rust's std and parking_lot mutexes – who wins? by halper on Tuesday November 25, 2025 @02:52AM (#46043381)
Is that not because there is not much to do, and therefore people use .unwrap() — because crashing is actually quite sane?

Correctness trumps ergonomics, and the default should definitely be poisoning/panicking unless handled. There could definitely be an optional poison-eating mutex, but I argue the current Mutex does the right thing.

Re:Inside Rust's std and parking_lot mutexes – who wins? by exDM69 on Tuesday November 25, 2025 @07:22AM (#46045139)
I've used recovering from poisoned state in impl Drop in quite a few places.

In my case it's usually waiting for the GPU to finish some asynchronous work that's been spun up by CPU threads that may have panicked while holding the lock. This is necessary to avoid freeing resources that the GPU may still be using.

I usually prefix this with `if !std::thread::panicking() {}`, so I don't end up waiting (possibly forever) if I'm already cleaning up after a panic.

Re:Inside Rust's std and parking_lot mutexes – who wins? by JoshTriplett on Tuesday November 25, 2025 @06:27PM (#46052093)
Thank you for mentioning this; I'd be really interested in hearing more about this, and seeing some examples.
Re:Inside Rust's std and parking_lot mutexes – who wins? by exDM69 on Wednesday November 26, 2025 @03:23AM (#46055325)
Hi, I don't have public examples to share but I can give an explanation of a simple scenario.

I have a container of resources, e.g. textures. When the GPU wants to use them, CPU will lease them until a point of time in the future denoted by a value (u64) of a GPU timeline semaphore. The handle and value of the semaphore is added to a list guarded by a mutex. Then GPU work is kicked off and the GPU will increment semaphore to that value when done.

In the Drop implementation of the container, we need to wait until all semaphores reach their respective value before freeing resources, and do so even if some thread panicked while holding the lock guarding the list. This is where I use .unwrap_or_else to get the list from the poison value.

It's not infeasible to try to catch any errors and propagate them when the lock is grabbed. But this is mostly for OOM and asserts that are not expected to fire. The ergonomics would be worse if the "lease" function would be fallible.

This said, I would not object to poisoning being made optional.

Re:Inside Rust's std and parking_lot mutexes – who wins? by dap on Tuesday November 25, 2025 @11:16AM (#46047279)
That’s not surprising to me, but it’s not much of an argument for changing the default to be less safe. Most people want poisoning to propagate fatal errors and avoid reading corrupted data, not to recover from panics.

Edit: isn’t that an argument not to change the default? If people were recovering from poison a lot and that was painful, that’s one thing. But if people aren’t doing that, why is this a problem?

Re:Inside Rust's std and parking_lot mutexes – who wins? by JoshTriplett on Tuesday November 25, 2025 @05:28PM (#46051581)
Because right now everyone writes `.lock().unwrap()` everywhere without really thinking about it, and it just makes Mutex more painful to work with.
Re:Inside Rust's std and parking_lot mutexes – who wins? by sunshowers on Tuesday November 25, 2025 @08:36PM (#46053139)
If the issue is that everyone has to write an extra unwrap, then a good step would be to make lock panic automatically in the 2027 edition, and add a lock_or_poison method for the current behavior. But I think removing poisoning altogether from the default mutex, such that it silently unlocks on panic, would be very bad. The silent-unlock behavior is terrible with async cancellations and terrible with panics.
Re:Inside Rust's std and parking_lot mutexes – who wins? by dap on Tuesday November 25, 2025 @11:10PM (#46054120)
You seem to keep making the implicit assumption that because people are using `unwrap()`, they must not care about the poisoning behavior. I really don't understand where this assumption is coming from. I explicitly want to propagate panics from contexts that hold locks to contexts that take locks. The way to write that is `lock().unwrap()`. I get that some people might write `lock().unwrap()` not because they care about propagating panics, but because they don't care either way and it's easy. But why are you assuming that that's most people?
Re:Inside Rust's std and parking_lot mutexes – who wins? by JoshTriplett on Wednesday November 26, 2025 @01:36AM (#46054750)
https://news.ycombinator.com/item?id=46051602

I'm suggesting that the balance of pain to benefit is not working out enough to inflict it on everyone by default. I'm not suggesting it has no value, just not enough to be worth it.

Re:Inside Rust's std and parking_lot mutexes – who wins? by dap on Wednesday November 26, 2025 @04:08PM (#46062330)
I hear that, but it feels kind of empty because I haven't seen much discussion of that cost/benefit analysis (both of poisoning itself and of the change to the default behavior, which has its own costs and benefits).

I take it as uncontroversial that an important function of Mutexes is to ensure that invariants about data are maintained when the data is modified and that very bad things can happen when a program's data invariants are violated at runtime and the program doesn't notice. Maybe folks disagree about whether a program should always panic when invariants are violated at runtime (though there's certainly plenty of precedent in Rust itself for doing this, like with array bounds checking). Probably the bigger question mark is that panicking with a Mutex held doesn't necessarily mean an invariant is violated. But it does mean that the mechanism for ensuring the invariant has itself failed. I can see different choices about what to do here. For myself, the event itself is so rare and the impact of getting an invariant wrong so high that I absolutely do want to panic -- the false positive rate is just too small to matter.

Re:Inside Rust's std and parking_lot mutexes – who wins? by nixpulvis on Monday November 24, 2025 @08:35PM (#46041429)
Excited to hear this.