voelin-gpui: closing the window gives the engine time to send the session closes
The window's close sends CloseSession for every session (AppState::shutdown) and ended the app straight into runtime.shutdown_timeout(2 s), which waits for blocking tasks only and drops the engine's async tasks: the commands could go with the runtime and leave ghost clients on the servers until they time out. voelin-ui sleeps 500 ms in that case before shutting its runtime down (app.rs after ui.run()); run() does the same now. shutdown() returns how many sessions it told the engine to close, the close handler keeps that number for run() (the App and its entities are gone once GPUI's loop has returned), and run() waits CLOSE_GRACE only when it is not zero. A comment in run() tells the slices that add another way to end the app to call shutdown() too. The GPUI window cannot open a session in this slice, so the wait is not seen end to end; the tests pin the count: with an engine a state holding a session answers 1 and a state with none 0, without an engine 0. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a4694e944b
commit
efc7db57a3
3 files changed
+61
-8
No files matched your search
@@ -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
|
||||
|
||||
Reference in new issue
Block a user