feat: quiesce the VM and unmount windows before native application exit

This commit is contained in:
2026-10-24 01:01:22 +08:00
parent 864e57ce1f
commit 0ad4555922
6 changed files with 383 additions and 26 deletions
+60 -3
View File
@@ -27,6 +27,7 @@ use crate::{
pub enum AppEvent {
RuntimeFailed { message: String },
DocumentUpdated,
ShutdownComplete { error: Option<String> },
ConsoleFailed { message: String, source: String },
}
@@ -40,6 +41,8 @@ struct TabScrollHover {
pub struct TesseraApp {
resource: u64,
control: crate::api::RuntimeControl,
exit_requested: bool,
native_session: u64,
native_error: Option<String>,
children: std::collections::BTreeMap<u64, TesseraApp>,
@@ -72,10 +75,13 @@ impl TesseraApp {
namespace: Arc<RwLock<NamespaceState>>,
activity: Activity,
proxy: EventLoopProxy<AppEvent>,
control: crate::api::RuntimeControl,
) -> anyhow::Result<Self> {
let event_client = crate::events::Client::new(proxy.clone())?;
Ok(Self {
resource: crate::window::MAIN_WINDOW,
control,
exit_requested: false,
native_session: 0,
native_error: None,
children: std::collections::BTreeMap::new(),
@@ -153,7 +159,12 @@ impl TesseraApp {
submission,
..NamespaceState::default()
}));
match Self::new(namespace, self.activity.clone(), self.proxy.clone()) {
match Self::new(
namespace,
self.activity.clone(),
self.proxy.clone(),
self.control.clone(),
) {
Ok(mut child) => {
child.resource = id;
child.native_session = session;
@@ -188,7 +199,7 @@ impl TesseraApp {
let id = envelope.command.window;
if matches!(envelope.command.action, crate::window::Action::Close) {
if id == crate::window::MAIN_WINDOW {
event_loop.exit();
self.request_exit();
} else {
self.children.remove(&id);
}
@@ -993,6 +1004,23 @@ impl TesseraApp {
Ok(())
}
fn request_exit(&mut self) {
if self.exit_requested {
return;
}
self.clear_tab_scroll();
self.guest_pressed = None;
self.guest_key = None;
if self.control.request_shutdown() {
self.exit_requested = true;
} else {
self.native_error = Some("VM worker unavailable during shutdown".into());
let _ = self.proxy.send_event(AppEvent::ShutdownComplete {
error: self.native_error.clone(),
});
}
}
pub fn exit_result(&self) -> anyhow::Result<()> {
if let Some(error) = &self.native_error {
anyhow::bail!("{error}");
@@ -1079,6 +1107,23 @@ impl TesseraApp {
}
impl ApplicationHandler<AppEvent> for TesseraApp {
fn exiting(&mut self, _: &ActiveEventLoop) {
// OS termination can bypass CloseRequested. The loop is already ending;
// use a bounded fallback rather than silently dropping every callback.
if !self.control.request_shutdown() {
self.native_error = Some("VM worker unavailable during shutdown".into());
return;
}
match self.control.wait_shutdown(Duration::from_secs(5)) {
Some(outcome) => {
if let Some(error) = outcome.error {
self.native_error = Some(error);
}
}
None => self.native_error = Some("VM shutdown did not finish within 5 seconds".into()),
}
}
fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) {
event_loop.set_control_flow(ControlFlow::Wait);
self.tick_tab_scroll(event_loop);
@@ -1097,7 +1142,16 @@ impl ApplicationHandler<AppEvent> for TesseraApp {
}
fn user_event(&mut self, event_loop: &ActiveEventLoop, event: AppEvent) {
if self.control.is_exiting() && !matches!(&event, AppEvent::ShutdownComplete { .. }) {
return;
}
match event {
AppEvent::ShutdownComplete { error } => {
if let Some(error) = error {
self.native_error = Some(error);
}
event_loop.exit();
}
AppEvent::RuntimeFailed { message } => {
self.developer_tools.input_error = message;
self.developer_tools.open = true;
@@ -1124,6 +1178,9 @@ impl ApplicationHandler<AppEvent> for TesseraApp {
window_id: WindowId,
event: WindowEvent,
) {
if self.control.is_exiting() {
return;
}
self.native_tab_drag(window_id, &event);
let Some(window) = self.window.as_ref().cloned() else {
return;
@@ -1144,7 +1201,7 @@ impl ApplicationHandler<AppEvent> for TesseraApp {
match event {
WindowEvent::CloseRequested => {
if self.resource == crate::window::MAIN_WINDOW {
event_loop.exit();
self.request_exit();
} else {
let input = self
.namespace