4 ms·
NIFs run without most (all?) of the safety features the erlang VM provides, such as memory safety and scheduling. If the NIF crashes, the entire app (aka, VM) c
by latch 6y ago
NIFs run without most (all?) of the safety features the erlang VM provides, such as memory safety and scheduling. If the NIF crashes, the entire app (aka, VM) crashes.
This is anathema to some...i dunno..philosophies? expectations? of Erlang/Elixir.
NIFs written in Rust (via Rustler) should never be able to crash the VM. Unlike NIFs written in C.
Rustler brings other quality of life improvements, like easier interop (decoding/encoding values to and from) and managing the cleanup of objects that live in Rust but are referenced by Elixir.
- pjmlp 6y agoSo what happens to the VM when an index out of bounds happens in Rustler? Is there a panic handler that prevents a crash and returns a magic value that makes sense to the caller?
- filmor 6y agoThe called code is made unwind-safe at the FFI boundary using `std::panic::catch_unwind`. A panic will be converted to an Erlang exception.
- pjmlp 6y agoExcept it doesn't work always, > Note that this function may 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.
- c-cube 6y agoIf you compile a NIF you probably will not go out of your way to write "panic = abort" in your project. Unwinding is the default behavior.
- pjmlp 6y agoIt depends on the dependencies.
- bluejekyll 6y agoFrom the docs filmor linked to, if a library sets panic = unwind and a user sets that to abort, “If any of your users choose to abort, they'll get a compile-time failure.”
- filmor 6y agoIt works whenever it's configured to unwind, which is the default. Apparently one can force this by adding a few lines in the rustler Cargo.toml (https://doc.rust-lang.org/edition-guide/rust-2018/error-handling-and-panics/controlling-panics-with-std-panic.html https://doc.rust-lang.org/edition-guide/rust-2018/error-hand...), I'll create a PR.
- latch 6y agoWe use rustler only as a thin wrapper to existing crates (like, for hdrhistograms). I'm far from a Rust expert, but I believe it uses std::panic::catch_unwind
- pjmlp 6y ago> Note that this function may 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.