Skip to content

The headless browser for AI agents and web scraping

fix(browser): bound iframe discovery microtasks with the watchdog

SGavrlc8b209d0d9ef
diff --git a/crates/obscura-browser/src/page.rs b/crates/obscura-browser/src/page.rs
index 15a14933..a9d1ca72 100644
--- a/crates/obscura-browser/src/page.rs
+++ b/crates/obscura-browser/src/page.rs
@@ -3702,11 +3702,9 @@ impl Page {
for _ in 0..ROUNDS {
if let Some(js) = &mut self.js {
- let _ = tokio::time::timeout(
- tokio::time::Duration::from_millis(ROUND_MS),
- js.run_event_loop(),
- )
- .await;
+ // A Tokio timeout cannot interrupt synchronous microtasks.
+ // Use the same cooperative, watchdog-bounded pump as settling.
+ let _ = js.run_event_loop_bounded(ROUND_MS).await;
}
if !self.advance_frames().await {
break;
@@ -7547,6 +7545,31 @@ mod tests {
);
}
+ #[tokio::test(flavor = "current_thread")]
+ async fn frame_discovery_bounds_a_microtask_storm() {
+ let mut page = import_map_test_page(
+ "frame-discovery-microtasks",
+ "https://example.test/",
+ "<html><body><iframe></iframe></body></html>",
+ );
+ page.js.as_mut().unwrap().execute_script(
+ "frame-discovery-busy-task",
+ "setTimeout(() => {\
+ const end = Date.now() + 7000;\
+ const spin = () => { if (Date.now() < end) Promise.resolve().then(spin); };\
+ spin();\
+ }, 0);",
+ ).unwrap();
+
+ let started = std::time::Instant::now();
+ page.build_document_frames().await;
+ assert!(started.elapsed() < std::time::Duration::from_millis(6500),
+ "frame discovery must bound synchronous microtasks, not rely on a Tokio timeout: {:?}",
+ started.elapsed());
+ assert_eq!(page.js.as_mut().unwrap().evaluate("1 + 1").unwrap(), serde_json::json!(2.0),
+ "the page must remain usable after an overrun");
+ }
+
#[tokio::test(flavor = "current_thread")]
async fn load_delaying_script_progresses_through_continuously_ready_timer_work() {
let (base, requests) = spawn_delayed_classic_script_server(