Coder Social home page Coder Social logo

How to handle rust panics correctly? about jni-rs HOT 3 OPEN

doums avatar doums commented on June 21, 2024
How to handle rust panics correctly?

from jni-rs.

Comments (3)

argv-minus-one avatar argv-minus-one commented on June 21, 2024

Currently, the correct-ish way to handle panics is to catch_unwind them like you did there, and throw a Java exception. I say “correct-ish” because there are several problems with doing that, and currently there is no reasonable way to solve them.

A while back, I was working on adding a panic handler to this library, which would catch panics and throw Java exceptions as described above, but I ran into the aforementioned problems, so I set it aside until they're solved. I think #478 will solve them, but it hasn't landed yet.

See also #432, a related issue.

from jni-rs.

doums avatar doums commented on June 21, 2024

the correct-ish way to handle panics is to catch_unwind them like you did there

So why SIGABRT is still sent and cross ffi boundary anyway, crashes the whole app? It should be caught by catch_unwind if I understand, right?

EDIT: Reading the doc, probably because Im facing this:

Note that this function might not catch all panics in Rust. A panic in Rust is not always implemented via unwinding, but can be implemented by aborting the process as well. This function only catches unwinding panics, not those that abort the process.

In case of aborting panics there is something I can do to catch the SIGABRT signal ?

from jni-rs.

argv-minus-one avatar argv-minus-one commented on June 21, 2024

If your program aborts at that point, despite the presence of catch_unwind, then the compiler has most likely been configured to abort on panic. The rustc option -C panic=abort sets aborting panics. The Cargo setting panic = "abort" in the applicable [profile.*] section also sets aborting panics. Unwinding panics are the default, so if you want unwinding panics, make sure you don't have anything like that.

But no, there is nothing you can do to catch aborting panics. Catching SIGABRT won't work because, as soon as the signal handler returns, the process will keep trying to abort, over and over, forever. The only way to do anything other than abort upon panic is to use unwinding panics and catch them using catch_unwind. That's the whole purpose of unwinding panics: to be caught and handled instead of simply aborting the process.

Note that panics will also abort upon a double panic, which goes like this:

  1. A panic occurs.
  2. Unwinding begins. Every local variable between the panic location and the nearest catch_unwind is dropped, including running destructors (impl Drop).
  3. One of those destructors panics.

This is unlike Java, where the equivalent (a finally block) can throw an exception while an exception is already being thrown, and this will merely replace the original exception instead of aborting the whole process.

Other ways a panic can abort include:

  • If a panic hook is set, and the panic hook itself panics.
  • If the panic hook chooses to abort.
  • The experimental function always_abort is called. This switches off unwinding panics and causes all future panics to abort. It is not available in stable Rust yet, though.

from jni-rs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.