diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 2662b73..d3c8f19 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -118,12 +118,14 @@ jobs: with: toolchain: ${{ matrix.rust }} target: ${{ matrix.target }} - - uses: Swatinem/rust-cache@v1 + - uses: Swatinem/rust-cache@v2 - name: Run ${{ matrix.lua }} tests run: | cargo test --features "${{ matrix.lua }},vendored" cargo test --features "${{ matrix.lua }},vendored,async,send,serialize,macros,parking_lot,unstable" shell: bash + env: + RUSTFLAGS: --cfg mlua_test - name: Run compile tests (macos lua54) if: ${{ matrix.os == 'macos-latest' && matrix.lua == 'lua54' }} run: | @@ -149,12 +151,13 @@ jobs: with: toolchain: ${{ matrix.rust }} target: ${{ matrix.target }} - - uses: Swatinem/rust-cache@v1 + - uses: Swatinem/rust-cache@v2 - name: Run ${{ matrix.lua }} tests with address sanitizer run: | - RUSTFLAGS="-Z sanitizer=address" \ cargo test --tests --features "${{ matrix.lua }},vendored,async,send,serialize,macros,parking_lot,unstable" --target x86_64-unknown-linux-gnu -- --skip test_too_many_recursions shell: bash + env: + RUSTFLAGS: --cfg mlua_test -Z sanitizer=address test_modules: name: Test modules @@ -176,7 +179,7 @@ jobs: with: toolchain: ${{ matrix.rust }} target: ${{ matrix.target }} - - uses: Swatinem/rust-cache@v1 + - uses: Swatinem/rust-cache@v2 - name: Run ${{ matrix.lua }} module tests run: | (cd tests/module && cargo build --release --features "${{ matrix.lua }}") diff --git a/src/lua.rs b/src/lua.rs index e5decdf..ec2df98 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -247,6 +247,8 @@ impl Drop for LuaInner { { (*ffi::lua_callbacks(self.state())).userdata = ptr::null_mut(); } + // This is an internal assertion used in integration tests + #[cfg(mlua_test)] mlua_debug_assert!( ffi::lua_gettop(extra.ref_thread) == extra.ref_stack_top && extra.ref_stack_top as usize == extra.ref_free.len(), diff --git a/tests/async.rs b/tests/async.rs index 563865a..9ba06da 100644 --- a/tests/async.rs +++ b/tests/async.rs @@ -9,7 +9,7 @@ use futures_util::stream::TryStreamExt; use mlua::{ AnyUserDataExt, Error, Function, Lua, LuaOptions, Result, StdLib, Table, TableExt, UserData, - UserDataMethods, + UserDataMethods, Value, }; #[tokio::test] @@ -270,6 +270,28 @@ async fn test_async_thread() -> Result<()> { Ok(()) } +#[test] +fn test_async_thread_leak() -> Result<()> { + let lua = Lua::new(); + + let f = lua.create_async_function(move |_lua, v: Value| async move { + tokio::task::yield_now().await; + drop(v); + Ok(()) + })?; + + let thread = lua.create_thread(f)?; + // After first resume, `v: Value` is captured in the coroutine + thread.resume::<_, ()>("abc").unwrap(); + drop(thread); + + // Without running garbage collection, the captured `v` would trigger "reference leak detected" error + // with `cfg(mlua_test)` + lua.gc_collect()?; + + Ok(()) +} + #[tokio::test] async fn test_async_table() -> Result<()> { let options = LuaOptions::new().thread_pool_size(4);