From 6c4be694f7b67db0243f09b5d381c83000521302 Mon Sep 17 00:00:00 2001 From: Codex Bot Date: Fri, 23 Oct 2026 01:22:03 +0800 Subject: [PATCH] fix: guard runtime mutation across parked refresh stages --- README.md | 3 +++ src/README.md | 3 +++ src/api.rs | 3 +++ src/endor_host.rs | 66 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+) diff --git a/README.md b/README.md index 0126c5a..ab9784d 100644 --- a/README.md +++ b/README.md @@ -512,3 +512,6 @@ ReplSession now owns a resumable RefreshWork containing window traversal, pendin Viewport refresh now advances through ReplSession's saved refresh state on worker idle turns. A turn advances one traversal stage or a 256-instruction layout quantum; a pending job shortens the next worker wait to 1 ms. Tasks/window commands and document publication wait for complete refresh. ui.stats().refreshYields reports cooperative idle returns. Mutating requests first drain the existing job, so they cannot run inside its read-only scope. This is serialized input, not live cancellation/rollback or latest-input coalescing. Initial loads, explicit REPL/event refresh and task callbacks still drain synchronously; native Rust measurement/paint calls remain atomic. + + +Runtime mutation entry points reject direct calls while refresh work is pending, before executing source or changing metadata; task polling defers. The API worker drains the saved refresh before accepting those mutations, keeping the runtime usable. Incremental refresh keeps the prior complete document map until a stable pass, including across window traversal boundaries where no VM read-only frame is currently active. diff --git a/src/README.md b/src/README.md index 36887d7..8ed5d71 100644 --- a/src/README.md +++ b/src/README.md @@ -164,3 +164,6 @@ ReplSession now owns a resumable RefreshWork containing window traversal, pendin Viewport refresh now advances through ReplSession's saved refresh state on worker idle turns. A turn advances one traversal stage or a 256-instruction layout quantum; a pending job shortens the next worker wait to 1 ms. Tasks/window commands and document publication wait for complete refresh. ui.stats().refreshYields reports cooperative idle returns. Mutating requests first drain the existing job, so they cannot run inside its read-only scope. This is serialized input, not live cancellation/rollback or latest-input coalescing. Initial loads, explicit REPL/event refresh and task callbacks still drain synchronously; native Rust measurement/paint calls remain atomic. + + +Runtime mutation entry points reject direct calls while refresh work is pending, before executing source or changing metadata; task polling defers. The API worker drains the saved refresh before accepting those mutations, keeping the runtime usable. Incremental refresh keeps the prior complete document map until a stable pass, including across window traversal boundaries where no VM read-only frame is currently active. diff --git a/src/api.rs b/src/api.rs index fff05cf..7cca6a8 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1167,6 +1167,9 @@ UiBridge::begin_layout_frame/resume_layout_frame now expose PendingLayout around Viewport refresh now advances through ReplSession's saved refresh state on worker idle turns. A turn advances one traversal stage or a 256-instruction layout quantum; a pending job shortens the next worker wait to 1 ms. Tasks/window commands and document publication wait for complete refresh. ui.stats().refreshYields reports cooperative idle returns. Mutating requests first drain the existing job, so they cannot run inside its read-only scope. This is serialized input, not live cancellation/rollback or latest-input coalescing. Initial loads, explicit REPL/event refresh and task callbacks still drain synchronously; native Rust measurement/paint calls remain atomic. + +Runtime mutation entry points reject direct calls while refresh work is pending, before executing source or changing metadata; task polling defers. The API worker drains the saved refresh before accepting those mutations, keeping the runtime usable. Incremental refresh keeps the prior complete document map until a stable pass, including across window traversal boundaries where no VM read-only frame is currently active. + ## UI host object Positional arguments only: diff --git a/src/endor_host.rs b/src/endor_host.rs index b7c00a6..6c548ba 100644 --- a/src/endor_host.rs +++ b/src/endor_host.rs @@ -355,6 +355,7 @@ impl ReplSession { }) } pub fn execute(&mut self, source: &str, namespace: &str) -> Result { + self.require_idle_refresh()?; if self.faulted { bail!( "runtime stopped after a callback or rendering failure; reset the session or replace its program" @@ -494,6 +495,14 @@ impl ReplSession { self.refresh.is_some() } + fn require_idle_refresh(&self) -> Result<()> { + anyhow::ensure!( + !self.has_pending_refresh(), + "finish pending refresh before mutating this runtime" + ); + Ok(()) + } + fn refresh_document(&mut self) -> Result> { loop { match self.poll_refresh(false)? { @@ -733,6 +742,9 @@ impl ReplSession { /// Deliver ready work on the owning VM worker, never from the GUI or a /// provider thread. A failed callback stops the same shared fault domain. pub fn poll_tasks(&mut self) -> Result> { + if self.has_pending_refresh() { + return Ok(None); + } if self.faulted { if self.bridge.cancel_tasks(&mut self.candidate.vm)? > 0 { self.bridge @@ -762,6 +774,7 @@ impl ReplSession { } pub fn take_window_commands(&self) -> Result> { + self.require_idle_refresh()?; self.bridge.take_window_commands(&self.candidate.vm) } @@ -769,6 +782,7 @@ impl ReplSession { &mut self, viewport: Option, ) -> Result> { + self.require_idle_refresh()?; if self.faulted { bail!("runtime stopped after a callback or rendering failure"); } @@ -786,6 +800,7 @@ impl ReplSession { } pub fn retire_window(&mut self, id: u64) -> Result<()> { + self.require_idle_refresh()?; // This path is exclusively host cleanup after the domain has stopped. // No language functions, layout, paint or lifecycle hooks may run here. self.faulted = true; @@ -809,6 +824,7 @@ impl ReplSession { } pub fn dispatch(&mut self, input: &crate::events::Input) -> Result { + self.require_idle_refresh()?; if self.faulted { bail!("runtime stopped after a callback or rendering failure"); } @@ -4015,6 +4031,56 @@ page.render(block) ); } + #[test] + fn incremental_viewport_refresh_retains_published_documents_until_complete() { + let (mut session, _) = ReplSession::load( + include_str!("../examples/flex-constraints.endor"), + "app.main", + 271, + 1, + ) + .unwrap(); + let old_version = session.documents[&crate::window::MAIN_WINDOW] + .frame + .layout_version; + let viewports = std::collections::BTreeMap::from([( + crate::window::MAIN_WINDOW, + crate::layout::Size { + width: 800.0, + height: 600.0, + }, + )]); + assert!( + session + .resize_windows_incremental(&viewports, false) + .unwrap() + .is_none() + ); + assert!(session.has_pending_refresh()); + assert!(session.execute("blocked = 1", "app.main").is_err()); + assert!(!session.is_faulted()); + let mut completed = false; + for _ in 0..1000 { + if let Some(document) = session + .resize_windows_incremental(&viewports, false) + .unwrap() + { + assert!(document.frame.layout_version > old_version); + assert!(!session.has_pending_refresh()); + completed = true; + break; + } + assert_eq!( + session.documents[&crate::window::MAIN_WINDOW] + .frame + .layout_version, + old_version + ); + } + assert!(completed); + assert!(source_measure_stat(&mut session, "refreshYields") > 0); + } + #[test] fn bridge_layout_can_return_pending_before_committing_a_complete_frame() { let children = std::iter::repeat_n("item", 40)