From ba3318ffbdb2db2e4cbdb3564efd76cdbe309120 Mon Sep 17 00:00:00 2001 From: "minke.yu" Date: Tue, 13 Oct 2026 15:14:05 +0800 Subject: [PATCH] =?UTF-8?q?gateway=20g9i:=20fix=20shadow=20compile=20error?= =?UTF-8?q?s=20=E2=80=94=20lookup=20returns=20owned=20snapshot=20(borrow?= =?UTF-8?q?=20across=20loop=20mutation),=20bind=20temp=20in=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sgl-model-gateway/src/routers/http/g9i_shadow.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/sgl-model-gateway/src/routers/http/g9i_shadow.rs b/sgl-model-gateway/src/routers/http/g9i_shadow.rs index 8f37d1ac6d..9feec020e4 100644 --- a/sgl-model-gateway/src/routers/http/g9i_shadow.rs +++ b/sgl-model-gateway/src/routers/http/g9i_shadow.rs @@ -128,14 +128,14 @@ impl PrefixTable { /// Returns `(depth, entry)` where depth is the number of table probes /// spent (1 = deepest boundary hit immediately). Hits refresh /// `last_seen`; expired entries are removed on encounter and count as - /// misses. - pub fn lookup(&mut self, chain: &[u64]) -> Option<(usize, &PrefixEntry)> { + /// misses. The entry is CLONED on return (a few dozen bytes): returning + /// a borrow would pin `self.map` across the loop's mutation (expire- + /// on-encounter), which the borrow checker rejects — and a clone makes + /// the API honest about the entry being a snapshot anyway. + pub fn lookup(&mut self, chain: &[u64]) -> Option<(usize, PrefixEntry)> { let mut depth = 0usize; for &key in chain.iter().rev() { depth += 1; - // Resolve presence/freshness first, then borrow mutably in a - // separate statement — splitting keeps the borrow checker - // happy without NLL edge cases (match-guard borrows). enum Probe { Miss, Expired, @@ -156,7 +156,8 @@ impl PrefixTable { Probe::Fresh => { if let Some(e) = self.map.get_mut(&key) { e.last_seen = Instant::now(); - return Some((depth, e)); + let snapshot = e.clone(); + return Some((depth, snapshot)); } } } @@ -1107,7 +1108,8 @@ mod tests { // injected details field is removed from it. let (usage, forward) = process_sse_chunk(chunk, false, true); assert!(usage.is_some()); - let fwd_str = String::from_utf8_lossy(&forward.unwrap()); + let fwd = forward.unwrap(); + let fwd_str = String::from_utf8_lossy(&fwd); assert!(fwd_str.contains("\"prompt_tokens\":42")); assert!(!fwd_str.contains("cached_tokens_details"));