Skip to content

armgit / moli

Public Imported from GitHub
0

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

test(fetch): bound HTTP failure CLI subprocesses

ldm0755404696591
diff --git a/moli/tests/fetch_cli/http_fail.rs b/moli/tests/fetch_cli/http_fail.rs
index 23c9860c8..d195719f7 100644
--- a/moli/tests/fetch_cli/http_fail.rs
+++ b/moli/tests/fetch_cli/http_fail.rs
@@ -1,4 +1,4 @@
-use anyhow::Result;
+use anyhow::{Result, ensure};
use axum::{
Router,
extract::Path,
@@ -11,9 +11,9 @@ use clap::Parser;
use moli::{app, cli::Cli};
use serde_json::{Value, json};
use std::{
- io::{self, Write},
- process::{Command, ExitCode, Output},
- time::Duration,
+ io::{self, Read, Write},
+ process::{Child, Command, ExitCode, Output, Stdio},
+ time::{Duration, Instant},
};
use tokio::{
io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader},
@@ -187,6 +187,57 @@ impl Drop for HttpFailureFixture {
}
}
+struct KillOnDropChild(Child);
+
+impl Drop for KillOnDropChild {
+ fn drop(&mut self) {
+ let _ = self.0.kill();
+ let _ = self.0.wait();
+ }
+}
+
+fn read_process_pipe(mut pipe: impl Read) -> io::Result<Vec<u8>> {
+ let mut bytes = Vec::new();
+ pipe.read_to_end(&mut bytes)?;
+ Ok(bytes)
+}
+
+fn run_command_with_process_timeout(mut command: Command, timeout: Duration) -> Result<Output> {
+ std::thread::scope(|scope| {
+ let mut child = KillOnDropChild(
+ command
+ .stdin(Stdio::null())
+ .stdout(Stdio::piped())
+ .stderr(Stdio::piped())
+ .spawn()?,
+ );
+ let stdout = child.0.stdout.take().expect("CLI stdout must be piped");
+ let stderr = child.0.stderr.take().expect("CLI stderr must be piped");
+ // Drain both pipes while polling so large output cannot block exit.
+ let stdout = scope.spawn(move || read_process_pipe(stdout));
+ let stderr = scope.spawn(move || read_process_pipe(stderr));
+ let started = Instant::now();
+ let status = loop {
+ if let Some(status) = child.0.try_wait()? {
+ break status;
+ }
+ // The guard kills and reaps the child before the scoped pipe
+ // readers are joined, including on timeout or an early error.
+ ensure!(
+ started.elapsed() < timeout,
+ "CLI subprocess did not exit within {} ms",
+ timeout.as_millis()
+ );
+ std::thread::sleep(Duration::from_millis(10));
+ };
+ Ok(Output {
+ status,
+ stdout: stdout.join().expect("CLI stdout reader must not panic")?,
+ stderr: stderr.join().expect("CLI stderr reader must not panic")?,
+ })
+ })
+}
+
struct FailingOutput {
fail_on_flush: bool,
written: Vec<u8>,
@@ -518,12 +569,10 @@ async fn unsupported_raw_output_cancels_without_waiting_for_the_body() -> Result
])
.args(flag)
.arg(format!("{}/raw", server.base_url));
- let output = tokio::time::timeout(
- Duration::from_secs(2),
- tokio::task::spawn_blocking(move || command.output()),
- )
- .await
- .expect("unsupported raw output must return before the body timeout")??;
+ let output = tokio::task::spawn_blocking(move || {
+ run_command_with_process_timeout(command, Duration::from_secs(2))
+ })
+ .await??;
assert!(output.stdout.is_empty(), "{output:?}");
let stderr = String::from_utf8_lossy(&output.stderr);
if flag == Some("--fail") && status >= 400 {