Fix rare early exit condition because of async race

This commit is contained in:
Filip Tibell 2024-01-18 18:20:42 +01:00
parent 13d1f13c88
commit 78bb958797
No known key found for this signature in database

View file

@ -3,7 +3,7 @@ use std::{collections::VecDeque, rc::Rc};
use mlua::prelude::*; use mlua::prelude::*;
use smol::{ use smol::{
channel::{Receiver, Sender}, channel::{Receiver, Sender},
future::{race, yield_now}, future::{yield_now, FutureExt},
lock::Mutex, lock::Mutex,
stream::StreamExt, stream::StreamExt,
*, *,
@ -127,17 +127,16 @@ impl ThreadRuntime {
// executor forward, until all lua threads finish // executor forward, until all lua threads finish
let fut = async { let fut = async {
loop { loop {
race( // Wait for a new thread to arrive __or__ next futures step, prioritizing
// Wait for next futures step... // new threads, so we don't accidentally exit when there is more work to do
async { self.rx
.recv()
.or(async {
lua_exec.tick().await; lua_exec.tick().await;
}, Ok(())
// ...or for a new thread to arrive })
async { .await
self.rx.recv().await.ok(); .ok();
},
)
.await;
// If a new thread was spawned onto queue, we // If a new thread was spawned onto queue, we
// must drain it and schedule on the executor // must drain it and schedule on the executor