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
+87 -15
View File
@@ -999,6 +999,30 @@ impl ReplSession {
result
}
fn cancel_pending_refresh(&mut self) -> Result<()> {
if let Some(mut work) = self.refresh.take() {
if let Some(paint) = work.paint.take() {
self.bridge.cancel_paint_frame(
&mut self.candidate.vm,
paint.window,
*paint.work,
)?;
}
if let Some(pending) = work.pending.take() {
self.bridge
.cancel_layout_frame(&mut self.candidate.vm, *pending)?;
}
}
Ok(())
}
pub fn shutdown(&mut self) -> Result<(usize, usize, usize)> {
self.cancel_pending_refresh()?;
let callbacks = !self.faulted;
self.faulted = true;
self.bridge.shutdown(&mut self.candidate.vm, callbacks)
}
pub fn retire_window(&mut self, id: u64) -> Result<()> {
self.require_idle_refresh()?;
// This path is exclusively host cleanup after the domain has stopped.
@@ -1083,21 +1107,7 @@ impl ReplSession {
impl Drop for ReplSession {
fn drop(&mut self) {
if let Some(mut work) = self.refresh.take() {
if let Some(paint) = work.paint.take() {
let _ = self.bridge.cancel_paint_frame(
&mut self.candidate.vm,
paint.window,
*paint.work,
);
}
if let Some(pending) = work.pending.take() {
// Close the read-only call/checkpoint before Candidate roots.
let _ = self
.bridge
.cancel_layout_frame(&mut self.candidate.vm, *pending);
}
}
let _ = self.cancel_pending_refresh();
}
}
@@ -6774,4 +6784,66 @@ function main(): View { let child = ui.text("shared") return wrap(child) }"#;
Some(deleted + 1)
);
}
const EXIT_SOURCE: &str = r#"
class ExitText: TextBlock {
log: Object
function onUnmount(self: ExitText, mount: Mount): Unit {
self.log.increment()
}
}
log = ui.state(0)
text = ExitText("exit",1,log)
page = Page()
page.render(text)
second = Window()
other = Page()
other.render(text)
other.moveToWindow(second)
pending = ui.sleep(ui,"pending",30000)
"#;
fn exit_log(session: &ReplSession) -> Value {
let globals = session.candidate.globals.unwrap();
let Some(Value::Object(log)) = session
.candidate
.vm
.dict_get(globals, &Value::Text("log".into()))
.unwrap()
else {
panic!("log")
};
session.candidate.vm.get_attr(log, "value").unwrap()
}
#[test]
fn shutdown_unmounts_all_windows_once_and_cancels_tasks() {
let (mut session, _) = ReplSession::load(EXIT_SOURCE, "app.main", 701, 1).unwrap();
assert_eq!(exit_log(&session), Value::Number(0_i64.into()));
let (windows, mounts, tasks) = session.shutdown().unwrap();
assert_eq!(windows, 2);
assert!(mounts >= 2);
assert!(tasks >= 1);
assert_eq!(exit_log(&session), Value::Number(2_i64.into()));
session.shutdown().unwrap();
assert_eq!(exit_log(&session), Value::Number(2_i64.into()));
assert!(session.execute("42", "app.main").is_err());
}
#[test]
fn shutdown_stopped_runtime_never_reenters_source_hooks() {
let (mut session, _) = ReplSession::load(EXIT_SOURCE, "app.main", 702, 1).unwrap();
session.faulted = true;
session.shutdown().unwrap();
assert_eq!(exit_log(&session), Value::Number(0_i64.into()));
}
#[test]
fn shutdown_hooks_cannot_create_new_background_work() {
let source = EXIT_SOURCE.replace("self.log.increment()", "ui.sleep(ui,\"late\",30000)");
let (mut session, _) = ReplSession::load(&source, "app.main", 703, 1).unwrap();
let error = session.shutdown().unwrap_err();
assert!(format!("{error:#}").contains("runtime is exiting; new tasks are forbidden"));
assert!(session.is_faulted());
}
}