diff --git a/apps/figcraft-cli/src/live_tests.rs b/apps/figcraft-cli/src/live_tests.rs index 9650856bb..e76f7d224 100644 --- a/apps/figcraft-cli/src/live_tests.rs +++ b/apps/figcraft-cli/src/live_tests.rs @@ -137,3 +137,17 @@ fn a_headless_session_is_confined_to_its_folders() { let _ = std::fs::remove_dir_all(&dir); let _ = std::fs::remove_dir_all(&outside); } + +// #2740: a connection file a crashed app left behind (nothing listens on its port any more) is not +// used: the call doesn't go there, and runs without a window in the command's folders. +#[test] +fn a_stale_connection_file_is_not_used() { + let dir = folder("stale"); + let gone = TcpListener::bind(("127.0.0.1", 0)).unwrap().local_addr().unwrap().port(); + publish(&dir, gone, "the-token"); + let mut backend = LiveBackend::new(options(&dir, false)); + let reply = backend.call("engine.commands", json!({})).unwrap(); + assert_ne!(reply.get("from"), Some(&json!("window")), "{reply}"); + assert!(!backend.has_ui(), "no window behind a stale file"); + let _ = std::fs::remove_dir_all(&dir); +} diff --git a/apps/figcraft/src/control_server.rs b/apps/figcraft/src/control_server.rs index 63f8d7213..91de4d7e4 100644 --- a/apps/figcraft/src/control_server.rs +++ b/apps/figcraft/src/control_server.rs @@ -291,8 +291,27 @@ enum Line<'a> { /// Read a line of at most [`MAX_LINE`] bytes plus its newline into `line`, taking its bytes from /// `budget` as they arrive. fn read_line<'a>(reader: &mut impl BufRead, line: &mut Vec, budget: &'a AtomicUsize) -> std::io::Result> { + read_line_by(reader, line, budget, None) +} + +/// [`read_line`], ending at `deadline` (an instant, and the socket whose read timeout counts down +/// to it) however the line arrives: a client trickling bytes can't stretch it past the deadline. +fn read_line_by<'a>( + reader: &mut impl BufRead, + line: &mut Vec, + budget: &'a AtomicUsize, + deadline: Option<(Instant, &TcpStream)>, +) -> std::io::Result> { let mut held = Held { budget, bytes: 0 }; loop { + // Data already buffered is returned without a read; the timeout only bounds the next one. + if let Some((at, socket)) = deadline { + let left = at.saturating_duration_since(Instant::now()); + if left.is_zero() { + return Err(std::io::Error::new(std::io::ErrorKind::TimedOut, "the token wait ended")); + } + socket.set_read_timeout(Some(left))?; + } let buf = reader.fill_buf()?; if buf.is_empty() { return Ok(if line.is_empty() { Line::End } else { Line::Read(held) }); @@ -347,6 +366,8 @@ fn serve_with_events( timeout: Duration, ) -> std::io::Result<()> { let mut shown = token.is_none(); + // The token wait is one deadline for the whole first line, however it arrives. + let token_by = Instant::now() + timeout.min(TOKEN_WAIT); out.set_read_timeout(Some(if shown { timeout } else { timeout.min(TOKEN_WAIT) }))?; out.set_write_timeout(Some(timeout))?; let mut reader = BufReader::new(out.try_clone()?); @@ -355,7 +376,8 @@ fn serve_with_events( set_idle(Some(Instant::now())); // A new buffer each time: an idle connection keeps no large line's capacity. let mut line = Vec::new(); - let _held = match read_line(&mut reader, &mut line, budget)? { + let deadline = (!shown).then_some((token_by, &out)); + let _held = match read_line_by(&mut reader, &mut line, budget, deadline)? { Line::End => break, Line::Read(held) => held, Line::Busy => { diff --git a/apps/figcraft/src/control_server_tests.rs b/apps/figcraft/src/control_server_tests.rs index 3a99b00d8..4708a8a5c 100644 --- a/apps/figcraft/src/control_server_tests.rs +++ b/apps/figcraft/src/control_server_tests.rs @@ -721,3 +721,26 @@ fn nothing_is_queued_once_the_server_has_stopped() { assert!(q.lock().unwrap().is_empty(), "nothing queued after the stop"); assert!(waiting.lock().unwrap().gates.is_empty()); } + +// The token wait is one deadline for the whole first line: a client sending a byte at a time, +// never a newline, can't hold its connection open past it. +#[test] +fn a_trickled_first_line_ends_at_the_token_wait() { + let (client, server) = pair(); + let started = Instant::now(); + let worker = std::thread::spawn(move || serve_with_token(server, "secret-token", queue(), context(), Duration::from_secs(60))); + let mut writer = client.try_clone().unwrap(); + let trickle = std::thread::spawn(move || { + for _ in 0..40 { + if writer.write_all(b"{").is_err() { + return; + } + std::thread::sleep(Duration::from_millis(50)); + } + }); + let _ = worker.join().unwrap(); + let took = started.elapsed(); + assert!(took < TOKEN_WAIT * 4, "ended near the token wait ({TOKEN_WAIT:?}), not after the trickle: {took:?}"); + drop(client); + let _ = trickle.join(); +}