mirror of
https://github.com/lune-org/mlua-luau-scheduler.git
synced 2025-04-10 21:40:55 +01:00
Fix unwrap on cancelled threads
This commit is contained in:
parent
1aba727ce6
commit
c2cf9da5cd
2 changed files with 23 additions and 14 deletions
|
@ -326,12 +326,11 @@ impl<'lua> Runtime<'lua> {
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
// Spawn it on the executor and store the result when done
|
// Create our future which will run the thread and store its final result
|
||||||
local_exec
|
let fut = async move {
|
||||||
.spawn(async move {
|
if id_tracked {
|
||||||
if id_tracked {
|
// Run until yield and check if we got a final result
|
||||||
// Run until yield and check if we got a final result
|
if let Some(res) = run_until_yield(thread.clone(), args).await {
|
||||||
let res = run_until_yield(thread.clone(), args).await;
|
|
||||||
if let Err(e) = res.as_ref() {
|
if let Err(e) = res.as_ref() {
|
||||||
self.error_callback.call(e);
|
self.error_callback.call(e);
|
||||||
}
|
}
|
||||||
|
@ -339,15 +338,18 @@ impl<'lua> Runtime<'lua> {
|
||||||
let thread_res = ThreadResult::new(res, self.lua);
|
let thread_res = ThreadResult::new(res, self.lua);
|
||||||
result_map_inner.unwrap().insert(id, thread_res);
|
result_map_inner.unwrap().insert(id, thread_res);
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
// Just run until yield
|
} else {
|
||||||
let res = run_until_yield(thread, args).await;
|
// Just run until yield
|
||||||
|
if let Some(res) = run_until_yield(thread, args).await {
|
||||||
if let Err(e) = res.as_ref() {
|
if let Err(e) = res.as_ref() {
|
||||||
self.error_callback.call(e);
|
self.error_callback.call(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
.detach();
|
};
|
||||||
|
// Spawn it on the executor
|
||||||
|
local_exec.spawn(fut).detach();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
13
lib/util.rs
13
lib/util.rs
|
@ -4,12 +4,14 @@ use mlua::prelude::*;
|
||||||
/**
|
/**
|
||||||
Runs a Lua thread until it manually yields (using coroutine.yield), errors, or completes.
|
Runs a Lua thread until it manually yields (using coroutine.yield), errors, or completes.
|
||||||
|
|
||||||
Returns the values yielded by the thread, or the error that caused it to stop.
|
May return `None` if the thread was cancelled.
|
||||||
|
|
||||||
|
Otherwise returns the values yielded by the thread, or the error that caused it to stop.
|
||||||
*/
|
*/
|
||||||
pub(crate) async fn run_until_yield<'lua>(
|
pub(crate) async fn run_until_yield<'lua>(
|
||||||
thread: LuaThread<'lua>,
|
thread: LuaThread<'lua>,
|
||||||
args: LuaMultiValue<'lua>,
|
args: LuaMultiValue<'lua>,
|
||||||
) -> LuaResult<LuaMultiValue<'lua>> {
|
) -> Option<LuaResult<LuaMultiValue<'lua>>> {
|
||||||
let mut stream = thread.into_async(args);
|
let mut stream = thread.into_async(args);
|
||||||
/*
|
/*
|
||||||
NOTE: It is very important that we drop the thread/stream as
|
NOTE: It is very important that we drop the thread/stream as
|
||||||
|
@ -17,8 +19,13 @@ pub(crate) async fn run_until_yield<'lua>(
|
||||||
and detached tasks will not drop until the executor does
|
and detached tasks will not drop until the executor does
|
||||||
|
|
||||||
https://github.com/smol-rs/smol/issues/294
|
https://github.com/smol-rs/smol/issues/294
|
||||||
|
|
||||||
|
We also do not unwrap here since returning `None` is expected behavior for cancellation.
|
||||||
|
|
||||||
|
Even though we are converting into a stream, and then immediately running it,
|
||||||
|
the future may still be cancelled before it is polled, which gives us None.
|
||||||
*/
|
*/
|
||||||
stream.next().await.unwrap()
|
stream.next().await
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Reference in a new issue