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): retire unpromoted child realm handles
ldm031d40b20a600
diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/isolated_world.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/isolated_world.rsindex 409e8f0f4..063c04dd8 100644--- a/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/isolated_world.rs+++ b/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/isolated_world.rs@@ -139,12 +139,16 @@ impl JsContextHost {}let stale = pending_contexts.borrow_mut().remove(&handle);if let Some(stale) = stale {+ let stale_context = v8::Local::new(scope, &stale.context);+ {+ let stale_scope = &mut v8::ContextScope::new(scope, stale_context);+ crate::native_bridge::clear_context_wrapper_cache_for_teardown(stale_scope, false);+ }self.pending_history_traversal_admissions.retire_owner(WindowExecutionContextOwner::Frame(stale.local_window_id));self.retire_window_execution_contexts_for_context_token(stale.runtime_observable_context_token,);- let stale_context = v8::Local::new(scope, &stale.context);if self.child_window_proxy_frame_is_current(handle, &stale.frame_id) {stale_context.detach_global();}diff --git a/moli-renderer-v8/src/script_vm.rs b/moli-renderer-v8/src/script_vm.rsindex 76a465409..1b6d08834 100644--- a/moli-renderer-v8/src/script_vm.rs+++ b/moli-renderer-v8/src/script_vm.rs@@ -2678,8 +2678,16 @@ impl ScriptVm {}fn clear_context_wrapper_caches_for_context_teardown(&mut self) {+ // A contentWindow can be exposed before its owner turn promotes the+ // realm into child_frame_realm_store. Retire these contexts too: their+ // intrinsic Global handles otherwise keep the Context and native host+ // alive after the Document is replaced.+ let prebootstrapped_contexts =+ std::mem::take(&mut *self.prebootstrapped_child_default_contexts.borrow_mut());let mut context_ptrs: Vec<*const v8::Global<v8::Context>> = Vec::with_capacity(- 1 + self.page_isolated_world_contexts.len() + self.child_frame_realm_store.len(),+ 1 + self.page_isolated_world_contexts.len()+ + self.child_frame_realm_store.len()+ + prebootstrapped_contexts.len(),);context_ptrs.push(&self.page_default_context as *const _);context_ptrs.extend(@@ -2692,6 +2700,11 @@ impl ScriptVm {.values().map(|world| &world.context as *const _),);+ context_ptrs.extend(+ prebootstrapped_contexts+ .values()+ .map(|world| &world.context as *const _),+ );for (index, context_ptr) in context_ptrs.into_iter().enumerate() {self.clear_context_wrapper_cache_for_context_ptr(context_ptr, index == 0);diff --git a/moli-renderer-v8/src/script_vm/tests/extracted/realms_and_teardown.rs b/moli-renderer-v8/src/script_vm/tests/extracted/realms_and_teardown.rsindex 699e240de..7f9dcf1f0 100644--- a/moli-renderer-v8/src/script_vm/tests/extracted/realms_and_teardown.rs+++ b/moli-renderer-v8/src/script_vm/tests/extracted/realms_and_teardown.rs@@ -1,5 +1,89 @@use super::*;+#[test]+fn unpromoted_child_realm_releases_native_host_on_document_teardown() {+ let vm = new_vm_with_unpromoted_child_realm_for_teardown_test();+ let weak_host = vm.context_host_weak_for_test();+ let isolate = vm.renderer_document_isolate.clone();+ drop(vm);+ for _ in 0..2 {+ isolate.with_renderer_document_isolate_mut(|isolate| isolate.low_memory_notification());+ }+ assert!(+ weak_host.upgrade().is_none(),+ "an iframe awaiting realm promotion must not root its retired native Document"+ );+}++fn new_vm_with_unpromoted_child_realm_for_teardown_test() -> StandaloneScriptVmHarness {+ let mut vm = new_storage_test_vm("https://unpromoted-child-teardown.test/");+ vm.eval(+ r#"+ const root = document.documentElement || document.appendChild(document.createElement('html'));+ const body = document.body || root.appendChild(document.createElement('body'));+ const frame = document.createElement('iframe');+ body.appendChild(frame);+ typeof frame.contentWindow.Function+ "#,+ )+ .expect("exposing an iframe should prebootstrap its realm");+ assert_eq!(vm.prebootstrapped_child_default_contexts.borrow().len(), 1);+ assert_eq!(vm.child_frame_realm_store.len(), 0);+ vm+}++#[test]+fn retained_unpromoted_child_realm_keeps_native_values_until_the_last_v8_reference() {+ let mut vm = new_vm_with_unpromoted_child_realm_for_teardown_test();+ vm.eval(+ r#"+ frame.contentWindow.Function(`+ globalThis.savedNode = document.createElement('p');+ savedNode.textContent = 'child';+ (document.body || document.documentElement || document).appendChild(savedNode);+ `)()+ "#,+ )+ .expect("an unpromoted child realm should expose native DOM values");+ let weak_host = vm.context_host_weak_for_test();+ let isolate = vm.renderer_document_isolate.clone();+ let context = vm+ .prebootstrapped_child_default_contexts+ .borrow()+ .values()+ .next()+ .unwrap()+ .context+ .clone();+ drop(vm);+ for _ in 0..2 {+ isolate.with_renderer_document_isolate_mut(|isolate| isolate.low_memory_notification());+ }+ assert!(weak_host.upgrade().is_some());+ let value = isolate.with_renderer_document_isolate_mut(|isolate| {+ let scope = std::pin::pin!(v8::HandleScope::new(isolate));+ let scope = &mut scope.init();+ let context = v8::Local::new(scope, &context);+ let scope = &mut v8::ContextScope::new(scope, context);+ let source = crate::util::v8str(scope, "savedNode.textContent = 'retained-child'");+ let script =+ v8::Script::compile(scope, source, None).expect("retained child realm compiles");+ crate::script_execution::execute_compiled_script(scope, script)+ .expect("retained child native values remain usable")+ .to_rust_string_lossy(scope)+ });+ assert_eq!(value, "retained-child");+ isolate.with_renderer_document_isolate_mut(|isolate| {+ drop(context);+ isolate.low_memory_notification();+ });+ isolate.with_renderer_document_isolate_mut(|isolate| isolate.low_memory_notification());+ assert!(+ weak_host.upgrade().is_none(),+ "the last child realm reference must release its retired native Document"+ );+}+fn capture_initial_environment_for_gc_test(vm: &StandaloneScriptVmHarness,) -> crate::script_vm::ScriptVmCapturedDocumentEnvironment {