Abolish Lock Poisoning

26 points by fanf


yorickpeterse

I think the root of the issue here is that panics can in fact be handled (sort of), rather than being the equivalent of abort(3). The result is that you end up with errors as return values (e.g. Result), and an alternative error handling mechanism that has all the drawbacks of exceptions but without any benefits that justify it.

The usual counter-argument is that you need to be able to catch panics for certain applications, such as mission critical embedded systems. I argue that this is the wrong way of thinking about it: panics are meant to signal "this code is utterly broken", and such errors aren't meant to be handled at runtime. If your car engine explodes while driving you're not going to say "Oh well oopsie-woopsie, let me apply some duct tape to it and then try driving again while pretending this didn't happen". Instead, you stop driving immediately, get out the car and call the fire brigade.

For mission critical systems the approach would be an external supervisor of sorts, perhaps combined with some form of formal verification (ish) that all errors are handled correctly; something that's a lot easier when the possible errors are in fact part of the type signatures. Actually I would argue the former is sensible everywhere (and indeed is often used for server applications as well), the latter would be nice too but often comes at a high cost. Having Claudette-opus-420-69-whatever write the proof isn't much better, as a proof nobody understands is about as useful as no proof at all.

There's also a nice side effect of panics being aborts: they force developers to do something once they hit production, because their impact is very visible, i.e. you can't just slap a catch_unwind() call on top of things and call it a day.

I suspect that in the few cases the above wouldn't be sufficient the requirements are likely such that a highly specialized approach is necessary anyway, and I sincerely doubt Rust's current panic strategy (and all the issues it brings) would suffice in such a case.

wofo

Strongly agree! Every time I need locking in my Rust code I use the parking_lot crate merely because I don't want poisoning. I look forward to the nonpoison mutexes becoming available on the standard library, even if they don't become the default.

rtpg

I went into this pretty against the idea, but I see the arguments reading through this.

I do feel like there's a big tent idea here already in making Poison<T> be a thing and then having Mutex<T> be in fact Mutex<Poison<T>>. Poisoning doesn't seem entirely coupled to mutexes!

The thing that really got me though just looking at the docs and seeing the following:

In addition, the panic detection is not ideal, so even unpoisoned mutexes need to be handled with care, since certain panics may have been skipped.

So you're in this world where even if you're saying "poisoning as a principle is good", it sounds like in practice the poisoning done by Mutex isn't 100% solid anyways (and thus you might want to opt for something way more expensive but that has stronger guarantees.... if that's even possible. I imagine some "poison by default" strategy.)


I struggle with Rust and panics though. In theory I'd expect panics to happen way less than they actually do, and the pedantic thing to do in that case is often just "OK assume the whole program is no longer following invaraints". But this feels entirely unreasonable from an availability perspective.

Actual Rust code seems to end up calling panic way more than I'd expect it to (from a Python dev's perspective: it feels like a bunch of people throwing BaseExceptions , which won't get caught without explicit checks...).

A part of me ends up thinking that my Rust code that has any chance of panicking should just be message passing between independent processes. But I don't really want to re-invent OTP.