diff --git a/crates/voelin-gpui/src/lib.rs b/crates/voelin-gpui/src/lib.rs index 40a6d77..f3e45ea 100644 --- a/crates/voelin-gpui/src/lib.rs +++ b/crates/voelin-gpui/src/lib.rs @@ -172,6 +172,14 @@ fn open_main_window( Ok(()) } +/// Wait `grace` on `runtime`, so that the engine's tasks run (they send the +/// closes of the sessions) before the runtime drops them. The sleep is made +/// inside the runtime: a `Sleep` made outside it, as an argument of +/// `block_on`, panics ("there is no reactor running"). +fn give_the_engine_time(runtime: &tokio::runtime::Runtime, grace: Duration) { + runtime.block_on(async move { tokio::time::sleep(grace).await }); +} + /// Run the app on the current thread until its window closes. pub fn run() -> Result<()> { init_logging(); @@ -228,7 +236,7 @@ pub fn run() -> Result<()> { // on the servers until they time out. Any other way to end the app that // the slices add must call `AppState::shutdown` too. if closed_sessions.get() > 0 { - runtime.block_on(tokio::time::sleep(CLOSE_GRACE)); + give_the_engine_time(&runtime, CLOSE_GRACE); } runtime.shutdown_timeout(Duration::from_secs(2)); match failure.take() { @@ -236,3 +244,21 @@ pub fn run() -> Result<()> { None => Ok(()), } } + +#[cfg(test)] +mod tests { + use super::*; + + /// The grace after a close with open sessions is a wait on the engine's + /// runtime from the GPUI thread, which is no runtime thread: it must not + /// panic, and it must wait. (The first end-to-end close against a server, + /// the first slice that opens a session, found the panic.) + #[test] + fn the_close_grace_waits_on_the_runtime_from_a_thread_that_is_none() { + let runtime = tokio::runtime::Builder::new_multi_thread().enable_all().build().unwrap(); + let started = std::time::Instant::now(); + give_the_engine_time(&runtime, Duration::from_millis(30)); + assert!(started.elapsed() >= Duration::from_millis(30)); + runtime.shutdown_timeout(Duration::from_secs(2)); + } +}