Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
fix(renderer): enter committed typestate before source teardown
ldm09384cf96c5f1
diff --git a/moli-renderer-v8/src/runtime/mod.rs b/moli-renderer-v8/src/runtime/mod.rsindex e2383a5f5..bfd7f4336 100644--- a/moli-renderer-v8/src/runtime/mod.rs+++ b/moli-renderer-v8/src/runtime/mod.rs@@ -404,7 +404,7 @@ pub(crate) use self::page_vm::{};pub(in crate::runtime) use self::page_vm::{PageVmCommittedNavigationBootstrap, PageVmDocumentCommitPreparation,- PageVmPreparedFollowedNavigationCommit,+ PageVmPreparedFollowedNavigationCommit, PageVmValidatedFollowedNavigationCommit,};pub use self::phase_one::ExternalRawDocumentBodyStream;use self::phase_one::PendingPhaseOneResidence;diff --git a/moli-renderer-v8/src/runtime/owner_local_store/entry.rs b/moli-renderer-v8/src/runtime/owner_local_store/entry.rsindex 2cf54712d..bb3747d8c 100644--- a/moli-renderer-v8/src/runtime/owner_local_store/entry.rs+++ b/moli-renderer-v8/src/runtime/owner_local_store/entry.rs@@ -76,8 +76,9 @@ pub(in crate::runtime) struct LivePageEntry {pub(super) last_published_replacement_document: Option<PublishedReplacementDocument>,}-/// A checked-out Page whose source Document has committed away while its-/// replacement `PageVm` is still being bootstrapped.+/// A checked-out Page that has crossed the navigation commit boundary. It+/// retains the source residence until cleanup completes, then owns replacement+/// bootstrap until a live replacement `PageVm` has been installed.////// This state never enters the stable Page scheduler. It exists only inside/// the navigation local-task guard, so cancellation and panic return a typed@@ -100,14 +101,45 @@ pub(in crate::runtime) struct RetiringPageEntry {impl CommittedNavigationEntry {pub(super) fn new(- mut entry: LivePageEntry,- navigation: PageVmCommittedNavigationBootstrap,+ entry: LivePageEntry,+ navigation: Box<PageVmCommittedNavigationBootstrap>,) -> Self {- entry.vm = None;- Self {- entry,- navigation: Box::new(navigation),- }+ Self { entry, navigation }+ }++ /// Called only after the local-task guard owns this committed residence.+ /// A panic during detachment, lifecycle termination or source retirement+ /// must return this type to the owner instead of restoring a live Page.+ pub(super) fn commit_source(+ &mut self,+ commit: crate::script_vm::MainWindowProxyNavigationCommit,+ ) {+ commit.detach();+ #[cfg(test)]+ self.navigation+ .inject_commit_panic_for_test("x-moli-test-panic-after-source-detach");+ self.entry.page_vm_mut().record_main_navigation_commit();+ self.retire_source();+ }++ fn retire_source(&mut self) {+ self.entry.retire_document_lifecycle_turn();+ let mut page_vm = if let Some(pending) = self.entry.pending_phase_one_navigation.take() {+ let (residence, mut metadata) = pending.into_parts();+ let mut page_vm = residence.retire_for_committed_navigation();+ metadata.complete_service_worker_follow(&mut page_vm);+ page_vm+ } else {+ self.entry+ .vm+ .take()+ .expect("committed navigation must retain its source PageVm until retirement")+ };+ #[cfg(test)]+ self.navigation+ .inject_commit_panic_for_test("x-moli-test-panic-during-source-retirement");+ page_vm.retire_committed_main_script_vm();+ self.entry.vm = None;}pub(super) async fn bootstrap_replacement(@@ -153,26 +185,29 @@ impl CommittedNavigationEntry {}}- pub(super) fn into_live(self) -> LivePageEntry {- assert!(+ pub(super) fn ensure_live_replacement(&self) -> Result<()> {+ ensure!(self.entry.active_page_vm().is_some_and(PageVm::has_live_script_vm),"a completed committed navigation must produce a live replacement PageVm");+ Ok(())+ }++ pub(super) fn into_live(self) -> LivePageEntry {self.entry}pub(in crate::runtime) fn reject_and_retire(mut self, failure: &str) -> RetiringPageEntry {self.navigation.reject(failure);- // Replacement installation and the Committed -> Live tag change are- // synchronous, but a panic inside that narrow transition must still- // retire any partially installed replacement instead of double- // panicking in the task-guard cleanup path.+ // Detachment and source cleanup may unwind before releasing the source+ // residence. Bootstrap may also leave a partially installed replacement.+ // Both belong to this committed state and must be retired in place.if self.entry.active_page_vm().is_some() {tracing::error!(failure,- "retiring a replacement PageVm left inside a committed navigation transition"+ "retiring a PageVm retained by a committed navigation");self.entry.close_for_context_teardown();self.entry.vm = None;@@ -350,30 +385,6 @@ impl LivePageEntry {self.renderer_navigation_follow.settle(current, succeeded);}- /// Commit the source Document synchronously. The navigation task guard- /// must immediately move this entry into `CommittedNavigationEntry`- /// before crossing another await boundary.- pub(in crate::runtime) fn commit_prepared_navigation(- &mut self,- prepared: PageVmPreparedFollowedNavigationCommit,- ) -> Result<PageVmCommittedNavigationBootstrap> {- // Commit validation and WindowProxy detachment can fail. Keep the- // complete source residence live until they succeed; every operation- // after that boundary is synchronous and infallible.- let navigation = self- .page_vm_mut()- .commit_prepared_followed_location_navigation(prepared)?;- if let Some(pending) = self.pending_phase_one_navigation.take() {- let (residence, mut metadata) = pending.into_parts();- let mut page_vm = residence.into_navigation_triggered_page_vm();- metadata.complete_service_worker_follow(&mut page_vm);- self.install_resumed_phase_one_page_vm(page_vm);- }- self.retire_document_lifecycle_turn();- self.page_vm_mut().retire_committed_main_script_vm();- Ok(navigation)- }-/// 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/navigation_follow.rs b/moli-renderer-v8/src/runtime/owner_local_store/navigation_follow.rsindex 146c16792..dc9853099 100644--- a/moli-renderer-v8/src/runtime/owner_local_store/navigation_follow.rs+++ b/moli-renderer-v8/src/runtime/owner_local_store/navigation_follow.rs@@ -6,7 +6,8 @@ use crate::local_executor::JsLocalExecutor;use crate::runtime::page_vm::DocumentLifecycleTurnOutcome;use crate::runtime::{PageVmDocumentCommitPreparation, PageVmFollowNavigationTurnOutcome, PageVmInitStage,- PageVmPreparedFollowedNavigationCommit, RendererPageToken, RendererPendingDownloadActivation,+ PageVmPreparedFollowedNavigationCommit, PageVmValidatedFollowedNavigationCommit,+ RendererPageToken, RendererPendingDownloadActivation,};use super::bound::run_typed_entry_on_bound_owner_local_store_local_task;@@ -89,19 +90,31 @@ impl NavigationLocalTaskEntry {}fn commit(&mut self, prepared: PageVmPreparedFollowedNavigationCommit) -> Result<()> {- let navigation = self.live_mut().commit_prepared_navigation(prepared)?;+ let validated: PageVmValidatedFollowedNavigationCommit = self+ .live_mut()+ .page_vm_mut()+ .validate_prepared_followed_location_navigation(prepared)?;+ let (commit, navigation) = validated.into_parts();+ // Only moves remain between removing Live and publishing Committed.+ // Detachment and all source teardown run with the committed tag+ // already held by the local-task guard.let Self::Live(entry) = std::mem::replace(self, Self::Transitioning) else {unreachable!("navigation commit must consume a live entry")};*self = Self::Committed(CommittedNavigationEntry::new(entry, navigation));+ self.committed_mut().commit_source(commit);Ok(())}- fn finish_commit(&mut self) {+ fn finish_commit(&mut self) -> Result<()> {+ // Validate before moving the entry so even a broken bootstrap invariant+ // returns the committed residence through the task guard.+ self.committed_mut().ensure_live_replacement()?;let Self::Committed(entry) = std::mem::replace(self, Self::Transitioning) else {unreachable!("replacement bootstrap must complete a committed entry")};*self = Self::Live(entry.into_live());+ Ok(())}async fn bootstrap_committed_navigation(&mut self) -> Result<LivePageNavigationFollowOutcome> {@@ -109,7 +122,7 @@ impl NavigationLocalTaskEntry {let completion = self.committed_mut().install_bootstrap_outcome(bootstrap_outcome)?;- self.finish_commit();+ self.finish_commit()?;match completion {CommittedNavigationBootstrapCompletion::ContinuePostParseLifecycle {page_tasks,diff --git a/moli-renderer-v8/src/runtime/page_vm/followed_navigation.rs b/moli-renderer-v8/src/runtime/page_vm/followed_navigation.rsindex e4ef9400c..82bd67bd1 100644--- a/moli-renderer-v8/src/runtime/page_vm/followed_navigation.rs+++ b/moli-renderer-v8/src/runtime/page_vm/followed_navigation.rs@@ -528,6 +528,24 @@ pub(in crate::runtime) struct PageVmPreparedFollowedNavigationCommit {stage: PageVmInitStage,}+/// All fallible checks and bootstrap allocations complete while the source+/// Page remains live. Consuming this value does not itself detach or retire it.+pub(in crate::runtime) struct PageVmValidatedFollowedNavigationCommit {+ window_proxy_commit: crate::script_vm::MainWindowProxyNavigationCommit,+ navigation: Box<PageVmCommittedNavigationBootstrap>,+}++impl PageVmValidatedFollowedNavigationCommit {+ pub(in crate::runtime) fn into_parts(+ self,+ ) -> (+ crate::script_vm::MainWindowProxyNavigationCommit,+ Box<PageVmCommittedNavigationBootstrap>,+ ) {+ (self.window_proxy_commit, self.navigation)+ }+}+struct PageVmCommittedNavigationBootstrapPayload {page_id: PageId,local_executor: JsLocalExecutor,@@ -556,6 +574,17 @@ pub(in crate::runtime) struct PageVmCommittedNavigationBootstrap {}impl PageVmCommittedNavigationBootstrap {+ #[cfg(test)]+ pub(in crate::runtime) fn inject_commit_panic_for_test(&self, injection_header: &str) {+ if self+ .payload+ .as_ref()+ .is_some_and(|payload| payload.loaded.has_header_for_test(injection_header))+ {+ panic!("injected navigation commit panic for testing: {injection_header}");+ }+ }+pub(in crate::runtime) async fn bootstrap(&mut self,) -> Result<PageVmFollowedNavigationBuildOutcome> {@@ -912,10 +941,10 @@ impl PageVm {})}- pub(in crate::runtime) fn commit_prepared_followed_location_navigation(+ pub(in crate::runtime) fn validate_prepared_followed_location_navigation(&mut self,prepared: PageVmPreparedFollowedNavigationCommit,- ) -> Result<PageVmCommittedNavigationBootstrap> {+ ) -> Result<PageVmValidatedFollowedNavigationCommit> {let PageVmPreparedFollowedNavigationCommit {initiator_url,navigation_handoff,@@ -953,18 +982,21 @@ impl PageVm {runtime_hooks.has_renderer_page_script_environment(),"owner-managed navigation commit requires a renderer Page script environment");- self.commit_main_window_proxy_navigation(commit_page_id)+ self.prepare_main_window_proxy_navigation(commit_page_id)})();- if let Err(error) = commit_result {- self.reject_failed_followed_location_navigation(- &initiator_url,- reserved_service_worker_client_id,- service_worker_client_navigate,- &error,- );- return Err(error);- }- Ok(PageVmCommittedNavigationBootstrap {+ let window_proxy_commit = match commit_result {+ Ok(commit) => commit,+ Err(error) => {+ self.reject_failed_followed_location_navigation(+ &initiator_url,+ reserved_service_worker_client_id,+ service_worker_client_navigate,+ &error,+ );+ return Err(error);+ }+ };+ let navigation = Box::new(PageVmCommittedNavigationBootstrap {payload: Some(PageVmCommittedNavigationBootstrapPayload {page_id,local_executor,@@ -980,6 +1012,10 @@ impl PageVm {navigation_handoff,reserved_service_worker_client_id,service_worker_client_navigate,+ });+ Ok(PageVmValidatedFollowedNavigationCommit {+ window_proxy_commit,+ navigation,})}@@ -1236,7 +1272,9 @@ impl PageVm {let env = self.followed_location_navigation_env();let runtime_hooks = self.runtime_hooks.clone().for_cross_document_commit();if runtime_hooks.has_renderer_page_script_environment() {- self.commit_main_window_proxy_navigation(self.page_id)?;+ self.prepare_main_window_proxy_navigation(self.page_id)?+ .detach();+ self.record_main_navigation_commit();self.retire_committed_main_script_vm();} else {self.record_main_navigation_commit();diff --git a/moli-renderer-v8/src/runtime/page_vm/mod.rs b/moli-renderer-v8/src/runtime/page_vm/mod.rsindex da4aa8715..61d4958fb 100644--- a/moli-renderer-v8/src/runtime/page_vm/mod.rs+++ b/moli-renderer-v8/src/runtime/page_vm/mod.rs@@ -160,7 +160,8 @@ use css_agent_state::RendererCssAgentSessionState;use dom_agent_state::RendererDomAgentState;pub(in crate::runtime) use followed_navigation::{PageVmCommittedNavigationBootstrap, PageVmDocumentCommitPreparation,- PageVmPreparedFollowedNavigationCommit, attach_navigation_response_to_page_vm,+ PageVmPreparedFollowedNavigationCommit, PageVmValidatedFollowedNavigationCommit,+ attach_navigation_response_to_page_vm,};#[cfg(test)]pub(crate) use page_dom_manipulation_test_support::PageDomManipulationTestFamily;@@ -2191,23 +2192,17 @@ impl PageVm {.page_task_producer_routes_match(sources)}- fn commit_main_window_proxy_navigation(&mut self, page_id: PageId) -> Result<()> {+ fn prepare_main_window_proxy_navigation(+ &mut self,+ page_id: PageId,+ ) -> Result<crate::script_vm::MainWindowProxyNavigationCommit> {let vm = self.vm.as_mut().ok_or_else(|| {anyhow::anyhow!("main navigation attempted to commit an already retired PageVm")})?;- // ScriptVm validates ownership and proxy identity before detach_global.- // An Err therefore leaves the inspector, context resources and parser- // intact. Keep the ScriptVm until its parser residence has been retired.- vm.detach_main_window_proxy_for_navigation_commit(page_id.as_u64())?;- self.record_main_navigation_commit();- tracing::debug!(- page_id = self.page_id.as_u64(),- "committed main navigation before replacement realm bootstrap"- );- Ok(())+ vm.prepare_main_window_proxy_navigation_commit(page_id.as_u64())}- fn record_main_navigation_commit(&mut self) {+ pub(in crate::runtime) fn record_main_navigation_commit(&mut self) {let termination = self.document_lifecycle.request_termination(self.document_lifecycle.identity(),RendererDocumentTerminationReason::SupersededByCrossDocumentNavigation,diff --git a/moli-renderer-v8/src/runtime/phase_one/pending_residence.rs b/moli-renderer-v8/src/runtime/phase_one/pending_residence.rsindex b2e78b3e3..c88b53f46 100644--- a/moli-renderer-v8/src/runtime/phase_one/pending_residence.rs+++ b/moli-renderer-v8/src/runtime/phase_one/pending_residence.rs@@ -133,15 +133,15 @@ impl PendingPhaseOneResidence {}}- /// Retire the source parser while keeping its PageVm live for a prepared- /// cross-document navigation commit. This never waits for source input.- pub(in crate::runtime) fn into_navigation_triggered_page_vm(self) -> PageVm {+ /// Retire the source parser after navigation crosses the committed+ /// typestate boundary. Keep the PageVm only for final context teardown.+ pub(in crate::runtime) fn retire_for_committed_navigation(self) -> PageVm {match self {Self::ParserBlockingSourceLoad { runtime, .. }| Self::ClosedInputPageWork { runtime, .. } => {(*runtime).into_navigation_triggered_page_vm()}- Self::OpenStreaming(continuation) => continuation.into_navigation_triggered_page_vm(),+ Self::OpenStreaming(continuation) => continuation.retire_for_committed_navigation(),}}diff --git a/moli-renderer-v8/src/runtime/phase_one/streaming_residence.rs b/moli-renderer-v8/src/runtime/phase_one/streaming_residence.rsindex a1c341605..88722b39d 100644--- a/moli-renderer-v8/src/runtime/phase_one/streaming_residence.rs+++ b/moli-renderer-v8/src/runtime/phase_one/streaming_residence.rs@@ -74,7 +74,7 @@ impl PendingStreamingPhaseOneContinuation {self.input.has_ready_input()}- pub(super) fn into_navigation_triggered_page_vm(self) -> PageVm {+ pub(super) fn retire_for_committed_navigation(self) -> PageVm {let Self { runtime, input, .. } = self;// Dropping the input receiver cancels the old body bridge without// waiting for its response to finish.diff --git a/moli-renderer-v8/src/runtime/tests/extracted/navigation_streaming.rs b/moli-renderer-v8/src/runtime/tests/extracted/navigation_streaming.rsindex b90ee9783..71de372c8 100644--- a/moli-renderer-v8/src/runtime/tests/extracted/navigation_streaming.rs+++ b/moli-renderer-v8/src/runtime/tests/extracted/navigation_streaming.rs@@ -1346,6 +1346,25 @@ async fn committed_navigation_bootstrap_panic_returns_committed_entry_to_owner()).await;}++#[tokio::test(flavor = "current_thread")]+async fn committed_navigation_source_detach_panic_returns_committed_entry_to_owner() {+ assert_committed_navigation_bootstrap_injection_retires_page(+ "x-moli-test-panic-after-source-detach",+ "local task panicked before restoring its page entry",+ )+ .await;+}++#[tokio::test(flavor = "current_thread")]+async fn committed_navigation_source_retirement_panic_returns_committed_entry_to_owner() {+ assert_committed_navigation_bootstrap_injection_retires_page(+ "x-moli-test-panic-during-source-retirement",+ "local task panicked before restoring its page entry",+ )+ .await;+}+#[tokio::test(flavor = "current_thread")]async fn external_raw_streaming_delegates_post_load_meta_refresh_to_browser() {let runtime = JsRuntime::initialize();diff --git a/moli-renderer-v8/src/runtime/tests/open_streaming.rs b/moli-renderer-v8/src/runtime/tests/open_streaming.rsindex 302bbe189..bb1e81d7e 100644--- a/moli-renderer-v8/src/runtime/tests/open_streaming.rs+++ b/moli-renderer-v8/src/runtime/tests/open_streaming.rs@@ -86,6 +86,77 @@ async fn location_replace_commits_while_source_phase_one_stream_remains_open() {server.await.expect("self-replacement server should finish");}+#[tokio::test(flavor = "multi_thread")]+async fn location_replace_source_detach_panic_retires_open_source_stream() {+ assert_post_commit_panic_retires_open_source_stream("x-moli-test-panic-after-source-detach")+ .await;+}++#[tokio::test(flavor = "multi_thread")]+async fn location_replace_source_retirement_panic_retires_open_source_stream() {+ assert_post_commit_panic_retires_open_source_stream(+ "x-moli-test-panic-during-source-retirement",+ )+ .await;+}++async fn assert_post_commit_panic_retires_open_source_stream(injection_header: &'static str) {+ let (base_url, server) = spawn_owner_wake_server_with_content_type_and_headers(+ "/page",+ "<!doctype html><main>replacement</main>",+ "text/html",+ vec![(injection_header, "1")],+ Duration::ZERO,+ )+ .await;+ let source = OpenStreamingPage::create(&base_url, SELF_REPLACE_WHILE_PARSING_HTML).await;+ tokio::time::timeout(Duration::from_secs(5), server)+ .await+ .expect("replacement response should reach the commit boundary")+ .expect("source-detach panic server should finish");++ // Exercise both the retained source residence just after detach_global and+ // the consumed parser during teardown. The guard must already be Committed.+ tokio::time::timeout(Duration::from_secs(5), source.body_tx.closed())+ .await+ .expect("post-detach panic must retire the source body receiver");+ assert!(+ source.completion_tx.send(Ok(())).is_err(),+ "post-detach retirement must release the source completion receiver"+ );+ assert!(+ source+ .page+ .run_async_command(RendererPageCommand::EvaluateExpression {+ expression: "document.readyState".to_owned(),+ await_promise: false,+ })+ .await+ .is_err(),+ "the retired source must not accept commands"+ );+ let runtime = source._runtime_owner.handle();+ assert_eq!(+ runtime.renderer_page_count_for_testing(),+ 0,+ "the detached source must never be restored as a live Page"+ );++ let loader = source._loader_owner.handle();+ 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 the commit panic");+ assert_eq!(runtime.renderer_page_count_for_testing(), 0);+}+#[tokio::test(flavor = "multi_thread")]async fn location_replace_without_document_preserves_pending_phase_one_stream() {let listener = TcpListener::bind("127.0.0.1:0")diff --git a/moli-renderer-v8/src/script_vm.rs b/moli-renderer-v8/src/script_vm.rsindex ecb340cf1..fd4746f76 100644--- a/moli-renderer-v8/src/script_vm.rs+++ b/moli-renderer-v8/src/script_vm.rs@@ -54,6 +54,31 @@ use url::Url;pub(crate) type ScriptVmBootstrapError = Box<(anyhow::Error, DomHost)>;+/// A one-shot commit for a source context whose page ownership and stable+/// WindowProxy have already been checked. The navigation task must enter its+/// committed typestate before consuming this token.+pub(crate) struct MainWindowProxyNavigationCommit {+ context: v8::Global<v8::Context>,+ renderer_document_isolate: RendererDocumentIsolateHandle,+ page_id: u64,+}++impl MainWindowProxyNavigationCommit {+ pub(crate) fn detach(self) {+ self.renderer_document_isolate+ .with_renderer_document_isolate_mut(|isolate| {+ let scope = pin!(v8::HandleScope::new(isolate));+ let scope = &mut scope.init();+ v8::Local::new(scope, &self.context).detach_global();+ });+ tracing::debug!(+ page_id = self.page_id,+ isolate_identity_key = self.renderer_document_isolate.identity_key(),+ "detached committed main WindowProxy for replacement context"+ );+ }+}+fn renderer_document_isolate_critical_pressure_required(used_heap_size: usize,heap_size_limit: usize,@@ -2507,10 +2532,10 @@ impl ScriptVm {.deactivate_page_vm_binding_for_teardown();}- pub(super) fn detach_main_window_proxy_for_navigation_commit(+ pub(super) fn prepare_main_window_proxy_navigation_commit(&mut self,page_id: u64,- ) -> Result<()> {+ ) -> Result<MainWindowProxyNavigationCommit> {let environment = self.renderer_page_script_environment.as_ref()@@ -2521,9 +2546,9 @@ impl ScriptVm {"main navigation crossed page script environment ownership"));}- let isolate_identity_key = self.renderer_document_isolate.identity_key();let context_ptr: *const v8::Global<v8::Context> = &self.page_default_context;- self.renderer_document_isolate+ let context = self+ .renderer_document_isolate.with_entered_renderer_document_isolate(|isolate| {let scope = pin!(v8::HandleScope::new(isolate));let scope = &mut scope.init();@@ -2537,15 +2562,13 @@ impl ScriptVm {"main context global does not match its page-owned WindowProxy"));}- context.detach_global();- Ok(())+ Ok(v8::Global::new(scope, context))})?;- tracing::debug!(+ Ok(MainWindowProxyNavigationCommit {+ context,+ renderer_document_isolate: self.renderer_document_isolate.clone(),page_id,- isolate_identity_key,- "detached committed main WindowProxy for replacement context"- );- Ok(())+ })}pub(super) fn sync_live_document_style_sources_if_pending(&mut self) {