36
you are viewing a single comment's thread
view the rest of the comments
[-] xthexder@l.sw0.com 7 points 2 days ago

In my opinion, catching a panic is a mistake in the first place. They're meant to be unrecoverable anyway, so if you want the program to keep running, use Result types or return errors some other way.

[-] sgued@programming.dev 1 points 1 day ago

There are valid reasons to want to catch panics. The N°1 usage of catch_panic are testing tools. But even then, in a low-stakes web server for example, it can make sense to catch panics to return a 500, and it's unlikely that a panic would lead to corrupted state outside of a poisonned mutex, so keeping running can make sense.

[-] SorteKanin@feddit.dk 4 points 2 days ago

Mutex poisoning isn't really about catching panics. Mutex poisoning happens without any relation to catch_unwind.

The thing with mutex poisoning is that it is multi-threaded. One thread holds the mutex and panics. It does not catch the panic, the thread simply dies and releases the lock while unwinding. That marks the mutex as poisoned.

Another thread then tries to get the mutex and sees the poison error. This other thread is still alive since panics do not propogate among all threads. Panics only kill the thread the panic occurs on.

[-] xthexder@l.sw0.com 2 points 2 days ago* (last edited 2 days ago)

I guess I would have thought panic would trigger other threads to abort as well. Admittedly I haven't really done any multithreading in Rust. I was thinking it was more like exit(status) in C/C++ that immediately terminated the entire process. Exception unrolling in general I feel is something that should be avoided if you care at all about error conditions.

If panics are isolated to one thread, then I guess mutex poisoning is a better default error case than a deadlock where the process never exits. An entirely independent thread might still be unaware the rest of the program has crashed though.

[-] SorteKanin@feddit.dk 2 points 2 days ago

Panics in Rust default to stack unwinding, which I believe is similar to how exceptions work in C++. I don't believe an unhandled exception in C++ kills other threads either? Could be wrong.

Panics can also be configured to instead abort the process immediately, which is the exit(status) way. Obviously with that solution you can never observe a poisoned mutex so you would never need a poisoning mutex in that situation anyway.

[-] fruitcantfly@programming.dev 3 points 2 days ago* (last edited 1 day ago)

I don’t believe an unhandled exception in C++ kills other threads either? Could be wrong.

An unhandled exception in C++ causes the program to call std::terminate, which calls std::abort by default and brings down the entire program. I don't think there's a way to prevent an unhandled exception from doing that without causing nasal demons, but I could likewise be wrong. According to the standard, regarding the function function called by std::terminate:

A std::terminate_handler shall terminate execution of the program without returning to the caller, otherwise the behavior is undefined.

A quick check suggests that GCC's std::terminate calls std::abort if your terminate handler does not, but I don't know if that applies to other implementations.

EDIT: If anyone else is curious, here is the GCC implementation:

void
__cxxabiv1::__terminate (std::terminate_handler handler) throw ()
{
  __try
    {
      handler ();
      std::abort ();
    }
  __catch(...)
    { std::abort (); }
}

The LLVM version does the same thing.

this post was submitted on 18 Nov 2026
36 points (100.0% liked)

Rust

8379 readers
30 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

!performance@programming.dev

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 3 years ago
MODERATORS