From 78bb95879746db917456fa9428ed44748e2ebbae Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Thu, 18 Jan 2024 18:20:42 +0100 Subject: [PATCH] Fix rare early exit condition because of async race --- src/thread_runtime.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/thread_runtime.rs b/src/thread_runtime.rs index 2b2e6ac..f8edfe1 100644 --- a/src/thread_runtime.rs +++ b/src/thread_runtime.rs @@ -3,7 +3,7 @@ use std::{collections::VecDeque, rc::Rc}; use mlua::prelude::*; use smol::{ channel::{Receiver, Sender}, - future::{race, yield_now}, + future::{yield_now, FutureExt}, lock::Mutex, stream::StreamExt, *, @@ -127,17 +127,16 @@ impl ThreadRuntime { // executor forward, until all lua threads finish let fut = async { loop { - race( - // Wait for next futures step... - async { + // Wait for a new thread to arrive __or__ next futures step, prioritizing + // new threads, so we don't accidentally exit when there is more work to do + self.rx + .recv() + .or(async { lua_exec.tick().await; - }, - // ...or for a new thread to arrive - async { - self.rx.recv().await.ok(); - }, - ) - .await; + Ok(()) + }) + .await + .ok(); // If a new thread was spawned onto queue, we // must drain it and schedule on the executor