Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(renderer): retire failed committed navigations

ldm006134f461df1
diff --git a/moli-renderer-v8/src/runtime/owner.rs b/moli-renderer-v8/src/runtime/owner.rs
index 2e528145a..993eafae7 100644
--- a/moli-renderer-v8/src/runtime/owner.rs
+++ b/moli-renderer-v8/src/runtime/owner.rs
@@ -3255,7 +3255,6 @@ impl RendererOwnerHandle {
retire_page_on_failure: bool,
disposition: LivePageNavigationFailureDisposition,
) -> RenderRuntimeDispatchOutcome {
- entry.settle_standalone_navigation_follow(false);
let had_uncommitted_page_vm = entry.has_uncommitted_page_vm();
let navigation_committed = entry
.vm
@@ -3297,7 +3296,14 @@ impl RendererOwnerHandle {
);
remove_page_on_bound_owner_local_store(token);
}
- self.restore_live_page_entry(token, entry);
+ if navigation_committed {
+ let mut entry = entry.into_retiring_after_committed_navigation();
+ entry.settle_standalone_navigation_follow(false);
+ restore_retiring_entry_after_command_on_bound_owner_local_store(token, entry);
+ } else {
+ entry.settle_standalone_navigation_follow(false);
+ self.restore_live_page_entry(token, entry);
+ }
match disposition {
LivePageNavigationFailureDisposition::PublishToPageCreation(failure) => {
match page_creation_publication
diff --git a/moli-renderer-v8/src/runtime/owner_local_store/entry.rs b/moli-renderer-v8/src/runtime/owner_local_store/entry.rs
index d7d628bfe..fce59db56 100644
--- a/moli-renderer-v8/src/runtime/owner_local_store/entry.rs
+++ b/moli-renderer-v8/src/runtime/owner_local_store/entry.rs
@@ -234,6 +234,26 @@ impl LivePageEntry {
self.standalone_navigation_follow.settle(current, succeeded);
}
+ /// Consume the inert PageVm shell left after a cross-document commit whose
+ /// replacement failed to bootstrap. The old ScriptVm is already gone, so
+ /// this entry can only return through the teardown-only residence path.
+ pub(in crate::runtime) fn into_retiring_after_committed_navigation(
+ mut self,
+ ) -> RetiringPageEntry {
+ assert!(
+ self.pending_phase_one_navigation.is_none(),
+ "a committed navigation shell cannot coexist with pending phase one"
+ );
+ assert!(
+ self.vm
+ .as_ref()
+ .is_none_or(|page_vm| !page_vm.has_live_script_vm()),
+ "a committed navigation shell must not retain a live ScriptVm"
+ );
+ self.vm = None;
+ RetiringPageEntry::new(self)
+ }
+
/// A replacement PageVm becomes the active owner-local runtime before its
/// view is committed to the stable cross-thread Page slot.
pub(in crate::runtime) fn has_uncommitted_page_vm(&self) -> bool {
diff --git a/moli-renderer-v8/src/runtime/owner_local_store/tests.rs b/moli-renderer-v8/src/runtime/owner_local_store/tests.rs
index d94c9583d..ba1903eda 100644
--- a/moli-renderer-v8/src/runtime/owner_local_store/tests.rs
+++ b/moli-renderer-v8/src/runtime/owner_local_store/tests.rs
@@ -111,16 +111,8 @@ mod navigation_dispatch_tests {
}
}
- fn retiring_entry_without_active_page_vm(
- standalone_navigation_follow: StandaloneNavigationFollowState,
- ) -> RetiringPageEntry {
- RetiringPageEntry::new(phase_one_entry_shell_without_active_page_vm(
- standalone_navigation_follow,
- ))
- }
-
#[test]
- fn retiring_navigation_settlement_does_not_require_an_active_vm() {
+ fn committed_navigation_transitions_to_retiring_before_settlement() {
let handoff = crate::page_task_queue::RendererTopLevelNavigationHandoff::new(1);
for succeeded in [false, true] {
for state in [
@@ -128,14 +120,15 @@ mod navigation_dispatch_tests {
StandaloneNavigationFollowState::Following { handoff },
StandaloneNavigationFollowState::FailedWithPendingNavigation { handoff },
] {
- let mut entry = retiring_entry_without_active_page_vm(state);
+ let entry = phase_one_entry_shell_without_active_page_vm(state);
+ let mut entry = entry.into_retiring_after_committed_navigation();
entry.settle_standalone_navigation_follow(succeeded);
assert_eq!(
entry.entry.standalone_navigation_follow,
StandaloneNavigationFollowState::Idle,
- "an empty phase-one shell must settle {state:?} to Idle after succeeded={succeeded}"
+ "a committed navigation shell must settle {state:?} to Idle after succeeded={succeeded}"
);
}
}
diff --git a/moli-renderer-v8/src/runtime/page.rs b/moli-renderer-v8/src/runtime/page.rs
index 3fe54faae..854a6253c 100644
--- a/moli-renderer-v8/src/runtime/page.rs
+++ b/moli-renderer-v8/src/runtime/page.rs
@@ -210,6 +210,11 @@ impl JsRuntime {
.close_command_admission_for_testing();
}
+ #[cfg(test)]
+ pub(crate) fn renderer_page_count_for_testing(&self) -> usize {
+ self.inner.renderer_owner.len()
+ }
+
#[cfg(test)]
pub(crate) fn start_minimal_html_page_for_reservation_testing(
&self,
diff --git a/moli-renderer-v8/src/runtime/tests.rs b/moli-renderer-v8/src/runtime/tests.rs
index f058a17bb..d92650d1b 100644
--- a/moli-renderer-v8/src/runtime/tests.rs
+++ b/moli-renderer-v8/src/runtime/tests.rs
@@ -3090,6 +3090,67 @@ document.close();
.expect("document.open navigation identity test page should close");
}
+#[tokio::test(flavor = "current_thread")]
+async fn committed_navigation_bootstrap_failure_retires_page_before_follow_settlement() {
+ let runtime = JsRuntime::initialize();
+ let loader =
+ ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).expect("default loader");
+ let (base_url, server) = spawn_owner_wake_server_with_content_type_and_headers(
+ "/replacement",
+ "<!doctype html><main>replacement</main>",
+ "text/html",
+ vec![("x-moli-test-fail-after-navigation-commit", "1")],
+ Duration::ZERO,
+ )
+ .await;
+ let source_url = url::Url::parse(&format!("{base_url}/source")).expect("source URL");
+ let replacement_url = format!("{base_url}/replacement");
+ let replacement_url_literal =
+ serde_json::to_string(&replacement_url).expect("serialize replacement URL");
+ let html = format!(
+ "<!doctype html><script>location.href = {replacement_url_literal};</script><main>source</main>"
+ );
+
+ let pending = start_test_html_page_with_optional_indexed_db_manager_and_navigation_dispatch(
+ &runtime,
+ &loader,
+ source_url,
+ &html,
+ None,
+ RendererTopLevelNavigationDispatch::FollowInStandaloneAdapter,
+ );
+ let result = pending.await_ready().await;
+ let Err(error) = result else {
+ panic!("the injected post-commit bootstrap failure must reject page creation")
+ };
+ let failure = format!("{error:#}");
+ assert!(
+ failure.contains("injected failure after main navigation commit for testing"),
+ "page creation must report the injected post-commit failure: {failure}"
+ );
+ server
+ .await
+ .expect("post-commit failure server should finish");
+ assert_eq!(
+ runtime.renderer_page_count_for_testing(),
+ 0,
+ "a committed navigation bootstrap failure must not restore its inert PageVm as live"
+ );
+
+ let mut survivor = create_test_html_page(
+ &runtime,
+ &loader,
+ url::Url::parse("about:blank").expect("survivor URL"),
+ "<!doctype html><main>survivor</main>",
+ )
+ .await;
+ survivor
+ .close_async()
+ .await
+ .expect("renderer owner should remain usable after retiring the failed navigation");
+ assert_eq!(runtime.renderer_page_count_for_testing(), 0);
+}
+
#[tokio::test(flavor = "current_thread")]
async fn external_raw_streaming_delegates_post_load_meta_refresh_to_browser() {
let runtime = JsRuntime::initialize();
@@ -17372,7 +17433,31 @@ async fn create_test_html_page_with_optional_indexed_db_manager_and_navigation_d
indexed_db_manager: Option<crate::WeakIndexedDbManager>,
top_level_navigation_dispatch: RendererTopLevelNavigationDispatch,
) -> RendererPageHandle {
- let pending = runtime
+ let pending = start_test_html_page_with_optional_indexed_db_manager_and_navigation_dispatch(
+ runtime,
+ loader,
+ url,
+ html,
+ indexed_db_manager,
+ top_level_navigation_dispatch,
+ );
+ let (page, _, _, _creation_artifacts, pending_download) = pending
+ .await_ready()
+ .await
+ .expect("test HTML page should load");
+ assert!(pending_download.is_none());
+ page
+}
+
+fn start_test_html_page_with_optional_indexed_db_manager_and_navigation_dispatch(
+ runtime: &JsRuntime,
+ loader: &ResourceRequestClient,
+ url: url::Url,
+ html: &str,
+ indexed_db_manager: Option<crate::WeakIndexedDbManager>,
+ top_level_navigation_dispatch: RendererTopLevelNavigationDispatch,
+) -> super::PendingHtmlPage {
+ runtime
.start_create_html_page_from_response_with_inspector_session_restores(
runtime.reserve_page_for_creation(),
url.clone(),
@@ -17407,13 +17492,7 @@ async fn create_test_html_page_with_optional_indexed_db_manager_and_navigation_d
top_level_navigation_dispatch,
None,
)
- .expect("test HTML page should start");
- let (page, _, _, _creation_artifacts, pending_download) = pending
- .await_ready()
- .await
- .expect("test HTML page should load");
- assert!(pending_download.is_none());
- page
+ .expect("test HTML page should start")
}
async fn create_test_html_page_at_document_commit(
@@ -17952,6 +18031,23 @@ async fn spawn_owner_wake_server_with_content_type(
body: &'static str,
content_type: &'static str,
delay: Duration,
+) -> (String, tokio::task::JoinHandle<()>) {
+ spawn_owner_wake_server_with_content_type_and_headers(
+ expected_path,
+ body,
+ content_type,
+ Vec::new(),
+ delay,
+ )
+ .await
+}
+
+async fn spawn_owner_wake_server_with_content_type_and_headers(
+ expected_path: &'static str,
+ body: &'static str,
+ content_type: &'static str,
+ response_headers: Vec<(&'static str, &'static str)>,
+ delay: Duration,
) -> (String, tokio::task::JoinHandle<()>) {
let listener = TcpListener::bind("127.0.0.1:0")
.await
@@ -17972,8 +18068,12 @@ async fn spawn_owner_wake_server_with_content_type(
if !delay.is_zero() {
tokio::time::sleep(delay).await;
}
+ let response_headers = response_headers
+ .into_iter()
+ .map(|(name, value)| format!("{name}: {value}\r\n"))
+ .collect::<String>();
let response = format!(
- "HTTP/1.1 200 OK\r\nContent-Type: {content_type}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
+ "HTTP/1.1 200 OK\r\nContent-Type: {content_type}\r\n{response_headers}Content-Length: {}\r\nConnection: close\r\n\r\n{}",
body.len(),
body
);
diff --git a/moli/tests/fetch_cli.rs b/moli/tests/fetch_cli.rs
index af6c811a9..c638d0ffa 100644
--- a/moli/tests/fetch_cli.rs
+++ b/moli/tests/fetch_cli.rs
@@ -3006,6 +3006,39 @@ fn cli_streaming_page_creation_timeout_exits_cleanly_for_every_wait_mode() -> Re
Ok(())
}
+#[test]
+fn cli_meta_refresh_shutdown_exits_without_renderer_abort() -> Result<()> {
+ let output = Command::new(env!("CARGO_BIN_EXE_moli"))
+ .args([
+ "fetch",
+ "--log-level",
+ "error",
+ "--wait-until",
+ "load",
+ "--dump",
+ "html",
+ "data:text/html,<meta http-equiv='refresh' content='0;url=about:blank'>",
+ ])
+ .output()?;
+ let stdout = clean_output(&output.stdout);
+ let stderr = clean_output(&output.stderr);
+
+ assert_eq!(
+ output.status.code(),
+ Some(0),
+ "meta refresh shutdown must not abort the CLI: stdout={stdout}\nstderr={stderr}"
+ );
+ assert!(
+ stdout.contains("http-equiv=\"refresh\""),
+ "the source document should still be emitted: stdout={stdout}"
+ );
+ assert!(
+ !stderr.contains("PageVm must retain a live ScriptVm until drop"),
+ "renderer teardown invariant leaked to the CLI: stderr={stderr}"
+ );
+ Ok(())
+}
+
#[test]
fn cli_domcontentloaded_exit_stays_stable_with_inflight_background_fetch_tail() -> Result<()> {
let runtime = tokio::runtime::Runtime::new()?;