Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(fetch): bound diagnostics after readiness failures

ldm0b9e1ee4db56b
diff --git a/moli/src/app.rs b/moli/src/app.rs
index 54ca9a0b5..9fb774be4 100644
--- a/moli/src/app.rs
+++ b/moli/src/app.rs
@@ -9,6 +9,7 @@ use std::{
path::Path,
process::ExitCode,
sync::Arc,
+ time::Duration,
};
use crate::{
@@ -28,6 +29,8 @@ use moli_protocol_server::ProtocolServer;
use self::readiness::ReadinessPlan;
+const READINESS_FAILURE_DIAGNOSTICS_TIMEOUT: Duration = Duration::from_millis(250);
+
pub async fn run_from_env() -> Result<()> {
let cli = Cli::parse_from(normalize_args_for_compat(std::env::args_os()));
let config = AppConfig::from_cli(&cli).context("failed to build app configuration")?;
@@ -154,13 +157,28 @@ pub async fn run_cli_with_config<W: Write>(
}
.await;
if let Err(error) = readiness_result {
- if (args.fail || args.fail_with_body)
- && let Err(snapshot_error) = page.page_diagnostics_snapshot_async().await
- {
- tracing::debug!(
- error = %snapshot_error,
- "failed to refresh main-document state after readiness failure"
- );
+ if args.fail || args.fail_with_body {
+ // Keep diagnostics bounded so Page close can interrupt a
+ // renderer still occupied by the failed readiness script.
+ match tokio::time::timeout(
+ READINESS_FAILURE_DIAGNOSTICS_TIMEOUT,
+ page.page_diagnostics_snapshot_async(),
+ )
+ .await
+ {
+ Ok(Ok(_)) => {}
+ Ok(Err(snapshot_error)) => {
+ tracing::debug!(
+ error = %snapshot_error,
+ "failed to refresh main-document state after readiness failure"
+ );
+ }
+ Err(_) => {
+ tracing::debug!(
+ "timed out refreshing main-document state after readiness failure; using cached state"
+ );
+ }
+ }
}
let error = with_http_status_context(
error,
diff --git a/moli/tests/fetch_cli/http_fail.rs b/moli/tests/fetch_cli/http_fail.rs
index d195719f7..a9e41ec17 100644
--- a/moli/tests/fetch_cli/http_fail.rs
+++ b/moli/tests/fetch_cli/http_fail.rs
@@ -1,4 +1,4 @@
-use anyhow::{Result, ensure};
+use anyhow::{Context, Result, ensure};
use axum::{
Router,
extract::Path,
@@ -732,6 +732,59 @@ async fn http_failure_modes_report_status_when_page_readiness_times_out() -> Res
Ok(())
}
+#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
+async fn http_failure_modes_bound_diagnostics_for_busy_wait_scripts() -> Result<()> {
+ let server = HttpFailureFixture::spawn().await?;
+ for status in [200, 403] {
+ let path = format!("/status/{status}");
+ for flag in [None, Some("--fail"), Some("--fail-with-body")] {
+ let mut command = Command::new(env!("CARGO_BIN_EXE_moli"));
+ command
+ .env("MOLI_LAYOUT", "false")
+ .args([
+ "fetch",
+ "--http-no-proxy",
+ "*",
+ "--redirect-wait-ms",
+ "0",
+ "--wait-until",
+ "load",
+ "--timeout",
+ "1000",
+ "--wait-script",
+ "() => { for (;;) {} }",
+ ])
+ .args(flag)
+ .arg(format!("{}{path}", server.base_url));
+ let output = tokio::task::spawn_blocking(move || {
+ run_command_with_process_timeout(command, Duration::from_secs(4))
+ })
+ .await?
+ .with_context(|| format!("busy readiness script: status {status}, flag {flag:?}"))?;
+ if status == 403 && flag == Some("--fail") {
+ server.assert_http_failure(&output, status, &path);
+ } else {
+ assert_eq!(output.status.code(), Some(1), "{output:?}");
+ }
+ assert!(output.stdout.is_empty(), "{output:?}");
+ let stderr = String::from_utf8_lossy(&output.stderr);
+ assert!(
+ stderr.contains(
+ "fetch readiness timed out after 1000 ms while waiting for a script to become truthy"
+ ),
+ "{stderr}"
+ );
+ assert_eq!(
+ stderr.contains("HTTP status 403"),
+ status == 403 && flag.is_some(),
+ "{stderr}"
+ );
+ assert_eq!(stderr.lines().count(), 2, "{stderr}");
+ }
+ }
+ Ok(())
+}
+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn http_failure_modes_use_redirect_status_when_readiness_fails() -> Result<()> {
let server = HttpFailureFixture::spawn().await?;