Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better backtrace when Rust panics #117

Open
mimoo opened this issue Mar 6, 2023 · 5 comments
Open

Better backtrace when Rust panics #117

mimoo opened this issue Mar 6, 2023 · 5 comments

Comments

@mimoo
Copy link
Contributor

mimoo commented Mar 6, 2023

Currently Rust panicking can be caught as an exception in OCaml, and converted as a string it looks somewhat like that:

(Failure  "assertion failed: `(left == right)`\
         \n  left: `1`,\
         \n right: `0`")

but there is no backtrace, and not much information about the panic. Is it possible to obtain better backtraces?

@zshipko
Copy link
Owner

zshipko commented Mar 6, 2023

Right now I don't think that's possible - if you wanted to look into it I think the best path would be using this crate: https://crates.io/crates/backtrace to add the backtrace information to the error message in the panic handler:

::std::panic::set_hook(Box::new(|info| unsafe {

@mimoo
Copy link
Contributor Author

mimoo commented Mar 6, 2023

I exported that function:

    pub fn init_rust_panic_hook() {
        panic::set_hook(Box::new(|_| {
            let bt = Backtrace::new();
            println!("Rust custom panic hook: {:?}", bt);
        }));
    }

and I'm initializing it in Ocaml:

let () = RustHelpers.init_rust_panic_hook ()

but when the panic happens this is what I get:

File "src/lib/crypto/snarky_tests/dune", line 2, characters 7-19:
2 |  (name snarky_tests)
           ^^^^^^^^^^^^
Rust custom panic hook: 
fatal runtime error: failed to initiate panic, error 5

no dune/ocaml output past the panic, interesting o.o

@mimoo
Copy link
Contributor Author

mimoo commented Mar 6, 2023

removing the use of Backtrace and simply printing the argument to the closure actually improved things.

my theory is that the panic raised by OCaml is also caught by my hook, and it doesn't let OCaml recover

@mimoo
Copy link
Contributor Author

mimoo commented Mar 20, 2023

adding a backtrace to an error type (with thiserror) also creates issues. I've posted on ocamlforum as well: https://discuss.ocaml.org/t/catching-panics-in-rust/11730

@mt-caret
Copy link

Hi, I ran into a similar issue where I was trying to do something similar, but with ocaml-interop. I'm not exactly sure why I didn't hit any issues, since I was able to convert panics into exceptions (including backtraces) without any trouble; here's an expect test which demonstrates the code; if you change suppress_backtrace to false you should see a backtrace (and here's the code which implements the panic hook which is then converted into an OCaml exception).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants