diff --git a/crates/voelin-gpui/src/app.rs b/crates/voelin-gpui/src/app.rs index 69bab22..5c323e1 100644 --- a/crates/voelin-gpui/src/app.rs +++ b/crates/voelin-gpui/src/app.rs @@ -258,12 +258,16 @@ impl AppState { } /// Leave the servers properly, so no ghost client stays behind until it - /// times out (the window is closing). - pub fn shutdown(&self) { - let Some(engine) = &self.engine else { return }; + /// times out (the window is closing): tell the engine to close every + /// session. Returns how many it told, so that the caller can give the + /// engine time to act on them before the runtime goes (none without an + /// engine). + pub fn shutdown(&self) -> usize { + let Some(engine) = &self.engine else { return 0 }; for id in self.vm.sessions.keys() { engine.send(Command::CloseSession { session: *id as u64 }); } + self.vm.sessions.len() } } @@ -367,6 +371,36 @@ mod tests { assert_eq!(counters.later_jobs.get(), 1); } + /// What closing the window hands the runtime's shutdown: the number of + /// sessions the engine was told to close, which `run` gives time to act + /// on (the Slint front end does the same). The engine ignores a close for + /// a session it never opened, so the sessions here are only the state's. + #[gpui::test] + fn closing_counts_the_sessions_the_engine_was_told_to_close(cx: &mut TestAppContext) { + let runtime = tokio::runtime::Builder::new_multi_thread() + .worker_threads(2) + .enable_all() + .build() + .unwrap(); + let hosted = HostedEngine::start(&runtime); + let (tx, rx) = mpsc::unbounded_channel(); + let app = + cx.new(|cx| AppState::new(Some(hosted.engine.clone()), rx, Counters::default(), cx)); + assert_eq!(app.read_with(cx, |app, _| app.shutdown()), 0, "no session open yet"); + tx.send(voice(VoiceState::Connected)).unwrap(); + cx.run_until_parked(); + assert_eq!(app.read_with(cx, |app, _| app.shutdown()), 1); + runtime.shutdown_timeout(Duration::from_secs(2)); + } + + #[gpui::test] + fn closing_without_an_engine_tells_nothing(cx: &mut TestAppContext) { + let (app, tx, _) = entity(cx); + tx.send(voice(VoiceState::Connected)).unwrap(); + cx.run_until_parked(); + assert_eq!(app.read_with(cx, |app, _| (app.connections(), app.shutdown())), (1, 0)); + } + /// The whole path with a real engine: a command makes the engine report a /// setting change, which its forwarder hands to the window's channel and /// the foreground task to the entity. The engine's tasks run on tokio diff --git a/crates/voelin-gpui/src/lib.rs b/crates/voelin-gpui/src/lib.rs index ecbc673..48b077b 100644 --- a/crates/voelin-gpui/src/lib.rs +++ b/crates/voelin-gpui/src/lib.rs @@ -15,7 +15,7 @@ pub mod swatch; pub mod theme; pub mod view; -use std::cell::RefCell; +use std::cell::{Cell, RefCell}; use std::rc::Rc; use std::time::Duration; @@ -31,6 +31,11 @@ use crate::meter::Counters; use crate::theme::{Choice, VoelinTheme}; use crate::view::MainView; +/// How long the engine gets, after the window closed, to act on the +/// `CloseSession` commands the close sent (the Slint front end waits as long): +/// its tasks run on the runtime, and shutting the runtime down drops them. +const CLOSE_GRACE: Duration = Duration::from_millis(500); + /// What the log gets without `VOELIN_LOG` or `RUST_LOG`: warnings of every /// crate, this front end's information, and what GPUI says about the display /// and the GPU adapter it picked (which device a run used). Two warnings are @@ -70,12 +75,14 @@ fn window_options(switches: &Switches, cx: &App) -> WindowOptions { } /// Open the main window on the root state; closing it leaves the servers and -/// ends the app. +/// ends the app. `closed_sessions` is set to how many sessions the close told +/// the engine to close, which [`run`] needs after GPUI's loop has ended. fn open_main_window( cx: &mut App, switches: &Switches, engine: HostedEngine, counters: Counters, + closed_sessions: Rc>, ) -> Result<()> { // Without a display server GPUI falls back to a headless platform, which // opens windows that draw nothing and runs until it is stopped. @@ -101,7 +108,7 @@ fn open_main_window( window.on_window_should_close(cx, { let app = app.clone(); move |_, cx| { - app.read(cx).shutdown(); + closed_sessions.set(app.read(cx).shutdown()); cx.quit(); true } @@ -133,8 +140,10 @@ pub fn run() -> Result<()> { .context("cannot start the engine's runtime")?; let hosted = HostedEngine::start(&runtime); let failure: Rc>> = Rc::default(); + let closed_sessions: Rc> = Rc::default(); gpui_kit::application().with_assets(Assets).run({ let failure = failure.clone(); + let closed_sessions = closed_sessions.clone(); move |cx| { gpui_kit::init(cx); fonts::load(cx); @@ -146,7 +155,7 @@ pub fn run() -> Result<()> { cx.window_appearance(), switches.font_scale.unwrap_or(1.0), ); - if let Err(e) = open_main_window(cx, &switches, hosted, counters) { + if let Err(e) = open_main_window(cx, &switches, hosted, counters, closed_sessions) { *failure.borrow_mut() = Some(e); // Not `cx.quit()` here: GPUI's Linux platform runs this callback // before it starts its event loop, and the loop forgets a stop @@ -157,6 +166,14 @@ pub fn run() -> Result<()> { } } }); + // The close told the engine to close the sessions; give its tasks time to + // send that before the runtime drops them (`shutdown_timeout` waits for + // blocking tasks only), as the Slint front end does, or ghost clients stay + // 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)); + } runtime.shutdown_timeout(Duration::from_secs(2)); match failure.take() { Some(e) => Err(e), diff --git a/docs/ui.md b/docs/ui.md index b5b2c6c..4c89a4a 100644 --- a/docs/ui.md +++ b/docs/ui.md @@ -968,7 +968,9 @@ crates/voelin-gpui/ build.rs the client's 147 Lucide icons as a table of include_bytes! src/ main.rs lib.rs `run()`: logging, the engine's runtime, the window; closing it - tells the engine to close the sessions and ends the app + tells the engine to close the sessions, ends the app and, when + there were sessions, waits 500 ms for the engine to send the + closes before the runtime goes (as the Slint front end does) engine.rs HostedEngine: Engine::start on a tokio runtime, its events forwarded into a channel the window reads app.rs AppState, the root Entity: the voelin-ui-vm state and Social,