From 832a749d765b86629a9a07d82fd37cf0cd26a67a Mon Sep 17 00:00:00 2001 From: "andrea.difonzo" Date: Tue, 17 Nov 2026 12:53:28 +0100 Subject: [PATCH] Server locale: timeout dello stream a 30 minuti per i provider locali, nessun TCP keepalive (C) - stream_idle_timeout predefinito 30 minuti per i provider su loopback, rete privata, localhost, .local e .lan (5 minuti per gli altri), sempre sovrascrivibile con stream_idle_timeout_ms; funzioni ModelProviderInfo::is_local_endpoint e is_local_base_url; - client HTTP senza TCP keepalive: reqwest sonda dopo 15 s e chiude dopo 3 sonde senza risposta, e NetBird non fa passare le sonde: ogni risposta partita dopo 60 s veniva tagliata; - test del timeout per locali e remoti. Da 4e1f551b, solo le parti C (ordinamento strumenti, attesa MCP, eventi imeai restano fuori). Co-Authored-By: Claude Sonnet 5.5 --- akode-rs/http-client/src/client_builder.rs | 10 +++- akode-rs/model-provider-info/src/lib.rs | 46 +++++++++++++++++-- .../src/model_provider_info_tests.rs | 27 +++++++++++ 3 files changed, 79 insertions(+), 4 deletions(-) diff --git a/akode-rs/http-client/src/client_builder.rs b/akode-rs/http-client/src/client_builder.rs index 83415516..fa8f1da0 100644 --- a/akode-rs/http-client/src/client_builder.rs +++ b/akode-rs/http-client/src/client_builder.rs @@ -370,7 +370,15 @@ impl HttpClientBuilder { } fn base_reqwest_builder(self) -> reqwest::ClientBuilder { - let mut builder = reqwest::Client::builder(); + // No TCP keepalive: reqwest probes after 15 s of silence, 3 times every 15 s, and drops + // the connection when nobody answers. Some VPNs (NetBird towards the imeAI server) do + // not pass the probes, so every response that started after more than 60 s (a local + // model reading a long prompt) was cut and sent again from scratch. Dead streams are + // detected by the provider's idle timeout instead. + let mut builder = reqwest::Client::builder() + .tcp_keepalive(None) + .tcp_keepalive_interval(None) + .tcp_keepalive_retries(None); #[cfg(windows)] if let Some(path) = self.windows_proxy_pipe_path { builder = builder.windows_named_pipe(path); diff --git a/akode-rs/model-provider-info/src/lib.rs b/akode-rs/model-provider-info/src/lib.rs index a5eeca92..f74bc2c1 100644 --- a/akode-rs/model-provider-info/src/lib.rs +++ b/akode-rs/model-provider-info/src/lib.rs @@ -67,6 +67,10 @@ pub fn read_managed_residency_requirement() -> Option { } const DEFAULT_STREAM_IDLE_TIMEOUT_MS: u64 = 300_000; +/// Local servers (loopback, private network, `.local`/`.lan`) read long prompts on CPU: a cold +/// prompt of 15k tokens took more than 3 minutes before the first byte. They get a generous +/// default, still overridable with `stream_idle_timeout_ms`. +const DEFAULT_LOCAL_STREAM_IDLE_TIMEOUT_MS: u64 = 1_800_000; const DEFAULT_STREAM_MAX_RETRIES: u64 = 5; const DEFAULT_REQUEST_MAX_RETRIES: u64 = 4; const DEFAULT_CREDENTIAL_EXPORT_TIMEOUT_MS: u64 = 30_000; @@ -179,7 +183,9 @@ pub struct ModelProviderInfo { /// Number of times to retry reconnecting a dropped streaming response before failing. pub stream_max_retries: Option, /// Idle timeout (in milliseconds) to wait for activity on a streaming response before treating - /// the connection as lost. + /// the connection as lost. It also bounds the wait for the first response (headers). Any + /// bytes count as activity, including SSE comments and progress events. Defaults to 5 + /// minutes, or 30 minutes for local servers. pub stream_idle_timeout_ms: Option, /// Maximum time (in milliseconds) to wait for a websocket connection attempt before treating /// it as failed. @@ -390,11 +396,23 @@ impl ModelProviderInfo { .min(MAX_STREAM_MAX_RETRIES) } - /// Effective idle timeout for streaming responses. + /// Effective idle timeout for streaming responses (and for the first response). pub fn stream_idle_timeout(&self) -> Duration { self.stream_idle_timeout_ms .map(Duration::from_millis) - .unwrap_or(Duration::from_millis(DEFAULT_STREAM_IDLE_TIMEOUT_MS)) + .unwrap_or_else(|| { + Duration::from_millis(if self.is_local_endpoint() { + DEFAULT_LOCAL_STREAM_IDLE_TIMEOUT_MS + } else { + DEFAULT_STREAM_IDLE_TIMEOUT_MS + }) + }) + } + + /// Whether `base_url` points to this machine or the local network: loopback, private IPv4, + /// `localhost`, `.localhost`, `.local`, `.lan`. + pub fn is_local_endpoint(&self) -> bool { + self.base_url.as_deref().is_some_and(is_local_base_url) } /// Effective timeout for websocket connect attempts. @@ -519,3 +537,25 @@ pub fn create_oss_provider_with_base_url(base_url: &str, wire_api: WireApi) -> M #[cfg(test)] #[path = "model_provider_info_tests.rs"] mod tests; + +/// See [`ModelProviderInfo::is_local_endpoint`]. +pub fn is_local_base_url(base_url: &str) -> bool { + let Ok(uri) = base_url.trim().parse::() else { + return false; + }; + let Some(host) = uri.host() else { + return false; + }; + let host = host.trim_start_matches('[').trim_end_matches(']'); + if let Ok(ip) = host.parse::() { + return match ip { + std::net::IpAddr::V4(ip) => ip.is_loopback() || ip.is_private(), + std::net::IpAddr::V6(ip) => ip.is_loopback(), + }; + } + let host = host.trim_end_matches('.').to_ascii_lowercase(); + host == "localhost" + || host.ends_with(".localhost") + || host.ends_with(".local") + || host.ends_with(".lan") +} diff --git a/akode-rs/model-provider-info/src/model_provider_info_tests.rs b/akode-rs/model-provider-info/src/model_provider_info_tests.rs index bbc9d432..b184330d 100644 --- a/akode-rs/model-provider-info/src/model_provider_info_tests.rs +++ b/akode-rs/model-provider-info/src/model_provider_info_tests.rs @@ -470,3 +470,30 @@ fn merge_system_messages_defaults_on() { assert!(provider.merge_system_messages()); } } + +#[test] +fn local_providers_get_a_generous_idle_timeout() { + let provider = |base_url: &str| -> ModelProviderInfo { + toml::from_str(&format!("name = \"x\"\nbase_url = \"{base_url}\"\n")).expect("provider") + }; + for local in [ + "http://192.168.40.25:8080/v1", + "http://127.0.0.1:8080/v1", + "http://localhost:11434/v1", + "http://[::1]:1234/v1", + "http://imeai.lan/v1", + ] { + assert_eq!( + provider(local).stream_idle_timeout(), + Duration::from_secs(30 * 60), + "{local}" + ); + } + assert_eq!( + provider("https://openrouter.ai/api/v1").stream_idle_timeout(), + Duration::from_secs(5 * 60) + ); + let mut configured = provider("http://192.168.40.25:8080/v1"); + configured.stream_idle_timeout_ms = Some(90_000); + assert_eq!(configured.stream_idle_timeout(), Duration::from_secs(90)); +}