From 9f8ec5d45f6be67ca6e2866fbcd7af2f28137295 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Sun, 22 Nov 2026 09:16:27 +0700 Subject: [PATCH] Remote reconnects when its control client is not usable; the restart and stray-line tests wait on that reader --- crates/mcp/src/backend.rs | 10 ++++++++-- crates/mcp/src/tests.rs | 20 ++++++++++---------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/crates/mcp/src/backend.rs b/crates/mcp/src/backend.rs index d5a6be4f31..dbfabda5a9 100644 --- a/crates/mcp/src/backend.rs +++ b/crates/mcp/src/backend.rs @@ -57,9 +57,15 @@ impl Remote { &self.addr } - /// The open connection; a new one when the app closed the last (an idle connection, a restart). + #[cfg(test)] + pub(crate) fn is_usable(&self) -> bool { + self.client.as_ref().is_some_and(ControlClient::is_usable) + } + + /// The open connection; a new one when the app closed the last (an idle connection, a restart) + /// or it holds a line nobody asked for. fn open_client(&mut self, method: &str) -> Result<&mut ControlClient, String> { - if self.client.as_ref().is_none_or(ControlClient::is_broken) { + if !self.client.as_ref().is_some_and(ControlClient::is_usable) { self.client = None; let client = ControlClient::connect(self.sock, &self.token) .map_err(|e| format!("VectorCraft app at {} is not reachable: {e}; `{method}`: nothing was sent", self.addr))?; diff --git a/crates/mcp/src/tests.rs b/crates/mcp/src/tests.rs index c625db76ab..4385118af6 100644 --- a/crates/mcp/src/tests.rs +++ b/crates/mcp/src/tests.rs @@ -1441,10 +1441,13 @@ fn signed_in(listener: &std::net::TcpListener) -> (Remote, std::net::TcpStream) }) } -fn wait_for_remote_eof(remote: &Remote) { - let probe = remote.connection_probe(); - probe.set_read_timeout(Some(WAIT)).unwrap(); - assert_eq!(probe.peek(&mut [0]).unwrap(), 0); +/// Waits until the app's end of the remote's connection closed or wrote a line nobody asked for. +fn wait_until_unusable(remote: &Remote) { + let deadline = std::time::Instant::now() + WAIT; + while remote.is_usable() { + assert!(std::time::Instant::now() < deadline, "the connection stayed usable"); + std::thread::yield_now(); + } } #[test] @@ -1470,7 +1473,7 @@ fn remote_edit_succeeds_once_when_app_restarts() { assert!(inspected["objects"].as_u64().unwrap() > 0); drop(old_handle.join().unwrap()); drop(listener); - wait_for_remote_eof(&remote); + wait_until_unusable(&remote); let listener = reusable_listener(addr); listener.set_nonblocking(true).unwrap(); @@ -1522,10 +1525,7 @@ fn remote_does_not_send_on_a_connection_with_a_stray_line() { let (mut remote, mut stale) = signed_in(&listener); stale.set_read_timeout(Some(WAIT)).unwrap(); writeln!(stale, "{}", json!({"ok": false, "error": "idle"})).unwrap(); - let probe = remote.connection_probe(); - probe.set_read_timeout(Some(WAIT)).unwrap(); - assert_eq!(probe.peek(&mut [0]).unwrap(), 1, "the stray line arrived"); - drop(probe); + wait_until_unusable(&remote); let h = std::thread::spawn(move || { let (stream, _) = listener.accept().unwrap(); stream.set_read_timeout(Some(WAIT)).unwrap(); @@ -1551,7 +1551,7 @@ fn remote_call_reports_nothing_sent_when_reconnect_is_refused() { let (remote, old_connection) = signed_in(&listener); drop(old_connection); drop(listener); - wait_for_remote_eof(&remote); + wait_until_unusable(&remote); let held = socket2::Socket::new(socket2::Domain::IPV4, socket2::Type::STREAM, None).unwrap(); held.set_reuse_address(true).unwrap(); held.bind(&addr.into()).unwrap();