Closed cppcoffee closed 5 months ago
We'll split this into three PRs to resolve this issue.
The following are the three PRs, each enabling state_main
, LightUserData
, and Thread
to support Send/Sync
.
https://github.com/mlua-rs/mlua/pull/412 https://github.com/mlua-rs/mlua/pull/414 https://github.com/mlua-rs/mlua/pull/415
It's not a bug, it's by design.
I'm afraid the PRs you raised are unsound. Just an example how to cause a segfault (using your changes):
let lua = Lua::new();
std::thread::scope(|s| {
s.spawn(|| {
loop {
lua.load("return 1+1").exec().unwrap();
}
});
s.spawn(|| {
loop {
lua.load("return 1+1").exec().unwrap();
}
});
});
Lua is not thread safe and applying Sync
trait without significantly rewriting mlua core is not possible.
I hope next version will support proper sync
feature with all protection measure in place, but it's not trivial task at all.
Would it be better to introduce a LuaGuard
and use mutex locks to execute lua?
The bug demo is as follows:
Cargo.toml:
src/main.rs: