Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio desktop: share one loopback HTTP client per timeout (#15675)

Daniel Hane4d513f34cd8
diff --git a/studio/src-tauri/src/loopback_http.rs b/studio/src-tauri/src/loopback_http.rs
index b2b84ad5ee2..b21660afac2 100644
--- a/studio/src-tauri/src/loopback_http.rs
+++ b/studio/src-tauri/src/loopback_http.rs
@@ -1,12 +1,38 @@
+use std::collections::HashMap;
+use std::sync::{Mutex, OnceLock};
use std::time::Duration;
+/// Past this many distinct timeouts, clients are built per call.
+const MAX_CACHED_CLIENTS: usize = 16;
+
/// Redirects refused: this client posts `.desktop_secret`, and reqwest strips headers, not bodies,
/// on cross-host redirects, so a 307 would carry the secret off-host.
+/// Shared per timeout: a build loads TLS roots. No idle pool: no connection outlives a restart.
pub(crate) fn client(timeout: Duration) -> Result<reqwest::Client, reqwest::Error> {
+ static CLIENTS: OnceLock<Mutex<HashMap<Duration, reqwest::Client>>> = OnceLock::new();
+ let clients = CLIENTS.get_or_init(|| Mutex::new(HashMap::new()));
+ if let Ok(cached) = clients.lock() {
+ if let Some(client) = cached.get(&timeout) {
+ return Ok(client.clone());
+ }
+ }
+ let client = build_client(timeout)?;
+ if let Ok(mut cached) = clients.lock() {
+ if cached.len() < MAX_CACHED_CLIENTS {
+ cached.entry(timeout).or_insert_with(|| client.clone());
+ }
+ }
+ Ok(client)
+}
+
+fn build_client(timeout: Duration) -> Result<reqwest::Client, reqwest::Error> {
+ #[cfg(test)]
+ tests::record_build(timeout);
reqwest::Client::builder()
.no_proxy()
.timeout(timeout)
.redirect(reqwest::redirect::Policy::none())
+ .pool_max_idle_per_host(0)
.build()
}
@@ -33,6 +59,79 @@ mod tests {
const CHILD_ENV: &str = "UNSLOTH_TEST_LOOPBACK_HTTP_CHILD";
+ static BUILDS: std::sync::Mutex<Vec<Duration>> = std::sync::Mutex::new(Vec::new());
+
+ pub(super) fn record_build(timeout: Duration) {
+ BUILDS.lock().unwrap().push(timeout);
+ }
+
+ fn builds_of(timeout: Duration) -> usize {
+ BUILDS.lock().unwrap().iter().filter(|built| **built == timeout).count()
+ }
+
+ #[tokio::test]
+ async fn one_client_per_timeout_still_times_out_and_opens_a_connection_per_request() {
+ use std::sync::{Arc, Mutex};
+
+ // Unique timeout, so the build count is this test's own.
+ let timeout = Duration::from_millis(1_237);
+ for _ in 0..5 {
+ super::client(timeout).unwrap();
+ }
+ assert_eq!(builds_of(timeout), 1, "built once, then shared");
+
+ let listener = tokio::net::TcpListener::bind(("127.0.0.1", 0)).await.unwrap();
+ let port = listener.local_addr().unwrap().port();
+ let accepted = Arc::new(Mutex::new(0usize));
+ let counter = accepted.clone();
+ tokio::spawn(async move {
+ use tokio::io::{AsyncReadExt, AsyncWriteExt};
+ while let Ok((mut socket, _)) = listener.accept().await {
+ *counter.lock().unwrap() += 1;
+ tokio::spawn(async move {
+ let mut buf = [0u8; 2048];
+ let _ = socket.read(&mut buf).await;
+ let _ = socket
+ .write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok")
+ .await;
+ // Kept open: only a pool could reuse it.
+ tokio::time::sleep(Duration::from_secs(3)).await;
+ });
+ }
+ });
+ let client = super::client(timeout).unwrap();
+ for _ in 0..3 {
+ let body = client
+ .get(format!("http://127.0.0.1:{port}/"))
+ .send()
+ .await
+ .unwrap()
+ .text()
+ .await
+ .unwrap();
+ assert_eq!(body, "ok");
+ }
+ assert_eq!(*accepted.lock().unwrap(), 3);
+
+ let silent = tokio::net::TcpListener::bind(("127.0.0.1", 0)).await.unwrap();
+ let silent_port = silent.local_addr().unwrap().port();
+ let _hold = tokio::spawn(async move {
+ let mut held = Vec::new();
+ while let Ok((socket, _)) = silent.accept().await {
+ held.push(socket);
+ }
+ });
+ let started = std::time::Instant::now();
+ let error = super::client(timeout)
+ .unwrap()
+ .get(format!("http://127.0.0.1:{silent_port}/"))
+ .send()
+ .await
+ .unwrap_err();
+ assert!(error.is_timeout(), "{error:?}");
+ assert!(started.elapsed() < Duration::from_secs(3));
+ }
+
#[test]
fn client_ignores_system_proxy() {
if std::env::var_os(CHILD_ENV).is_none() {