From 8ab0ccf11cfbc4bd6e052fc1ff49e73ee4bd1f4c Mon Sep 17 00:00:00 2001 From: Alex Orlenko Date: Sun, 4 Jun 2023 02:31:29 +0100 Subject: Don't keep poll function in environment globals when polling async functions. This is redundant after deprecating scoped async. Closes #281 --- src/lua.rs | 5 ++--- tests/async.rs | 24 +++++++++++++++++++++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/lua.rs b/src/lua.rs index d8bb215..06bc5a3 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -2845,11 +2845,10 @@ impl Lua { LightUserData(&ASYNC_POLL_PENDING as *const u8 as *mut c_void) })?; - // We set `poll` variable in the env table to be able to destroy upvalues self.load( r#" - poll = get_poll(...) - local poll, pending, yield, unpack = poll, pending, yield, unpack + local poll = get_poll(...) + local pending, yield, unpack = pending, yield, unpack while true do local ready, res, nres = poll() if ready then diff --git a/tests/async.rs b/tests/async.rs index bc1448f..e49c8c2 100644 --- a/tests/async.rs +++ b/tests/async.rs @@ -1,7 +1,7 @@ #![cfg(feature = "async")] use std::sync::atomic::{AtomicU64, Ordering}; -use std::sync::Arc; +use std::sync::{Arc, Mutex}; use std::time::Duration; use futures_timer::Delay; @@ -502,3 +502,25 @@ async fn test_owned_async_call() -> Result<()> { Ok(()) } + +#[tokio::test] +async fn test_async_terminate() -> Result<()> { + let lua = Lua::new(); + + let mutex = Arc::new(Mutex::new(0u32)); + let mutex2 = mutex.clone(); + let func = lua.create_async_function(move |_, ()| { + let mutex = mutex2.clone(); + async move { + let _guard = mutex.lock(); + Delay::new(Duration::from_millis(100)).await; + Ok(()) + } + })?; + + let _ = tokio::time::timeout(Duration::from_millis(30), func.call_async::<_, ()>(())).await; + lua.gc_collect()?; + assert!(mutex.try_lock().is_ok()); + + Ok(()) +} -- cgit v1.2.3