From c1d26746bfa1a9643f1d9b529ce3e6400cc97b47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 23 Sep 2026 00:57:20 +0300 Subject: [PATCH] fix: send a reconnect's index replay to the new worker A RAG restart leaves the dying connection registered until the 30s idle timeout. Equal load picked the older id, so the replay that fires on register was sent to a connection that then timed out, and the school's notes never re-entered the in-memory index. Equal load now prefers the newer id. The blob-write stall is also 15s, under the 30s idle timeout, so a stalled blob resets its stream instead of dropping the connection. Local-Gate: 295251821acae9b52e92214d17dec1bac2baf67e (1513 passed, 521s) --- src/ai/registry.rs | 24 +++++++++++++++++++++--- src/constant.rs | 15 ++++++++++++++- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/ai/registry.rs b/src/ai/registry.rs index 693462c..6359415 100644 --- a/src/ai/registry.rs +++ b/src/ai/registry.rs @@ -191,9 +191,13 @@ impl Registry { let chosen = offering .filter(|w| w.inflight() < w.max_concurrent) .min_by(|a, b| { - a.inflight() - .cmp(&b.inflight()) - .then_with(|| a.id.cmp(&b.id)) + a.inflight().cmp(&b.inflight()).then_with(|| { + // A reconnect keeps the dying connection in the registry + // until its idle timeout. Equal load must prefer the + // newer id, or the replay that fires on register is sent + // to the connection that is about to time out. + b.id.cmp(&a.id) + }) }) .ok_or_else(|| AiError::Busy(capability.to_string()))?; chosen.inflight.fetch_add(1, Ordering::Relaxed); @@ -336,6 +340,20 @@ mod tests { reg.pick("ocr.extract").expect("slot came back"); } + #[tokio::test] + async fn equal_load_prefers_the_newer_worker() { + // A reconnect leaves the dying connection registered until its idle + // timeout. The replay that fires on the new Hello must not be handed + // to that older id. + let reg = registry_with(&[ + ("01a0cafe-old", &["rag.index"], 4), + ("01a0cb0f-new", &["rag.index"], 4), + ]); + let lease = reg.pick("rag.index").unwrap(); + assert_eq!(lease.worker().id, "01a0cb0f-new"); + } + + #[tokio::test] async fn picks_spread_across_workers_by_least_inflight() { let reg = registry_with(&[("w1", &["ocr.extract"], 4), ("w2", &["ocr.extract"], 4)]); diff --git a/src/constant.rs b/src/constant.rs index b8cc6c1..f523daf 100644 --- a/src/constant.rs +++ b/src/constant.rs @@ -559,6 +559,10 @@ pub const AI_DEFAULT_CONCURRENT_PER_WORKER: usize = 8; /// well under the idle timeout so an idle-but-healthy service is never dropped /// for being quiet; a service that actually died is deregistered within the /// idle window without any application-level heartbeat frame. +/// +/// Must stay strictly above [`AI_BLOB_WRITE_STALL_SECS`]. A stalled blob write +/// resets that stream; if the two bounds are equal, a busy runtime lets the +/// idle timeout win and the whole connection dies as `TimedOut` instead. pub const AI_IDLE_TIMEOUT_SECS: u64 = 30; pub const AI_KEEPALIVE_SECS: u64 = 10; @@ -568,7 +572,16 @@ pub const AI_KEEPALIVE_SECS: u64 = 10; /// takes, while a service that opens a blob stream and never reads it stops /// pinning a task and an open file descriptor once the QUIC stream window /// fills. A wall-clock deadline on the transfer could not tell the two apart. -pub const AI_BLOB_WRITE_STALL_SECS: u64 = 30; +/// +/// Strictly under [`AI_IDLE_TIMEOUT_SECS`]: the stream reset has to land +/// before the connection idle timeout, or a stalled reader kills every other +/// stream on that connection. +pub const AI_BLOB_WRITE_STALL_SECS: u64 = 15; + +const _: () = assert!( + AI_BLOB_WRITE_STALL_SECS + 5 < AI_IDLE_TIMEOUT_SECS, + "a stalled blob write must reset the stream before the connection idle timeout" +); /// The capability an AI service declares to answer chatbot turns. The bridge /// routes a chat request to any worker carrying it; with none registered the