The headless browser for AI agents and web scraping
fix(js): clear borrowed frame annexes before isolate teardown
SGavrlb6ab95e87380
diff --git a/crates/obscura-js/src/frame.rs b/crates/obscura-js/src/frame.rsindex 0785c374..1a07a324 100644--- a/crates/obscura-js/src/frame.rs+++ b/crates/obscura-js/src/frame.rs@@ -434,6 +434,20 @@ mod tests {runtime}+ #[test]+ fn fetched_frame_contexts_survive_runtime_teardown_under_gc_stress() {+ crate::set_v8_flags("--stress-compaction --stress-marking=1");+ for _ in 0..10 {+ let mut parent = page("https://parent.example/page", "<html><body>Parent</body></html>");+ let frame = FrameRealm::new(&mut parent, 1, 0, "https://parent.example/frame",+ "<html><body>Child</body></html>").expect("frame realm");+ assert_eq!(frame.evaluate(&mut parent, "document.body.textContent").unwrap(),+ serde_json::json!("Child"));+ drop(frame);+ drop(parent);+ }+ }+#[test]fn frame_has_its_own_realm_dom_and_origin() {let mut parent = page(diff --git a/crates/obscura-js/src/ops.rs b/crates/obscura-js/src/ops.rsindex 4c7c5517..d1863686 100644--- a/crates/obscura-js/src/ops.rs+++ b/crates/obscura-js/src/ops.rs@@ -5868,6 +5868,14 @@ fn op_register_document_realm(context.set_embedder_data(DOCUMENT_MEMBERS_SLOT, members.into());let id = v8::Integer::new_from_unsigned(scope, frame_id);context.set_embedder_data(DOCUMENT_OWNER_SLOT, id.into());+ if context != scope.get_current_context() {+ // Frame contexts own no Rust typed slots. Discard the empty annex+ // created by these setters while the isolate is alive (#1161).+ // Traced native embedder fields remain intact; deno_core cleans up+ // the main context's annex itself. The shared op runs in main, so+ // compare native identity, not the script-provided frame id.+ context.clear_all_slots();+ }}}@@ -5933,6 +5941,7 @@ fn op_initial_frame(context.set_aligned_pointer_in_embedder_data(index, pointer);}}+ context.clear_all_slots();context.set_security_token(parent_context.get_security_token(scope));let mut child = ObscuraState::new();#[cfg(feature = "render")]diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rsindex 1e4e66ec..b5669d75 100644--- a/crates/obscura-js/src/runtime.rs+++ b/crates/obscura-js/src/runtime.rs@@ -802,6 +802,10 @@ impl ObscuraJsRuntime {realm_ctx.set_aligned_pointer_in_embedder_data(CONTEXT_STATE_SLOT_INDEX, cs);realm_ctx.set_aligned_pointer_in_embedder_data(MODULE_MAP_SLOT_INDEX, mm);}+ // These borrowed native fields need no Rust slot annex. Leaving its+ // weak finalizer alive until isolate disposal can access freed memory+ // in rusty_v8; clearing it now preserves the native fields (#1161).+ realm_ctx.clear_all_slots();}pub(crate) fn share_ops_with_realm(@@ -5019,6 +5023,67 @@ mod tests {]));}+ #[test]+ fn retained_initial_frame_contexts_survive_runtime_teardown_under_gc_stress() {+ crate::set_v8_flags("--stress-compaction --stress-marking=1");+ for _ in 0..10 {+ let mut rt = setup_runtime("<html><body></body></html>");+ assert_eq!(rt.evaluate(r#"(() => {+ globalThis.retainedFrames = [];+ for (let i = 0; i < 4; i++) {+ const frame = document.createElement('iframe');+ document.body.appendChild(frame);+ const child = frame.contentWindow;+ child.document.body.textContent = 'retained';+ retainedFrames.push(child);+ frame.remove();+ }+ return retainedFrames.map(child => child.document.body.textContent);+ })()"#).unwrap(), serde_json::json!(["retained", "retained", "retained", "retained"]));+ drop(rt);+ }+ }++ #[test]+ fn document_realm_registration_preserves_main_context_rust_slots() {+ let mut rt = setup_runtime("<html><body></body></html>");+ rt.expose_ops_for_tests();+ {+ let mut entered = rt.runtime();+ let context = entered.main_context();+ v8::scope_with_context!(scope, entered.v8_isolate(), context);+ scope.get_current_context().set_slot(Rc::new(String::from("main context state")));+ }+ rt.evaluate("__obscura_test_ops.op_register_document_realm({}, 123)").unwrap();+ let mut entered = rt.runtime();+ let context = entered.main_context();+ v8::scope_with_context!(scope, entered.v8_isolate(), context);+ assert_eq!(scope.get_current_context().get_slot::<String>().as_deref().map(String::as_str),+ Some("main context state"));+ }++ #[test]+ fn document_realm_registration_with_zero_id_survives_teardown_under_gc_stress() {+ crate::set_v8_flags("--stress-compaction --stress-marking=1");+ for _ in 0..10 {+ let mut rt = setup_runtime("<html><body></body></html>");+ rt.expose_ops_for_tests();+ assert_eq!(rt.evaluate(r#"(() => {+ globalThis.retainedFrames = [];+ for (let i = 0; i < 4; i++) {+ const frame = document.createElement('iframe');+ document.body.appendChild(frame);+ const child = frame.contentWindow;+ __obscura_test_ops.op_register_document_realm(child.eval('({})'), 0);+ retainedFrames.push(child);+ frame.remove();+ }+ return retainedFrames.length;+ })()"#).unwrap(), serde_json::json!(4.0));+ drop(rt);+ }+ }+#[test]fn initial_blank_iframe_eval_uses_the_child_realm() {let mut rt = setup_runtime("<html><body></body></html>");