Craft Anything: understand how any software works, and craft your own.
Rust
Stabilize loopback request and cancellation fixtures (#767)
Afloatccfb5d48fc39
diff --git a/crates/ai/src/detect.rs b/crates/ai/src/detect.rsindex 41b3cca2..c5e4ae8b 100644--- a/crates/ai/src/detect.rs+++ b/crates/ai/src/detect.rs@@ -469,11 +469,9 @@ mod tests {#[test]fn closed_port_is_fast_and_negative() {- let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap();- let port = l.local_addr().unwrap().port();- drop(l);+ // Binding port 0 chooses a nonzero port, so no listener can take this endpoint.let t = Instant::now();- assert!(probe_local("X", &format!("http://127.0.0.1:{port}/v1")).is_none());+ assert!(probe_local("X", "http://127.0.0.1:0/v1").is_none());assert!(t.elapsed() < Duration::from_secs(1));}diff --git a/crates/engine/src/tools/standin_tools/tests.rs b/crates/engine/src/tools/standin_tools/tests.rsindex 043492d4..22b10385 100644--- a/crates/engine/src/tools/standin_tools/tests.rs+++ b/crates/engine/src/tools/standin_tools/tests.rs@@ -229,27 +229,39 @@ fn waiting_keeps_it_running_until_cancelled_then_stops_it() {let e = Arc::new(Engine::new(crate::testing::session()));let out = dir.join("s");call(&e, "web_standin_build", json!({"path": recording(&dir), "output_dir": out})).unwrap();- let port = std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port();+ let servers = Arc::new(StandIns::default());+ e.session().ext.insert(servers.clone());let cancel = crate::CancelToken::default();let (engine, token) = (e.clone(), cancel.clone());let waiting = std::thread::spawn(move || {- engine- .call("web_standin_start", json!({"path": out, "port": port, "wait": true}), CallOptions::new(Caller::Cli).with_cancel(token))- .map(|o| o.to_json())+ engine.call("web_standin_start", json!({"path": out, "wait": true}), CallOptions::new(Caller::Cli).with_cancel(token)).map(|o| o.to_json())});- let url = format!("http://127.0.0.1:{port}");let until = std::time::Instant::now() + Duration::from_secs(20);- while TcpStream::connect(("127.0.0.1", port)).is_err() {+ let (url, replay) = loop {+ let ready = {+ let running = servers.running.lock().unwrap_or_else(PoisonError::into_inner);+ running.values().next().map(|r| (r.server.url(), Arc::downgrade(&r.replay)))+ };+ if let Some(ready) = ready {+ break ready;+ }assert!(std::time::Instant::now() < until, "it starts listening");std::thread::sleep(Duration::from_millis(20));- }+ };// It answers while the call that started it is still going: answering needs no session.assert!(get(&url, "/users/1").contains("Pat"));assert!(!waiting.is_finished());+ assert!(replay.upgrade().is_some(), "the running tool still owns its answerer");cancel.cancel();let done = waiting.join().unwrap().unwrap();assert_eq!(done["result"]["stopped"]["requests_answered"], 1, "{done}");- assert!(TcpStream::connect(("127.0.0.1", port)).is_err(), "and it's gone");+ // A worker releases its slot just before dropping its captured answerer.+ let until = std::time::Instant::now() + Duration::from_secs(1);+ while replay.strong_count() > 0 {+ assert!(std::time::Instant::now() < until, "the stopped server retained its answerer");+ std::thread::sleep(Duration::from_millis(10));+ }+ assert!(replay.upgrade().is_none(), "the server and its request workers released their answerer");assert_eq!(call(&e, "web_standin_show", json!({})).unwrap()["result"]["servers"], json!([]));});}diff --git a/crates/engines/src/cutter.rs b/crates/engines/src/cutter.rsindex 472cf8fe..d0273f6a 100644--- a/crates/engines/src/cutter.rs+++ b/crates/engines/src/cutter.rs@@ -78,17 +78,36 @@ pub fn probe(port: u16) -> Option<Value> {mod tests {use super::*;use std::io::Write;- use std::net::TcpListener;+ use std::net::{TcpListener, TcpStream};+ use std::sync::mpsc;/// A stand-in session: answers one request with `body` and hands back the request line.fn stand_in(body: &'static str) -> (u16, std::thread::JoinHandle<String>) {+ stand_in_observing_reads(body, None)+ }++ fn stand_in_observing_reads(body: &'static str, waiting_for_read: Option<mpsc::Sender<usize>>) -> (u16, std::thread::JoinHandle<String>) {let l = TcpListener::bind("127.0.0.1:0").unwrap();let port = l.local_addr().unwrap().port();let h = std::thread::spawn(move || {let (mut s, _) = l.accept().unwrap();+ s.set_write_timeout(Some(Duration::from_secs(3))).unwrap();+ let deadline = std::time::Instant::now() + Duration::from_secs(3);let mut buf = [0u8; 4096];- let n = s.read(&mut buf).unwrap();- let req = String::from_utf8_lossy(&buf[..n]).lines().next().unwrap_or("").to_string();+ let mut head = Vec::new();+ while !head.windows(4).any(|w| w == b"\r\n\r\n") {+ let left = deadline.saturating_duration_since(std::time::Instant::now());+ assert!(!left.is_zero(), "the fixture request did not complete in time");+ s.set_read_timeout(Some(left)).unwrap();+ if let Some(waiting) = &waiting_for_read {+ waiting.send(head.len()).unwrap();+ }+ let n = s.read(&mut buf).unwrap();+ assert_ne!(n, 0, "the request ended before its headers were complete");+ head.extend_from_slice(&buf[..n]);+ assert!(head.len() <= 16 * 1024, "the fixture request exceeded its header limit");+ }+ let req = String::from_utf8_lossy(&head).lines().next().unwrap_or("").to_string();write!(s, "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", body.len()).unwrap();req});@@ -104,6 +123,33 @@ mod tests {assert_eq!(h.join().unwrap(), "GET /cmd/pd%201%20%40%20entry0 HTTP/1.1");}+ #[test]+ fn waits_for_fragmented_request_headers_before_answering() {+ let (waiting_tx, waiting_rx) = mpsc::channel();+ let (port, h) = stand_in_observing_reads("ok", Some(waiting_tx));+ let mut client = TcpStream::connect(("127.0.0.1", port)).unwrap();+ client.set_read_timeout(Some(Duration::from_secs(3))).unwrap();+ client.set_write_timeout(Some(Duration::from_secs(3))).unwrap();+ let partial = b"GET /cmd/ij HTTP/1.1\r\nHost: 127.0.0.1\r\n\r";+ client.write_all(partial).unwrap();++ // Withhold the final byte until the server has consumed the fragment and needs another read.+ // A single-read fixture closes the observer channel without reaching this state.+ loop {+ let consumed = waiting_rx.recv_timeout(Duration::from_secs(3)).expect("the fixture must read again after the partial header");+ assert!(consumed <= partial.len());+ if consumed == partial.len() {+ break;+ }+ }+ client.write_all(b"\n").unwrap();++ let mut response = String::new();+ client.read_to_string(&mut response).unwrap();+ assert_eq!(h.join().unwrap(), "GET /cmd/ij HTTP/1.1");+ assert_eq!(response, "HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok");+ }+#[test]fn probe_reads_what_is_open() {let (port, h) = stand_in(r#"{"core":{"file":"C:\\a\\app.exe","format":"pe64"},"bin":{"arch":"x86","bits":64}}"#);@@ -111,8 +157,7 @@ mod tests {h.join().unwrap();assert_eq!(v["file"], "C:\\a\\app.exe");assert_eq!(v["bits"], 64);- // Nothing listening: a closed port.- let free = TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port();- assert!(probe(free).is_none());+ // Port zero cannot be reassigned to another fixture's listener.+ assert!(probe(0).is_none());}}diff --git a/docs/ROADMAP.md b/docs/ROADMAP.mdindex 0c324e65..2b9048cc 100644--- a/docs/ROADMAP.md+++ b/docs/ROADMAP.md@@ -794,6 +794,14 @@ Owner: open · Status: in progress### R3.11 Single-file apps, game files and stand-in backendsOwner: open · Status: in progress+- [x] Validate the remaining loopback fixture corrections for complete and fragmented request+ headers, negative probes and stand-in cancellation on the current main+ — claimed: Afloat16, branch test/loopback-fixture-stability-20261103+ Revision `cd468aca`, synchronized with main `41d2175e`, passed+ [full Linux CI](https://github.com/storytold/craft-anything/actions/runs/41765850311).+ The fragmented-header regression withholds the final byte until the next read and keeps+ the existing request and response assertions. The request-encoding, negative-probe,+ cancellation and builder-discovery smoke checks passed in the same run.- [x] Verify stand-in session cleanup by the original answerer's lifetime rather than polling areleased port that another listener may reuse— claimed: Afloat16, branch test/r3.11-standin-session-lifetime-20261103diff --git a/docs/pitfalls.md b/docs/pitfalls.mdindex 400d5482..16895429 100644--- a/docs/pitfalls.md+++ b/docs/pitfalls.md@@ -102,14 +102,31 @@ the engines its settings or store name.`a`, so `b`'s tests never ran; the rerun was filtered to the failing test, and a broken engine idreached `main`. Run with `--no-fail-fast`, and after a fix rerun the whole crates, not a filter.+**A stopped server's port was no longer its identity.** A shutdown test connected to the old+port and treated any listener there as the stopped server, although another fixture could reuse it.+Observe the actual tool's registered server and a weak reference to its shared answerer; cancellation+must release that resource and empty its registry. Do not reserve and release a port before startup.+**A test that changes the whole process.** A UI test raised the process-wide stuck-lock banner; inCI the banner moved the layout under another test running beside it, and its click missed. It passed25 runs on a Mac. Anything a test sets for the whole process goes in a test program of its own.+**A free port handed to another test.** For a negative TCP probe, a bound port stopped being+ours as soon as its listener was dropped. Another fixture could acquire it before the probe.+Use the unassigned loopback port zero to exercise a real connection refusal without borrowing+and releasing a shared ephemeral port.+**Fixtures that leak into each other.** A "nothing here" path sat beside a fake game folder in thesame temp directory, and game detection walked up and found the game. Give each case its own`tempfile::tempdir()`.+**A mock server closed on a partial request.** A loopback fixture read a request only once, then+replied and closed. TCP can split even a short header; unread bytes made that close reset the+connection. Consume the complete header before replying, with a size cap and an absolute deadline.+Negative probes use loopback port zero so another fixture cannot acquire a released port.+A controlled fragmented request reproduced the fixture defect; the old CI log did not preserve+the probe's underlying error, so it does not establish that failure's cause.+**A script written and run at once.** Tests that write a stand-in program and start it failed nowand then on Linux with "Text file busy": another test thread was starting a child at that moment,and the child still held the new file open for writing. Starting a program goes through