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.
Panics are not bad exceptions just because you can catch them.
panics are meant to signal "this code is utterly broken"
Yes!
the approach would be an external supervisor of sorts
Yes!
But does that supervisor need to be at a process level?
Imagine you're writing S3 in Rust. You have many requests in flight, they're mostly independent and stateless, and some of them are extremely long running and costly to abort. If one request panics, why should that take down the entire server process? If the code using shared resources within the server is written to be cancel-safe (i.e. using RAII types, scope guards, etc), panics can unwind the request task/thread, and the server to continue handling other requests without interruption.
That's a request-level supervisor, rather than process-level. And panics remain a path of last resort to bail out of a truly broken request when it cannot continue.
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
This is not highly specialized. A Rust S3 service would likely use async, and therefore also need cancellation safety, which is accomplished with all the same techniques (i.e. using RAII types, scope guards, etc). Add some discipline about propagating panics from subtasks—which can be done reliably with project-specific helpers and clippy rules—and you're done.
I think you are right, the thing that is unergonomic is:
... (i.e. using RAII types, scope guards, etc). Add some discipline about propagating panics from subtasks—which can be done reliably with project-specific helpers and clippy rules ...
Now... really we aren't in a signficantly better place than a moderately complex C++ project. I just want the type checker to enforce this stuff, the async library to enforce structured concurrency where we can just say "oh this task aborted, I cleaned it up and am letting you know", the language async features to enforce that the async library enforces structured concurrency correctly, etc. (Not claiming such a language is easy to design and implement - but that this is the bar).
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".
The reason this analogy does not work is because you are not driving just one car, but hundreds of thousands of cars out of the same company. When one car explodes while driving you don't cycle out all cars and replace them with new cars. You take that one bad car out of rotation.
That's the situation lots of server code finds itself today.
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.
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.
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.
What is not really covered in the post here is that there really are two separate worlds of Rust. There is the world of Rust where you might as well disable panicking and just go straight to abort. A lot of code can work in that environment and it's the better default for that type of code.
On the other hand though there are entire ecosystems that cannot live in that world at all because panics are frequent in practice, and concurrency will turn those panics into denial of service vectors. In remote applications quite a few of these can be attacker triggered.
There is an alternative world where Rust wouldn't have relied on panics for quite as many operations, but panics are very common in the standard library and some operations can only work because panics exist (eg: overflow checks, there is just no syntax support for checking operations today). So that world needs to be supported somehow and it has to work better.
Exceptions have the issue of being too easy to use while also being too easy to ignore. This puts you in a situation where people are encouraged to produce code where common and uncommon errors both end up handled in the same way, and where nobody actually knows which exceptions some code might throw. Catching exceptions is also more fanfare than ideal in most languages (although, of course, you could make it as ergonomic as rust's try operator if you wanted to).
What I think rust is missing is an easy and reliable way of requiring that a function does not panic. At which point it just needs good default panicking APIs with alternatives which don't panic. Not sure how feasible that is for it at this stage in its life though...
Panics were intended to always abort, which makes the story nice and easy: there is no way to recover from panics, and if you want recoverable errors you need to use return values.
But now we are in a world where unwinding from supposedly fatal errors is a thing, with all the care and thought you need to put into every line because you can always be aborted. With exceptions, you always needed to write code that is safe to abort at any point. And turns out that with panic unwinding, you need do the same. So what have we gained, exactly?
(I will grant that perhaps the machine code to do result code checking can be made to be more efficient than the instructions required to do stack unwinding, but I'm talking about the programmer-facing language level here.)
I don't really buy the argument that exceptions are too easy or too verbose. Exceptions behave either like unwinding panics or return codes, and importantly: it's the caller that chooses how they behave. The consumer knows how the function slots into their program best, so putting the control into their hands seems right to me. And the strategies for handling exceptions are well-known: handle only very specific ones, let the rest bubble up, and catch-and-present-to-user at the very highest level. The days of "catch-and-continue" or "catch-and-log-and-rethrow" are well behind us.
I agree knowing the types of errors that can be returned by code you call into would be very useful to know; this was true for exceptions and it is of course still is true for panicking (or not) code. Java tried something with checked exceptions as we all know, and it was bothersome and apparently declared unworkable.
I really don't understand what the fuss about exceptions is. I think they're a great way of error reporting.
The longer they're not reinvented, the better for the ecosystem because it means people are forced to actually handle errors as they occur. Rust not having exceptions is one of the major points in its favor.
ecosystems that cannot live in that world at all because panics are frequent in practice, and concurrency will turn those panics into denial of service vectors. In remote applications quite a few of these can be attacker triggered.
Right, there I’m just thinking more and more “ok… well maybe we should rely on process separation and maybe a panic knocks out 20 threads in a process but your coordinator is at the other side of a process boundary and we’re fine”
Like if you are actually often hitting panics but want to rely on structural, whole program guarantees the language helps you define , it feels like the safe thing to do is figure out a way to work that gives you proper protection without throwing out the guarantees that we end up relying on to think about bugs
Shorter: maybe panic recovery in itself is “the problem”. I joke but tearing my hair out cuz some thread crash broke a bunch of invariants on some shared data structure doesn’t sound worth it
(EDIT: this doesn’t resolve the DoS claims really but I feel like even with panic recovery you’re gonna be in pain if you get panic-causing requests over and over)
Shorter: maybe panic recovery in itself is “the problem”. I joke but tearing my hair out cuz some thread crash broke a bunch of invariants on some shared data structure doesn’t sound worth it
...unless the reason you have a shared data structure is because truly avoiding shared mutability isn't a viable solution and then you just wind up reinventing "some thread crash broke a bunch of invariants on some shared data structure" with less help from the compiler using OS-level cross-process shared memory and synchronization primitives.
Sometimes, it really is a case of "If the problem truly does have a simple solution, then it could have been implemented using Rust's type system within the same address space. If it doesn't, then forcing it to cross process boundaries won't have fixed anything once you meet the acceptance criteria".
And I'm saying that, sometimes, it's not viable to communicate via IPC instead of sharing memory. For example, because the amount of data that would need to be moved back and forth or duplicated would present too significant a bottleneck, either in throughput (moving around) or in RAM requirements (duplicating).
In situations like that, some (but not all) cases can be solved in-process with the Rust compiler ensuring correctness by using something like a Vec<T> where the backing memory is shared/un-moving, while the handle to access it is passed around.
It seems to me that panics (and exceptions) are almost like an even worse version of the old function coloring problem. First, they’re not even part of the type system—whether some code can panic is only tracked via documentation, not types (well, Java sort of tried the types with checked exceptions, but it half-assed it and messed up the ergonomics). Second, the question isn’t really “can this code panic?” it’s “can this code panic and if so, is it recoverable, or would recovery corrupt your program’s invariants (and which ones)?” Whether code can panic can be conservatively approximated with static analysis, but whether recovery is possible is not generally going to be decidable.
The net effect is that panics and exceptions really ruin composition and encapsulation.
yorickpeterse | 4 hours ago
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.
peter | 3 hours ago
Panics are not bad exceptions just because you can catch them.
Yes!
Yes!
But does that supervisor need to be at a process level?
Imagine you're writing S3 in Rust. You have many requests in flight, they're mostly independent and stateless, and some of them are extremely long running and costly to abort. If one request panics, why should that take down the entire server process? If the code using shared resources within the server is written to be cancel-safe (i.e. using RAII types, scope guards, etc), panics can unwind the request task/thread, and the server to continue handling other requests without interruption.
That's a request-level supervisor, rather than process-level. And panics remain a path of last resort to bail out of a truly broken request when it cannot continue.
This is not highly specialized. A Rust S3 service would likely use async, and therefore also need cancellation safety, which is accomplished with all the same techniques (i.e. using RAII types, scope guards, etc). Add some discipline about propagating panics from subtasks—which can be done reliably with project-specific helpers and clippy rules—and you're done.
sph | an hour ago
And thus: Any sufficiently complicated distributed system contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of Erlang.
andyferris | an hour ago
I think you are right, the thing that is unergonomic is:
Now... really we aren't in a signficantly better place than a moderately complex C++ project. I just want the type checker to enforce this stuff, the async library to enforce structured concurrency where we can just say "oh this task aborted, I cleaned it up and am letting you know", the language async features to enforce that the async library enforces structured concurrency correctly, etc. (Not claiming such a language is easy to design and implement - but that this is the bar).
mitsuhiko | an hour ago
The reason this analogy does not work is because you are not driving just one car, but hundreds of thousands of cars out of the same company. When one car explodes while driving you don't cycle out all cars and replace them with new cars. You take that one bad car out of rotation.
That's the situation lots of server code finds itself today.
wofo | an hour ago
Strongly agree! Every time I need locking in my Rust code I use the
parking_lotcrate 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 | 8 hours ago
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 havingMutex<T>be in factMutex<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:
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
Mutexisn'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
panicway more than I'd expect it to (from a Python dev's perspective: it feels like a bunch of people throwingBaseExceptions , 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.
mitsuhiko | 5 hours ago
What is not really covered in the post here is that there really are two separate worlds of Rust. There is the world of Rust where you might as well disable panicking and just go straight to abort. A lot of code can work in that environment and it's the better default for that type of code.
On the other hand though there are entire ecosystems that cannot live in that world at all because panics are frequent in practice, and concurrency will turn those panics into denial of service vectors. In remote applications quite a few of these can be attacker triggered.
There is an alternative world where Rust wouldn't have relied on panics for quite as many operations, but panics are very common in the standard library and some operations can only work because panics exist (eg: overflow checks, there is just no syntax support for checking operations today). So that world needs to be supported somehow and it has to work better.
rix0rrr | 5 hours ago
To me it looks like Rust is taking the long way round to reinventing Exceptions.
atk | 5 hours ago
Not sure why you think so. I don't agree
Exceptions have the issue of being too easy to use while also being too easy to ignore. This puts you in a situation where people are encouraged to produce code where common and uncommon errors both end up handled in the same way, and where nobody actually knows which exceptions some code might throw. Catching exceptions is also more fanfare than ideal in most languages (although, of course, you could make it as ergonomic as rust's try operator if you wanted to).
What I think rust is missing is an easy and reliable way of requiring that a function does not panic. At which point it just needs good default panicking APIs with alternatives which don't panic. Not sure how feasible that is for it at this stage in its life though...
rix0rrr | 4 hours ago
Panics were intended to always abort, which makes the story nice and easy: there is no way to recover from panics, and if you want recoverable errors you need to use return values.
But now we are in a world where unwinding from supposedly fatal errors is a thing, with all the care and thought you need to put into every line because you can always be aborted. With exceptions, you always needed to write code that is safe to abort at any point. And turns out that with panic unwinding, you need do the same. So what have we gained, exactly?
(I will grant that perhaps the machine code to do result code checking can be made to be more efficient than the instructions required to do stack unwinding, but I'm talking about the programmer-facing language level here.)
I don't really buy the argument that exceptions are too easy or too verbose. Exceptions behave either like unwinding panics or return codes, and importantly: it's the caller that chooses how they behave. The consumer knows how the function slots into their program best, so putting the control into their hands seems right to me. And the strategies for handling exceptions are well-known: handle only very specific ones, let the rest bubble up, and catch-and-present-to-user at the very highest level. The days of "catch-and-continue" or "catch-and-log-and-rethrow" are well behind us.
I agree knowing the types of errors that can be returned by code you call into would be very useful to know; this was true for exceptions and it is of course still is true for panicking (or not) code. Java tried something with checked exceptions as we all know, and it was bothersome and apparently declared unworkable.
I really don't understand what the fuss about exceptions is. I think they're a great way of error reporting.
BenjaminRi | 3 hours ago
The longer they're not reinvented, the better for the ecosystem because it means people are forced to actually handle errors as they occur. Rust not having exceptions is one of the major points in its favor.
rtpg | 5 hours ago
Right, there I’m just thinking more and more “ok… well maybe we should rely on process separation and maybe a panic knocks out 20 threads in a process but your coordinator is at the other side of a process boundary and we’re fine”
Like if you are actually often hitting panics but want to rely on structural, whole program guarantees the language helps you define , it feels like the safe thing to do is figure out a way to work that gives you proper protection without throwing out the guarantees that we end up relying on to think about bugs
Shorter: maybe panic recovery in itself is “the problem”. I joke but tearing my hair out cuz some thread crash broke a bunch of invariants on some shared data structure doesn’t sound worth it
(EDIT: this doesn’t resolve the DoS claims really but I feel like even with panic recovery you’re gonna be in pain if you get panic-causing requests over and over)
ssokolow | 5 hours ago
...unless the reason you have a shared data structure is because truly avoiding shared mutability isn't a viable solution and then you just wind up reinventing "some thread crash broke a bunch of invariants on some shared data structure" with less help from the compiler using OS-level cross-process shared memory and synchronization primitives.
Sometimes, it really is a case of "If the problem truly does have a simple solution, then it could have been implemented using Rust's type system within the same address space. If it doesn't, then forcing it to cross process boundaries won't have fixed anything once you meet the acceptance criteria".
rtpg | 4 hours ago
To be clear I’m saying “use a process, communicate via chosen IPC, don’t share memory”. Like just go full Erlang
ssokolow | 2 hours ago
And I'm saying that, sometimes, it's not viable to communicate via IPC instead of sharing memory. For example, because the amount of data that would need to be moved back and forth or duplicated would present too significant a bottleneck, either in throughput (moving around) or in RAM requirements (duplicating).
In situations like that, some (but not all) cases can be solved in-process with the Rust compiler ensuring correctness by using something like a
Vec<T>where the backing memory is shared/un-moving, while the handle to access it is passed around.hyperpape | an hour ago
It seems to me that panics (and exceptions) are almost like an even worse version of the old function coloring problem. First, they’re not even part of the type system—whether some code can panic is only tracked via documentation, not types (well, Java sort of tried the types with checked exceptions, but it half-assed it and messed up the ergonomics). Second, the question isn’t really “can this code panic?” it’s “can this code panic and if so, is it recoverable, or would recovery corrupt your program’s invariants (and which ones)?” Whether code can panic can be conservatively approximated with static analysis, but whether recovery is possible is not generally going to be decidable.
The net effect is that panics and exceptions really ruin composition and encapsulation.
bryce | an hour ago
I would simply not panic while holding a lock that protects invalid state another thread might want to recover and reuse.
mxey | 29 minutes ago
What if you have a bug and use a too high index for a vector? That’s a panic.