Skip to content

Commit

Permalink
fix the scheduler running forever on specific async tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
bjcscat committed Jul 16, 2024
1 parent bc0e877 commit df4ba68
Showing 1 changed file with 21 additions and 16 deletions.
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

0 comments on commit df4ba68

Please sign in to comment.