Skip to content

with panic = "abort", any failure in a userdata function or method crashes the process even if it wasn't a panic #628

@ahicks92

Description

@ahicks92

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();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions