diff --git a/src-tauri/src/app_constants.rs b/src-tauri/src/app_constants.rs index 4021df15..1f7ae8e1 100644 --- a/src-tauri/src/app_constants.rs +++ b/src-tauri/src/app_constants.rs @@ -16,6 +16,9 @@ pub(crate) const GRACEFUL_STOP_TIMEOUT_MS: u64 = 10_000; // reported to the user instead of turning a restart into a long hang. pub(crate) const BACKEND_EXIT_CONFIRM_TIMEOUT_MS: u64 = 3_000; pub(crate) const BACKEND_EXIT_CONFIRM_POLL_INTERVAL_MS: u64 = 100; +// Windows keeps retrying a loopback connect to a closed port for about two seconds +// before it reports the refusal, so a shorter probe times out on a free port. +pub(crate) const BACKEND_ENDPOINT_PROBE_MIN_TIMEOUT_MS: u64 = 3_000; #[cfg(target_os = "windows")] pub(crate) const SYSTEM_SHUTDOWN_STOP_TIMEOUT_MS: u64 = 2_000; pub(crate) const DEFAULT_BACKEND_READY_POLL_INTERVAL_MS: u64 = 300; diff --git a/src-tauri/src/backend/http.rs b/src-tauri/src/backend/http.rs index e2b85a5a..51ccbec5 100644 --- a/src-tauri/src/backend/http.rs +++ b/src-tauri/src/backend/http.rs @@ -9,7 +9,8 @@ use url::{Host, Url}; use crate::{ backend::http_response, desktop_auth::{DesktopAuthSession, DESKTOP_SESSION_ENDPOINT, DESKTOP_SESSION_HEADER}, - BackendState, DESKTOP_AUTH_REQUEST_TIMEOUT_MS, GRACEFUL_RESTART_START_TIME_TIMEOUT_MS, + BackendState, BACKEND_ENDPOINT_PROBE_MIN_TIMEOUT_MS, DESKTOP_AUTH_REQUEST_TIMEOUT_MS, + GRACEFUL_RESTART_START_TIME_TIMEOUT_MS, }; const MAX_BACKEND_HTTP_HEADER_BYTES: usize = 64 * 1024; @@ -95,11 +96,9 @@ where } for address in addrs { - let remaining = deadline - .checked_duration_since(Instant::now()) - .filter(|remaining| !remaining.is_zero()) - .ok_or_else(|| inconclusive("probe timed out"))?; - match TcpStream::connect_timeout(&address, remaining) { + // Each address gets the whole budget: a hostname such as localhost can resolve + // to several closed addresses, and Windows spends about two seconds on each. + match TcpStream::connect_timeout(&address, timeout) { Ok(_) => { return Err(format!( "Backend endpoint {display_url} is already answering. Stop the existing backend before starting another." @@ -138,7 +137,7 @@ impl BackendState { confirm_backend_endpoint_available_with_resolver( &self.backend_url, &self.endpoint_resolver, - Duration::from_millis(timeout_ms.max(1)), + Duration::from_millis(timeout_ms.max(BACKEND_ENDPOINT_PROBE_MIN_TIMEOUT_MS)), |host, port| { (host.as_str(), port) .to_socket_addrs() @@ -565,7 +564,7 @@ mod tests { let result = confirm_backend_endpoint_available_with_resolver( "http://backend.invalid:6185/", &resolver_worker, - Duration::from_millis(200), + Duration::from_millis(BACKEND_ENDPOINT_PROBE_MIN_TIMEOUT_MS), move |_, _| Ok(vec![address]), ); assert!( @@ -712,7 +711,7 @@ mod tests { let result = confirm_backend_endpoint_available_with_resolver( &format!("http://{address}/"), &std::sync::Mutex::new(None), - Duration::from_millis(200), + Duration::from_millis(BACKEND_ENDPOINT_PROBE_MIN_TIMEOUT_MS), |_, _| panic!("numeric addresses must not invoke DNS"), ); assert!( @@ -721,6 +720,43 @@ mod tests { ); } + #[test] + fn restart_endpoint_probe_waits_for_a_slow_loopback_refusal() { + let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("bind listener"); + let address = listener.local_addr().expect("listener address"); + drop(listener); + let state = BackendState { + backend_url: format!("http://{address}/"), + ..BackendState::default() + }; + + // Windows reports the refusal after about two seconds, longer than the default ping. + assert_eq!( + state.confirm_backend_endpoint_available(crate::DEFAULT_BACKEND_PING_TIMEOUT_MS), + Ok(()) + ); + } + + #[test] + fn restart_endpoint_probe_gives_every_closed_address_the_full_timeout() { + let closed = || { + let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("bind listener"); + listener.local_addr().expect("listener address") + }; + let addresses = vec![closed(), closed()]; + + let result = confirm_backend_endpoint_available_with_resolver( + "http://backend.invalid:6185/", + &std::sync::Mutex::new(None), + Duration::from_millis(BACKEND_ENDPOINT_PROBE_MIN_TIMEOUT_MS), + move |_, _| Ok(addresses), + ); + assert!( + result.is_ok(), + "two refused addresses must permit respawn: {result:?}" + ); + } + #[test] fn restart_endpoint_probe_does_not_treat_resolution_errors_as_available() { let error = confirm_backend_endpoint_available_with_resolver( @@ -787,7 +823,7 @@ mod tests { let error = confirm_backend_endpoint_available_with_resolver( "http://backend.invalid:6185/", &std::sync::Mutex::new(None), - Duration::from_millis(200), + Duration::from_millis(BACKEND_ENDPOINT_PROBE_MIN_TIMEOUT_MS), move |_, _| Ok(vec![available, occupied]), ) .expect_err("one answering address must block a respawn");