I'm currently working with the whisper-rs crate, which is a Rust binding to whisper.cpp. The whisper.cpp codebase uses abort() calls for error handling, which means simple errors from c++ often turn into mysterious issues in rust.
I’m aware that Rust cannot currently catch foreign exceptions. However, it would still be valuable to capture and display the error messages from these c++ aborts before the program crashes. This would provide some insight into the errors instead of leaving them as a mystery, which has led to numerous issues with crates.
Could you suggest any improvements or approaches to achieve this? Thanks!
Abort in C and C++ isn't exceptions, and doesn't involve unwinding. It is basically the same as calling exit. So in this case there isn't anything that can be done (except by replacing the libc function).
As for your options: patch the C++ library to not use abort would be the best option. Then you could catch the exceptions on the edge of the C++ code and convert them into return values for Rust to handle.
Another option that might work (on ELF based Unix, so everything except Mac OS) would be to use LD_PRELOAD to replace abort with your own abort that logs more. It would be UB to not exit at that point (I'm fairly certain), but perhaps you could do something to also capture the stack trace and log it.
Using a separate crash handler process like browsers etc do might be the cleaner option. This process then acts as debugger for your process and uploads the crash to a server of some sort.
Technically POSIX abort sends a SIGABRT before aborting, so you could learn about signal handlers and print something out that way. You’re not going to be able to unwind from the abort though (please don’t try, no OS will help you do it).