Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mlua 0.10.0 panicking randomly with "internal error: 1 too many stack values popped" #477

Closed
Tracked by #1842
sxyazi opened this issue Oct 29, 2024 · 2 comments
Closed
Tracked by #1842

Comments

@sxyazi
Copy link

sxyazi commented Oct 29, 2024

Hi! I'm upgrading the mlua version of Yazi from 0.9.9 to 0.10.0. It compiles fine now, but I'm getting random panics at runtime:

The application panicked (crashed).
  mlua internal error: 1 too many stack values popped (this is a bug, please file an issue)
in rust:library/core/src/ptr/mod.rs, line 574
thread: tokio-runtime-worker

Here's the stack trace:

  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/mlua-0.10.0/src/macros.rs", line 27, in <mlua::util::StackGuard as core::ops::drop::Drop>::drop
    panic!(bug_msg!($msg), $($arg),+)
  File "rust:library/core/src/ptr/mod.rs", line 574, in core::ptr::drop_in_place<mlua::util::StackGuard>
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/mlua-0.10.0/src/state/raw.rs", line 987, in mlua::state::raw::RawLua::push_userdata_metatable
    }
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/mlua-0.10.0/src/state/raw.rs", line 818, in mlua::state::raw::RawLua::create_userdata_metatable
    self.push_userdata_metatable(registry)?;
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/mlua-0.10.0/src/state/raw.rs", line 758, in mlua::state::raw::RawLua::make_userdata::{{closure}}
    self.create_userdata_metatable(registry)
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/mlua-0.10.0/src/state/raw.rs", line 790, in mlua::state::raw::RawLua::make_userdata_with_metatable
    ffi::lua_rawgeti(state, ffi::LUA_REGISTRYINDEX, get_metatable_id()?);
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/mlua-0.10.0/src/state/raw.rs", line 747, in mlua::state::raw::RawLua::make_userdata
    self.make_userdata_with_metatable(data, || {
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/mlua-0.10.0/src/state.rs", line 1255, in mlua::state::Lua::create_userdata
    unsafe { self.lock().make_userdata(UserDataStorage::new(data)) }
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/mlua-0.10.0/src/conversion.rs", line 233, in mlua::conversion::<impl mlua::traits::IntoLua for T>::into_lua
    Ok(Value::UserData(lua.create_userdata(self)?))
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/mlua-0.10.0/src/traits.rs", line 28, in mlua::traits::IntoLua::push_into_stack
    lua.push_value(&self.into_lua(lua.lua())?)
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/mlua-0.10.0/src/table.rs", line 256, in mlua::table::Table::raw_set
    value.push_into_stack(&lua)?;
  File "/Users/alice/Desktop/yazi/yazi-plugin/src/isolate/peek.rs", line 49, in yazi_plugin::isolate::peek::peek::{{closure}}::{{closure}}
    plugin.raw_set("window", Window::default())?;
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.41.0/src/macros/select.rs", line 557, in yazi_plugin::isolate::peek::peek::{{closure}}::{{closure}}::{{closure}}
    let out = match Future::poll(fut, cx) {
  File "rust:library/core/src/future/poll_fn.rs", line 151, in <core::future::poll_fn::PollFn<F> as core::future::future::Future>::poll
  File "/Users/alice/Desktop/yazi/yazi-plugin/src/isolate/peek.rs", line 55, in yazi_plugin::isolate::peek::peek::{{closure}}::{{closure}}
    select! {
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.41.0/src/runtime/park.rs", line 281, in tokio::runtime::park::CachedParkThread::block_on::{{closure}}
    if let Ready(v) = crate::runtime::coop::budget(|| f.as_mut().poll(&mut cx)) {
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.41.0/src/runtime/coop.rs", line 107, in tokio::runtime::coop::with_budget
    f()
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.41.0/src/runtime/coop.rs", line 73, in tokio::runtime::coop::budget
    with_budget(Budget::initial(), f)
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.41.0/src/runtime/park.rs", line 281, in tokio::runtime::park::CachedParkThread::block_on
    if let Ready(v) = crate::runtime::coop::budget(|| f.as_mut().poll(&mut cx)) {
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.41.0/src/runtime/context/blocking.rs", line 66, in tokio::runtime::context::blocking::BlockingRegionGuard::block_on
    park.block_on(f)
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.41.0/src/runtime/handle.rs", line 327, in tokio::runtime::handle::Handle::block_on_inner::{{closure}}
    blocking.block_on(future).expect("failed to park thread")
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.41.0/src/runtime/context/runtime.rs", line 65, in tokio::runtime::context::runtime::enter_runtime
    return f(&mut guard.blocking);
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.41.0/src/runtime/handle.rs", line 326, in tokio::runtime::handle::Handle::block_on_inner
    context::enter_runtime(&self.inner, true, |blocking| {
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.41.0/src/runtime/handle.rs", line 305, in tokio::runtime::handle::Handle::block_on
    self.block_on_inner(future, SpawnMeta::new_unnamed(fut_size))
  File "/Users/alice/Desktop/yazi/yazi-plugin/src/isolate/peek.rs", line 54, in yazi_plugin::isolate::peek::peek::{{closure}}
    let result = Handle::current().block_on(async {
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.41.0/src/runtime/blocking/task.rs", line 42, in <tokio::runtime::blocking::task::BlockingTask<T> as core::future::future::Future>::poll
    Poll::Ready(func())
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.41.0/src/runtime/task/core.rs", line 331, in tokio::runtime::task::core::Core<T,S>::poll::{{closure}}
    future.poll(&mut cx)
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.41.0/src/loom/std/unsafe_cell.rs", line 16, in tokio::loom::std::unsafe_cell::UnsafeCell<T>::with_mut
    f(self.0.get())
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.41.0/src/runtime/task/core.rs", line 320, in tokio::runtime::task::core::Core<T,S>::poll
    self.stage.stage.with_mut(|ptr| {
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.41.0/src/runtime/task/harness.rs", line 499, in tokio::runtime::task::harness::poll_future::{{closure}}
    let res = guard.core.poll(cx);
  File "rust:library/core/src/panic/unwind_safe.rs", line 272, in <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
  File "rust:library/std/src/panicking.rs", line 557, in std::panicking::try::do_call
  File "<unknown>", line 0, in ___rust_try
  File "rust:library/std/src/panicking.rs", line 520, in std::panicking::try
  File "rust:library/std/src/panic.rs", line 348, in std::panic::catch_unwind
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.41.0/src/runtime/task/harness.rs", line 487, in tokio::runtime::task::harness::poll_future
    let output = panic::catch_unwind(panic::AssertUnwindSafe(|| {
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.41.0/src/runtime/task/harness.rs", line 209, in tokio::runtime::task::harness::Harness<T,S>::poll_inner
    let res = poll_future(self.core(), cx);
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.41.0/src/runtime/task/harness.rs", line 154, in tokio::runtime::task::harness::Harness<T,S>::poll
    match self.poll_inner() {
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.41.0/src/runtime/task/raw.rs", line 271, in tokio::runtime::task::raw::poll
    harness.poll();
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.41.0/src/runtime/task/raw.rs", line 201, in tokio::runtime::task::raw::RawTask::poll
    unsafe { (vtable.poll)(self.ptr) }
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.41.0/src/runtime/task/mod.rs", line 472, in tokio::runtime::task::UnownedTask<S>::run
    raw.poll();
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.41.0/src/runtime/blocking/pool.rs", line 161, in tokio::runtime::blocking::pool::Task::run
    self.task.run();
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.41.0/src/runtime/blocking/pool.rs", line 511, in tokio::runtime::blocking::pool::Inner::run
    task.run();
  File "/Users/alice/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.41.0/src/runtime/blocking/pool.rs", line 469, in tokio::runtime::blocking::pool::Spawner::spawn_thread::{{closure}}
    rt.inner.blocking_spawner().inner.run(id);
  File "rust:library/std/src/sys/backtrace.rs", line 154, in std::sys::backtrace::__rust_begin_short_backtrace
  File "rust:library/std/src/thread/mod.rs", line 522, in std::thread::Builder::spawn_unchecked_::{{closure}}::{{closure}}
  File "rust:library/core/src/panic/unwind_safe.rs", line 272, in <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
  File "rust:library/std/src/panicking.rs", line 557, in std::panicking::try::do_call
  File "<unknown>", line 0, in ___rust_try
  File "rust:library/std/src/panicking.rs", line 520, in std::panicking::try
  File "rust:library/std/src/panic.rs", line 348, in std::panic::catch_unwind
  File "rust:library/std/src/thread/mod.rs", line 521, in std::thread::Builder::spawn_unchecked_::{{closure}}
  File "rust:library/core/src/ops/function.rs", line 250, in core::ops::function::FnOnce::call_once{{vtable.shim}}
  File "rust:library/alloc/src/boxed.rs", line 2453, in <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once
  File "rust:library/alloc/src/boxed.rs", line 2453, in <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once
  File "rust:library/std/src/sys/pal/unix/thread.rs", line 105, in std::sys::pal::unix::thread::Thread::new::thread_start
  File "<unknown>", line 0, in __pthread_joiner_wake

In my case, I'm using tokio::task::spawn_blocking() to concurrently create multiple Lua 5.4 instances to do some time-consuming operations, and this error occurs when setting userdata for a table:

plugin.raw_set("window", Window::default())?;

The Window struct is a normal structure that implements UserData:

#[derive(Debug, Clone, Copy, FromLua)]
pub struct Window {
	pub rows:   u16,
	pub cols:   u16,
	pub width:  u16,
	pub height: u16,
}

impl UserData for Window {
	fn add_fields<F: mlua::UserDataFields<Self>>(fields: &mut F) {
		fields.add_field_method_get("rows", |_, me| Ok(me.rows));
		fields.add_field_method_get("cols", |_, me| Ok(me.cols));
		fields.add_field_method_get("width", |_, me| Ok(me.width));
		fields.add_field_method_get("height", |_, me| Ok(me.height));
	}
}

This issue comes up really randomly, and I've tried for a long time to create a minimal reproduction for it, but no luck :(

I think it might be related to set_hook(), after I commented out the following code no more panicking again:

lua.set_hook(
  HookTriggers::new().on_calls().on_returns().every_nth_instruction(2000),
  move |_, _| {
    if ct1.is_cancelled() {
      Err("Peek task cancelled".into_lua_err())
    } else {
      Ok(VmState::Continue)
    }
  },
);
khvzak added a commit that referenced this issue Oct 29, 2024
@khvzak
Copy link
Member

khvzak commented Oct 29, 2024

Thanks for the report! I pushed a fix, could you try the latest main branch?

@sxyazi
Copy link
Author

sxyazi commented Oct 30, 2024

I can confirm the main branch fixed it - I've been running it for a while and haven't encountered any more panics. Thank you so much!

@sxyazi sxyazi closed this as completed Oct 30, 2024
sxyazi added a commit to sxyazi/yazi that referenced this issue Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants