with panic = "abort", any failure in a userdata function or method crashes the process even if it wasn't a panic
I don't know if this is a bug or a feature request to be honest. The minimal repro below fails at the unwrap in main if panic = "abort" is not specified. If panic = "abort", though, it panics inside the crate across the Lua C API rather than bubbling the Result up to my code. This means that even if I go out of the way to write functions that validate and never panic, I can't use panic = "abort" because of that unwanted conversion.
Maybe I'm being dumb, but at the very least this doesn't seem documented. In my case though (game dev, backend stuff, etc) I really don't like unwind because it causes so-called grey failures. Maybe a panic propagates to all threads eventually, but that might not be for a long time, and you've just got this period where half the process died but the other half is still going--potentially even the bits that report that all is well to monitoring! So I consider it best practice to always use abort.
Anyway:
use mlua::{Lua, UserData, UserDataMethods};
struct Data(i32);
impl UserData for Data {
fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
methods.add_method_mut("test", |_, this, _: ()| {
return Result::<(), _>::Err(mlua::Error::runtime("Failed!"));
//this.0 = 1;
//Ok(())
});
}
}
fn main() {
let lua = Lua::new();
let mut data = Data(0);
lua.scope(|scope| {
let ud = scope.create_userdata_ref_mut(&mut data)?;
lua.globals().set("d", ud)?;
lua.load("d:test()").eval::<()>().unwrap();
Ok(())
})
.unwrap();
}
Unfortunately it's hard to guess your OS, build flags, Lua version and feature flags.
On macOS M3 with vendored Lua 5.1 - 5.4 everything works as expected. Luau uses C++ exceptions for error handling and relies on unwind information. LuaJIT also uses unwinding by design for errors handing and it's expected to abort when this mechanism is disabled.
The most reliable option for panic=abort is to use Lua 5.4.
Note that if you're on Windows, then Lua 5.4 is not a viable option either, see https://github.com/mlua-rs/mlua/issues/520
I was moving quickly and figured this was (1) universal and (2) a bug in mlua itself. I am indeed on Windows and #520 sure looks like it's the issue. That's super frustrating. TIL I guess. You can close if you want but a quick docs update somewhere might be nice for whoever comes after me, and if there's a way to get Lua to not hate this configuration I wouldn't complain.
(off to explore whether I can abort in panic handlers myself or something. Yay!)