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(fetch): preserve cancellation across response selection
ldm091544b66941d
diff --git a/Cargo.lock b/Cargo.lockindex 6cd306aca..8989fbfe3 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -2870,6 +2870,7 @@ dependencies = ["tokio","tokio-rustls","tokio-tungstenite",+ "tokio-util","tracing","url","wildcard",@@ -5857,6 +5858,7 @@ dependencies = ["bytes","futures-core","futures-sink",+ "futures-util","pin-project-lite","tokio",]diff --git a/moli-fetch/Cargo.toml b/moli-fetch/Cargo.tomlindex 4aacbbb8f..654074764 100644--- a/moli-fetch/Cargo.toml+++ b/moli-fetch/Cargo.toml@@ -29,6 +29,7 @@ moli-web-mime = { path = "../moli-web-mime" }parking_lot = "0.12"strum = { version = "0.28.0", features = ["derive"] }tokio = { version = "1.51.0", features = ["rt", "sync"] }+tokio-util = { version = "0.7.18", features = ["rt"] }tracing = "0.1.41"url = "2.5.7"wildcard = "0.3.0"diff --git a/moli-fetch/src/cancellation.rs b/moli-fetch/src/cancellation.rsindex 79f9236c0..280d30919 100644--- a/moli-fetch/src/cancellation.rs+++ b/moli-fetch/src/cancellation.rs@@ -1,9 +1,10 @@use std::sync::{Arc,- atomic::{AtomicBool, AtomicU8, Ordering},+ atomic::{AtomicU8, Ordering},};use parking_lot::Mutex;+use tokio_util::sync::CancellationToken;const RESPONSE_PENDING: u8 = 0;const RESPONSE_COMMITTED: u8 = 1;@@ -42,8 +43,7 @@ impl ResponseProgress {#[derive(Debug, Default)]struct FetchLifecycleState {- cancel_requested: AtomicBool,- parent: Option<Arc<FetchLifecycleState>>,+ cancellation: CancellationToken,// This lock protects selection, while the selected token arbitrates// transport completion and consumer cancellation without taking the lock.response_progress: Mutex<ResponseProgress>,@@ -60,7 +60,7 @@ impl FetchCancelHandle {}pub fn cancel(&self) {- self.state.cancel_requested.store(true, Ordering::SeqCst);+ self.state.cancellation.cancel();}/// Cancels only if this response's transport result has not committed.@@ -76,16 +76,7 @@ impl FetchCancelHandle {}pub fn is_cancelled(&self) -> bool {- let mut state = self.state.as_ref();- loop {- if state.cancel_requested.load(Ordering::SeqCst) {- return true;- }- match state.parent.as_deref() {- Some(parent) => state = parent,- None => return false,- }- }+ self.state.cancellation.is_cancelled()}/// Creates cancellation authority for one preflight or redirect hop.@@ -94,7 +85,7 @@ impl FetchCancelHandle {pub fn child_for_subrequest(&self) -> Self {Self {state: Arc::new(FetchLifecycleState {- parent: Some(self.state.clone()),+ cancellation: self.state.cancellation.child_token(),..FetchLifecycleState::default()}),}@@ -102,12 +93,19 @@ impl FetchCancelHandle {/// Selects the accepted final response's transport progress. This shares/// already recorded and future completion facts, without retaining the- /// child's cancellation state (which itself retains the parent). Call only- /// after the accepted response's headers arrive: selection is a snapshot,- /// and starting another transfer on the child does not change this choice.+ /// child's cancellation authority. Call only after the accepted response's+ /// headers arrive: selection is a snapshot, and starting another transfer+ /// on the child does not change this choice.+ /// Once cancellation wins, later selection cannot replace its terminal+ /// progress with a pending or already committed response.pub fn adopt_response_progress(&self, response: &Self) {let progress = response.state.response_progress.lock().clone();- *self.state.response_progress.lock() = progress;+ let mut selected = self.state.response_progress.lock();+ // Serialize this check with cancel_if_response_pending(), which sets+ // cancellation while holding the same selection lock.+ if !self.is_cancelled() {+ *selected = progress;+ }}/// Returns whether transport facts already determine this response's@@ -121,7 +119,11 @@ impl FetchCancelHandle {/// progress happens to be selected when the callback eventually runs.pub(crate) fn begin_response_progress(&self) -> ResponseProgress {let progress = ResponseProgress::default();- *self.state.response_progress.lock() = progress.clone();+ let mut selected = self.state.response_progress.lock();+ if self.is_cancelled() {+ progress.cancel_if_pending();+ }+ *selected = progress.clone();progress}@@ -158,15 +160,52 @@ mod tests {let parent = FetchCancelHandle::new();let first = parent.child_for_subrequest();let second = parent.child_for_subrequest();- first.cancel();+ first.clone().cancel();assert!(first.is_cancelled());assert!(!parent.is_cancelled());assert!(!second.is_cancelled());parent.cancel();assert!(second.is_cancelled());+ assert!(parent.child_for_subrequest().is_cancelled());assert!(second.child_for_subrequest().is_cancelled());}+ #[test]+ fn cancellation_reaches_descendants_after_an_intermediate_handle_is_dropped() {+ let parent = FetchCancelHandle::new();+ let child = parent.child_for_subrequest();+ let grandchild = child.child_for_subrequest();+ drop(child);++ assert!(!grandchild.is_cancelled());+ parent.cancel();+ assert!(grandchild.is_cancelled());+ assert!(grandchild.child_for_subrequest().is_cancelled());+ }++ #[test]+ fn concurrent_child_creation_inherits_parent_cancellation() {+ for _ in 0..64 {+ let parent = FetchCancelHandle::new();+ let ready = std::sync::Barrier::new(2);+ std::thread::scope(|scope| {+ let cancellation = scope.spawn(|| {+ ready.wait();+ parent.cancel();+ });+ ready.wait();+ let child = parent.child_for_subrequest();+ let grandchild = child.child_for_subrequest();+ cancellation.join().unwrap();++ // CancellationToken guarantees propagation to every child+ // once cancel() returns, including concurrently created hops.+ assert!(child.is_cancelled());+ assert!(grandchild.is_cancelled());+ });+ }+ }+#[test]fn only_selected_response_progress_commits_the_parent() {for complete_before_selection in [false, true] {@@ -236,6 +275,78 @@ mod tests {assert!(child.is_cancelled());}+ #[test]+ fn adoption_after_cancellation_cannot_replace_cancelled_progress() {+ for complete_before_selection in [false, true] {+ let parent = FetchCancelHandle::new();+ let child = parent.child_for_subrequest();+ let writer = child.begin_response_progress();++ assert!(parent.cancel_if_response_pending());+ if complete_before_selection {+ writer.commit();+ }+ parent.adopt_response_progress(&child);+ writer.commit();++ assert!(parent.is_cancelled());+ assert!(!parent.response_completion_is_committed());+ }+ }++ #[test]+ fn adoption_respects_direct_and_inherited_cancellation() {+ let root = FetchCancelHandle::new();+ let parent = root.child_for_subrequest();+ let response = parent.child_for_subrequest();+ let writer = response.begin_response_progress();+ root.cancel();+ root.adopt_response_progress(&response);+ parent.adopt_response_progress(&response);+ writer.commit();++ assert!(!root.response_completion_is_committed());+ assert!(parent.is_cancelled());+ assert!(!parent.response_completion_is_committed());+ }++ #[test]+ fn starting_transfer_after_cancellation_cannot_revive_completion() {+ let parent = FetchCancelHandle::new();+ let child = parent.child_for_subrequest();+ assert!(parent.cancel_if_response_pending());++ parent.begin_response_progress().commit();+ child.begin_response_progress().commit();++ assert!(parent.is_cancelled());+ assert!(!parent.response_completion_is_committed());+ assert!(child.is_cancelled());+ assert!(!child.response_completion_is_committed());+ }++ #[test]+ fn concurrent_adoption_completion_and_cancellation_have_one_winner() {+ for _ in 0..64 {+ let parent = FetchCancelHandle::new();+ let child = parent.child_for_subrequest();+ let writer = child.begin_response_progress();+ let ready = std::sync::Barrier::new(2);+ std::thread::scope(|scope| {+ let selection = scope.spawn(|| {+ ready.wait();+ parent.adopt_response_progress(&child);+ writer.commit();+ });+ ready.wait();+ let cancelled = parent.cancel_if_response_pending();+ selection.join().unwrap();+ assert_eq!(parent.response_completion_is_committed(), !cancelled);+ assert_eq!(parent.is_cancelled(), cancelled);+ });+ }+ }+#[test]fn completion_and_consumer_cancellation_have_one_winner() {for _ in 0..64 {diff --git a/moli-fetch/src/runtime.rs b/moli-fetch/src/runtime.rsindex 3db968caf..d2150912a 100644--- a/moli-fetch/src/runtime.rs+++ b/moli-fetch/src/runtime.rs@@ -864,6 +864,7 @@ impl RuntimeOwner {&self,mut job: RuntimeJob,) -> std::result::Result<JobOutcome, (RuntimeResponseTx, anyhow::Error)> {+ job.response_progress = job.cancel_handle.begin_response_progress();if job.cancel_handle.is_cancelled() {return Err((job.response_tx,@@ -1455,6 +1456,7 @@ impl RuntimeOwner {request_cookie_report,response,);+ job.response_progress.commit();return Ok(JobOutcome::Complete(job.response_tx,Box::new(CompletedBufferedResponse::Raw(response)),@@ -1620,6 +1622,9 @@ impl RuntimeOwner {response.redirected = !job.request.redirect_chain.is_empty();response.redirect_chain = job.request.redirect_chain;+ // Only the selected final response commits this buffered job. Redirect+ // and auth retries must leave consumer cancellation available.+ job.response_progress.commit();Ok(JobOutcome::Complete(job.response_tx,Box::new(CompletedBufferedResponse::Raw(response)),@@ -2534,6 +2539,7 @@ struct RuntimeJob {origin_key: Option<CurlOriginKey>,response_tx: RuntimeResponseTx,cancel_handle: FetchCancelHandle,+ response_progress: ResponseProgress,http_version: RequestHttpVersion,empty_http_https_upgrade_attempted: bool,client_hint_navigation_restarts: SharedNavigationClientHintRestarts,@@ -2546,6 +2552,7 @@ impl RuntimeJob {cancel_handle: FetchCancelHandle,) -> Self {let origin_key = origin_key(&request);+ let response_progress = cancel_handle.response_progress();Self {current_url: request.url.clone(),current_cookie_context: request.network_cookie_context(),@@ -2554,6 +2561,7 @@ impl RuntimeJob {origin_key,response_tx,cancel_handle,+ response_progress,http_version: RequestHttpVersion::PreferHttp2,empty_http_https_upgrade_attempted: false,client_hint_navigation_restarts: Arc::new(Mutex::new(BTreeSet::new())),diff --git a/moli-fetch/src/tests/mod.rs b/moli-fetch/src/tests/mod.rsindex 040180821..b9a706ade 100644--- a/moli-fetch/src/tests/mod.rs+++ b/moli-fetch/src/tests/mod.rs@@ -4,6 +4,7 @@ mod header_bytes;mod header_eof;mod mixed_transport;mod request_security;+mod response_progress;mod support;mod tls_credentials;mod websocket_transport;diff --git a/moli-fetch/src/tests/response_progress.rs b/moli-fetch/src/tests/response_progress.rsnew file mode 100644index 000000000..d27984ff2--- /dev/null+++ b/moli-fetch/src/tests/response_progress.rs@@ -0,0 +1,101 @@+use super::*;++#[tokio::test]+async fn buffered_response_commits_completion_before_delivery() -> Result<()> {+ let responses = [+ ScriptedResponse::ok("body"),+ ScriptedResponse::status(204, "No Content"),+ ScriptedResponse::status(302, "Found")+ .with_header("Location", "/final")+ .with_body("redirect body"),+ ];+ let server = ScriptedHttpServer::spawn(responses.iter().chain(&responses).cloned().collect());+ let client = FetchClient::new(&FetchConfig::default(), new_shared_browser_cookie_store());+ for use_callback in [false, true] {+ for (status, body) in [(200, "body"), (204, ""), (302, "redirect body")] {+ let parent = FetchCancelHandle::new();+ let child = parent.child_for_subrequest();+ let request = Request::get(&server.url())?.with_follow_redirects(false);+ let response = if use_callback {+ let (tx, rx) = oneshot::channel();+ let observer = child.clone();+ client.fetch_with_cancel_callback(request, child.clone(), move |result| {+ // The token must be committed before invoking consumers.+ assert!(observer.response_completion_is_committed());+ let _ = tx.send(result);+ })?;+ rx.await??+ } else {+ client.fetch_with_cancel(request, child.clone()).await?+ };+ parent.adopt_response_progress(&child);+ assert_eq!(response.status, status);+ assert_eq!(response.body_text(), body);+ assert!(parent.response_completion_is_committed());+ assert!(!parent.cancel_if_response_pending());+ assert!(!parent.is_cancelled());+ }+ }+ server.shutdown();+ Ok(())+}++#[tokio::test]+async fn buffered_redirect_body_wait_is_cancellable_without_committing_completion() -> Result<()> {+ let listener = TcpListener::bind("127.0.0.1:0").await?;+ let url = format!("http://{}/redirect", listener.local_addr()?);+ let (headers_tx, headers_rx) = oneshot::channel();+ let server = tokio::spawn(async move {+ let (mut stream, _) = listener.accept().await?;+ let mut request = Vec::new();+ let mut byte = [0_u8; 1];+ while !request.ends_with(b"\r\n\r\n") {+ anyhow::ensure!(stream.read(&mut byte).await? != 0, "incomplete request");+ request.push(byte[0]);+ }+ stream+ .write_all(+ concat!(+ "HTTP/1.1 302 Found\r\n",+ "Location: /final\r\n",+ "Content-Length: 100\r\n",+ "Connection: close\r\n\r\n",+ )+ .as_bytes(),+ )+ .await?;+ let _ = headers_tx.send(());+ assert_eq!(stream.read(&mut byte).await?, 0);+ Ok::<_, anyhow::Error>(())+ });+ let client = FetchClient::new(&FetchConfig::default(), new_shared_browser_cookie_store());+ let cancel = FetchCancelHandle::new();+ let (tx, mut rx) = oneshot::channel();+ client.fetch_with_cancel_callback(+ Request::get(&url)?.with_follow_redirects(false),+ cancel.clone(),+ move |result| {+ let _ = tx.send(result);+ },+ )?;+ tokio::time::timeout(Duration::from_secs(5), headers_rx).await??;+ // Known compatibility limitation: the buffered transport retains redirect+ // bodies, so headers alone cannot deliver its materialized response. Keep+ // cancellation working until that fallback can retire bodies at headers.+ // See docs/inner/fetch-buffered-redirect-limitations.md.+ assert!(+ tokio::time::timeout(Duration::from_millis(100), &mut rx)+ .await+ .is_err()+ );+ assert!(!cancel.response_completion_is_committed());+ assert!(cancel.cancel_if_response_pending());+ let error = tokio::time::timeout(Duration::from_secs(5), rx)+ .await??+ .unwrap_err();+ assert!(error.is::<crate::FetchCancelled>());+ assert!(cancel.is_cancelled());+ assert!(!cancel.response_completion_is_committed());+ tokio::time::timeout(Duration::from_secs(5), server).await???;+ Ok(())+}diff --git a/moli-renderer-v8/src/network_host/async_fetch.rs b/moli-renderer-v8/src/network_host/async_fetch.rsindex c885e9d37..b60a982be 100644--- a/moli-renderer-v8/src/network_host/async_fetch.rs+++ b/moli-renderer-v8/src/network_host/async_fetch.rs@@ -240,6 +240,34 @@ async fn fetch_browser_subresource_with_manual_preflight_redirects(preflight_request_headers: Vec<(String, String)>,preflight_observer: Option<&CorsPreflightNetworkObserver>,) -> Result<NetworkFetchResult<Response>, String> {+ if !request.auth_requires_buffered_transport()+ && request.redirect_mode == RequestRedirectMode::Follow+ {+ // Materialize only the accepted final response. Waiting for buffered+ // intermediate bodies here lets an unfinished redirect block the next+ // hop and its preflight indefinitely.+ let observed = fetch_browser_subresource_raw_stream_with_manual_preflight_redirects(+ &loader,+ request,+ cancel_handle,+ preflight_request_headers,+ preflight_observer,+ )+ .await?;+ let (response, observation_journal) = observed.into_parts_with_observation_journal();+ let response = response+ .into_lossy_materialized_text_response()+ .await+ .map_err(format_network_error)?;+ return Ok(NetworkFetchResult::with_observation_journal(+ response,+ observation_journal,+ ));+ }++ // Auth challenge-response and manual/error redirect compatibility still+ // use the buffered collector, which waits for each response's entire body.+ // See docs/inner/fetch-buffered-redirect-limitations.md.let mut redirects = ManualCorsRedirectState::new(request, preflight_request_headers);let cancel_handle = cancel_handle.unwrap_or_default();@@ -1280,7 +1308,7 @@ mod tests {}#[tokio::test(flavor = "multi_thread")]- async fn unsafe_xhr_redirect_preflights_next_origin_in_buffered_path() -> Result<()> {+ async fn buffered_xhr_discards_unfinished_redirect_and_preflight_bodies() -> Result<()> {let listener = TcpListener::bind("127.0.0.1:0").await?;let addr = listener.local_addr()?;let source_origin = format!("http://127.0.0.1:{}", addr.port());@@ -1292,11 +1320,14 @@ mod tests {let request = read_http_request_text(&mut redirect).await.unwrap();assert!(request.starts_with("POST /start HTTP/1.1"));assert!(request.to_ascii_lowercase().contains("x-challenge: yes"));+ let mut request_body = [0_u8; 7];+ redirect.read_exact(&mut request_body).await.unwrap();+ assert_eq!(&request_body, b"payload");let redirect_response = format!(concat!("HTTP/1.1 307 Temporary Redirect\r\n","Location: {}\r\n",- "Content-Length: 0\r\n",+ "Content-Length: 100\r\n","Connection: close\r\n","\r\n"),@@ -1306,7 +1337,7 @@ mod tests {.write_all(redirect_response.as_bytes()).await.unwrap();- drop(redirect);+ // Neither body is sent. The next requests must start from headers.let (mut preflight, _) = listener.accept().await.unwrap();let request = read_http_request_text(&mut preflight).await.unwrap();@@ -1316,11 +1347,11 @@ mod tests {assert!(request_lower.contains("access-control-request-headers: x-challenge"));let preflight_response = format!(concat!(- "HTTP/1.1 204 No Content\r\n",+ "HTTP/1.1 200 OK\r\n","Access-Control-Allow-Origin: {}\r\n","Access-Control-Allow-Methods: POST\r\n","Access-Control-Allow-Headers: x-challenge\r\n",- "Content-Length: 0\r\n",+ "Content-Length: 100\r\n","Connection: close\r\n","\r\n"),@@ -1330,12 +1361,16 @@ mod tests {.write_all(preflight_response.as_bytes()).await.unwrap();- drop(preflight);let (mut final_response, _) = listener.accept().await.unwrap();let request = read_http_request_text(&mut final_response).await.unwrap();assert!(request.starts_with("POST /final HTTP/1.1"));assert!(request.to_ascii_lowercase().contains("x-challenge: yes"));+ final_response.read_exact(&mut request_body).await.unwrap();+ assert_eq!(&request_body, b"payload");+ let mut byte = [0_u8; 1];+ assert_eq!(redirect.read(&mut byte).await.unwrap(), 0);+ assert_eq!(preflight.read(&mut byte).await.unwrap(), 0);let response = format!(concat!("HTTP/1.1 200 OK\r\n",@@ -1367,13 +1402,17 @@ mod tests {.with_credentials_mode(RequestCredentialsMode::SameOrigin).with_browser_request_metadata(BrowserRequestMetadata::Xhr);- let response = fetch_browser_subresource_with_preflight_headers(- loader,- request,- Some(FetchCancelHandle::new()),- request_headers,+ let cancel = FetchCancelHandle::new();+ let response = tokio::time::timeout(+ Duration::from_secs(5),+ fetch_browser_subresource_with_preflight_headers(+ loader,+ request,+ Some(cancel.clone()),+ request_headers,+ ),)- .await+ .await?.map_err(anyhow::Error::msg)?;assert_eq!(response.final_url.as_str(), target_url);assert_eq!(response.body_text(), "buffered-xhr");@@ -1382,8 +1421,11 @@ mod tests {assert_eq!(response.redirect_chain[0].from_url, request_url);assert_eq!(response.redirect_chain[0].to_url.as_str(), target_url);assert_eq!(response.redirect_chain[0].status, 307);+ assert!(cancel.response_completion_is_committed());+ assert!(!cancel.cancel_if_response_pending());+ assert!(!cancel.is_cancelled());- server.await?;+ tokio::time::timeout(Duration::from_secs(5), server).await??;Ok(())}