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

Fix scheduler loop causing infinitely running process #229

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions crates/mlua-luau-scheduler/src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
thread::panicking,
};

use futures_lite::prelude::*;
use futures_lite::{future::yield_now, prelude::*};
use mlua::prelude::*;

use async_executor::{Executor, LocalExecutor};
Expand Down Expand Up @@ -369,23 +369,28 @@ impl<'lua> Scheduler<'lua> {

// 5
let mut num_processed = 0;
let span_tick = trace_span!("Scheduler::tick");
let fut_tick = async {
local_exec.tick().await;
// NOTE: Try to do as much work as possible instead of just a single tick()
num_processed += 1;
while local_exec.try_tick() {
num_processed += 1;
}
};
// let span_tick = trace_span!("Scheduler::tick");
// let fut_tick = async {
// local_exec.tick().await;
// // NOTE: Try to do as much work as possible instead of just a single tick()
// num_processed += 1;
// while local_exec.try_tick() {
// num_processed += 1;
// }
// };
local_exec
.run(
fut_exit.or(fut_spawn).or(fut_defer).or(fut_futs).or(async {
local_exec.tick().await; // weird but is required to preserve scheduler ordering

if !local_exec.is_empty() {
yield_now().await;
}
}), // .or(fut_tick.instrument(span_tick.or_current())),
)
.await;

// 1 + 2 + 3 + 4 + 5
fut_exit
.or(fut_spawn)
.or(fut_defer)
.or(fut_futs)
.or(fut_tick.instrument(span_tick.or_current()))
.await;

// Check if we should exit
if self.exit.get().is_some() {
Expand Down
18 changes: 13 additions & 5 deletions tests/task/defer.luau
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,29 @@ end)
task.wait()
assert(flag3 == 3, "Defer should run after spawned threads")

-- Delay should be able to be nested
-- Defer should be able to be nested

local flag4: boolean = false
task.delay(0.05, function()
task.defer(function()
local function nested3()
task.delay(0.05, function()
task.defer(function()
task.wait(0.05)
flag4 = true
end)
end
local function nested2()
task.delay(0.05, nested3)
task.defer(function()
task.wait(0.05)
nested3()
end)
end
local function nested1()
task.delay(0.05, nested2)
task.defer(function()
task.wait(0.05)
nested2()
end)
end
task.wait(0.05)
nested1()
end)
task.wait(0.25)
Expand Down
20 changes: 6 additions & 14 deletions tests/task/delay.luau
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,25 @@ assert(flag, "Delay should work with yielding (1)")
task.wait(0.1)
assert(not flag2, "Delay should work with yielding (2)")

-- Defer should be able to be nested
-- Delay should be able to be nested

local flag4: boolean = false
task.defer(function()
task.delay(0.05, function()
local function nested3()
task.defer(function()
task.wait(0.05)
task.delay(0.05, function()
flag4 = true
end)
end
local function nested2()
task.defer(function()
task.wait(0.05)
nested3()
end)
task.delay(0.05, nested3)
end
local function nested1()
task.defer(function()
task.wait(0.05)
nested2()
end)
task.delay(0.05, nested2)
end
task.wait(0.05)
nested1()
end)
task.wait(0.25)
assert(flag4, "Defer should work with nesting")
assert(flag4, "Delay should work with nesting")

-- Varargs should get passed correctly

Expand Down
Loading