<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>SGLang on KbWen Blog</title>
    <link>https://www.kbwen.com/tags/sglang/</link>
    <description>Articles and notes on AI systems, machine learning, Python, data engineering, and software development.</description>
    <generator>Hugo</generator>
    <language>zh-tw</language>
    <image>
      <url>https://www.kbwen.com/images/og-default.png</url>
      <title>KbWen Blog</title>
      <link>https://www.kbwen.com/</link>
    </image>
    
    <lastBuildDate>Thu, 03 Dec 2026 18:55:00 +0800</lastBuildDate><atom:link href="https://www.kbwen.com/tags/sglang/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>GLM-5.3 在 Workers AI 上的 KV cache 外洩</title>
      <link>https://www.kbwen.com/glm-5-3-workers-ai-kv-cache-exposure/</link>
      <pubDate>Thu, 03 Dec 2026 18:55:00 +0800</pubDate><dc:creator>KbWen</dc:creator>
      <guid>https://www.kbwen.com/glm-5-3-workers-ai-kv-cache-exposure/</guid>
      <description>Cloudflare Workers AI 上的 GLM-5.3 在 8 月底到 9 月初，有 155 筆回應混進了別人的請求內容。我們來看看一個超長的 prompt 在 SGLang 的 prefill 和 decode 之間是怎麼出錯的。</description>
      <content:encoded><![CDATA[<p>Cloudflare 在 10 月 26 日公開了一份<a href="https://blog.cloudflare.com/kv-cache-exposure-remediation/">事件報告</a>：從 8 月 27 日到 9 月 9 日，Workers AI 上的 GLM-5.3（<code>@cf/zai-org/glm-5.3</code>）有少數客戶收到的回應裡，夾雜著其他客戶的請求資料，那些請求都是不久前送給同一個模型的。受影響的回應一共 155 筆，其中對應的 prompt 都超過 681,280 tokens。排除 Cloudflare 自己和回報研究者的測試之後，這些請求在時間和數量上都長得像一般的長上下文使用，沒有找到有人刻意利用或是設計的跡象。</p>
<h2 id="上下文長度和-kv-pool">上下文長度和 KV pool</h2>
<p>GLM-5.3 上線時開的上下文長度是 1,048,576 tokens，但 prefill GPU 上的 KV cache pool 只放得下 681,280 tokens。上下文長度是設定值，pool 的大小則是引擎啟動時，載入權重之後剩下多少 GPU 記憶體就切多少。</p>
<p>功能上線前 Cloudflare 就知道 pool 比上下文長度小，但他們以為引擎碰到放不下的 prompt 會回錯誤；長上下文的測試回傳的也都是成功的狀態碼，串流內容裡沒有顯示任何錯誤。那時他們主要是只驗證了狀態碼和引擎效能，沒有去看模型輸出結果。</p>
<h2 id="被拒絕的請求">被拒絕的請求</h2>
<p>先說明個問題來源，出問題的地方在 KV cache。模型讀取 prompt 時會計算出大量中間結果，生成時再反覆拿來做 attention，這些結果就放在 KV cache 裡（<a href="/deepseek-v3-multi-head-latent-attention-kv-cache/">DeepSeek-V3 的 Multi-head Latent Attention 如何壓縮 KV 快取</a>那篇講過 KV cache 為什麼很佔記憶體）。原因事實上是兩件事情疊加在一起，單看可能各有優點，但是一起發生就糟了：一是設定，另一件則是 Cloudflare 用的推論引擎 SGLang 裡的 bug。</p>
<p>SGLang 在處理單一節點上拒絕塞不下的 prompt，其實做法有點取巧：先把要回給客戶端的錯誤記下來，再把整個 prompt 換成只有一個 token 的佔位，讓請求照一般批次的流程跑完，不必真的配置記憶體，客戶端最後收到錯誤，一切就如預期。</p>
<p>不過 Workers AI 採用分離式部署。prefill 讀完整個 prompt、算出 KV cache，decode 再一個一個 token 往下生成，在不同的 GPU 上，prefill 算好的 KV cache 透過 RDMA 直接寫進 decode 的記憶體。準入佇列（admission queue）決定要不要收下請求，而兩個佇列都只檢查請求結束了沒。拒絕的狀態記在 <code>to_finish</code>，不在 <code>finished_reason</code>，所以 <code>finished()</code> 回傳的還是 False。</p>
<p>router 會先告訴 decode 這個 prompt 有多長，decode 照長度預留 KV cache。prefill 收下請求後，對那個佔位 token 做了一次很短的 forward pass，並通知傳輸完成。decode 收到通知，一樣用 <code>finished()</code> 檢查、放行、開始生成。此時 attention 讀取預留的全部頁面，可是這個請求只寫過第一頁，後面全部都還是前面的請求留下的東西，在這裡指的就是：別人的 prompt、別人生成到一半的內容。(但現今的推論引擎常這樣做，效能才能夠好。)</p>
<p>客戶端拿到 HTTP 200，照理這個請求應該拿到 400。回應裡的 <code>prompt_tokens</code> 跟送出去的長 prompt 一致，回覆讀起來也通順。從狀態碼和欄位上都看不出問題，只是回覆的速度比預想中快很多，比示意圖裡 prefill 該花的時間短。</p>
<h2 id="修補">修補</h2>
<p>Cloudflare 9 月 9 日緊急把模型下線，改設定讓 prefill 的 KV pool 放得下約 204 萬 tokens，正常的請求再也走不到拒絕那條路。光是這樣其實就擋住了這個 bug。不過只靠 pool 大小並不保險，量化方式、tensor parallelism、CUDA graph 的大小，甚至換不同版本模型，都會讓 pool 跟著變，而且很容易沒人注意到。所以同時也修了引擎，讓已經被拒絕的請求在交接之前就直接結束，回傳原本該出現的錯誤。</p>
<p>假設你自己也用 SGLang 跑分離式 prefill/decode，在引擎內建這種檢查之前，可以先從每台 prefill 和 decode 節點的啟動 log 找出 KV pool 的大小，跟上下文長度比對一下，pool 比較小就把上下文長度調低；權重、量化或平行設定改過之後，再比較一次。另外把 SGLang 升到 v0.5.22 以上，那樣子之前長度放不下的請求會在交接之前就被擋下來，回給客戶端錯誤。</p>
<p>問題最後是回報的研究者發現的：在超長 prompt 的回覆裡讀到屬於別人 session 的文字，交給 Cloudflare 的重現腳本，後來也拿來驗證設定和引擎的修補，事情也算是落幕了。
這是照 Cloudflare 的事件報告整理，有錯誤的地方歡迎指正。</p>
]]></content:encoded>
    </item>
    
    <item>
      <title>The Workers AI GLM-5.3 KV cache exposure</title>
      <link>https://www.kbwen.com/workers-ai-glm-5-3-kv-cache-exposure/</link>
      <pubDate>Thu, 03 Dec 2026 18:55:00 +0800</pubDate><dc:creator>KbWen</dc:creator>
      <guid>https://www.kbwen.com/workers-ai-glm-5-3-kv-cache-exposure/</guid>
      <description>The GLM-5.3 KV cache exposure on Cloudflare Workers AI, and the check of KV pool size against context length that Cloudflare now runs before a deployment change ships.</description>
      <content:encoded><![CDATA[<blockquote>
<p><strong>TL;DR:</strong> Between August 27 and September 9, 2026, 155 responses from the GLM-5.3 model on Cloudflare Workers AI contained data from other customers&rsquo; requests, all of them to prompts longer than 681,280 tokens. Cloudflare corrected its configuration and patched SGLang, the inference engine it uses, and recommends SGLang v0.5.22 or later to every operator running it.</p>
</blockquote>
<p>On September 8, a security researcher, Gadi Evron from Knostic, reported through Cloudflare&rsquo;s HackerOne program that very long prompts sent to <code>@cf/zai-org/glm-5.3</code> on Workers AI sometimes came back with text from other users&rsquo; sessions. Cloudflare reproduced it the next morning, took the model offline, and restored it the same day with a corrected configuration and a patched build of SGLang. Their <a href="https://blog.cloudflare.com/kv-cache-exposure-remediation/">incident write-up</a> was published on October 26, alongside a post from the SGLang side.</p>
<p>Workers AI offered GLM-5.3 with a context window of 1,048,576 tokens. The <a href="/how-kv-cache-speeds-up-llm-generation/">KV cache</a> pool on the prefill GPUs had room for 681,280. The context length is a number someone writes into the deployment config. The pool size is worked out by SGLang at startup, from whatever GPU memory is left after the weights load, so a change of quantization or parallelism moves it. Cloudflare knew about the gap before launch, and expected the engine to return an error for any prompt that did not fit.</p>
<h2 id="a-prompt-longer-than-the-pool">A prompt longer than the pool</h2>
<p>When a prompt over the pool size arrived, SGLang&rsquo;s scheduler did reject it. The rejection path records the error the client should receive and replaces the prompt with a one-token placeholder, so the request can drain through the normal batch machinery without allocating real memory. On a single machine this works: the placeholder runs, the batch finishes, and the client gets its error.</p>
<p>Workers AI runs GLM-5.3 with prefill and decode on separate machines. One pool of GPUs does prefill, reading the prompt and building its KV cache, and another does decode, generating the reply; the cache moves between them over RDMA (remote direct memory access). In that mode the admission queues on both sides checked whether a request was finished, not whether it had been rejected. A rejected request stored its rejection in a field called <code>to_finish</code>. <code>finished()</code> reads <code>finished_reason</code> instead, so it returned False, and the request went through both queues as if it were live.</p>
<p>The KV cache is the memory that holds keys and values for every token the model has read, and SGLang hands it out in pages. Cloudflare&rsquo;s report walks through the failure with a prompt that needs eight of them. The router tells the decode node how long the prompt is, and decode reserves eight pages for it. Prefill has already swapped that prompt for its placeholder, so it runs a forward pass on one token, sends one page of KV across, and signals that the transfer is complete. Decode accepts the handoff and starts generating, and attention reads all eight pages it reserved. Only page P0 holds anything this request wrote. Pages P1 through P7 still hold what earlier requests left there: other users&rsquo; prompts and partial generations (pages are not zeroed between requests; the next request to get one simply overwrites it).</p>
<p>What reached the client looked like an ordinary answer: HTTP 200 where it should have been a 400, a <code>prompt_tokens</code> count that matched the long prompt, and a coherent reply. The reply arrived faster than prefilling eight pages should take, and it contained content from other users&rsquo; KV pages.</p>
<h2 id="testing-and-fixes">Testing and fixes</h2>
<p>The benchmarks Cloudflare ran on long prompts before launch saw the same thing: successful status codes and a streaming body with no error frames. Their write-up calls it &ldquo;one critical gap in our testing&rdquo;: they checked response codes and performance, but never the output.</p>
<p>GLM-5.3 was the only Workers AI model affected. Every other deployment there that splits prefill and decode had a pool larger than its context length, so no request could reach the rejection path. SGLang prints the KV pool it selected in its startup log and exposes it as a metric, and Cloudflare now compares that figure with the context length, read from the running engine, before it ships any change to such a deployment. The same comparison is the first item on the list they give other operators: measure the pool on every prefill and decode node after each change that could move it, and lower the context length if the pool is smaller.</p>
<p>SGLang v0.5.22 or later adds a backstop in the engine: a request that is too long is rejected before prefill hands it to decode, whatever the pool size. Cloudflare&rsquo;s write-up says fixes like these reduce the ways a page can be misused, but do not make misuse impossible. What it calls the durable answer is for the engine to notice, at the moment decode reads the cache, that a request is about to attend over a page it does not own, and fail that one request instead of returning someone else&rsquo;s data.</p>
<p>Upstream SGLang already has a checksum, switched on by a flag, that validates the KV handed from prefill to decode, but not the reads that come after it. When Cloudflare published on October 26, they were working with the SGLang team to build the read-time check into the engine, for every model.</p>
<p>The problem was found through an outside researcher&rsquo;s HackerOne report and closed the next day with a corrected configuration and a patched engine. This post summarises Cloudflare&rsquo;s incident report; corrections are welcome.</p>
]]></content:encoded>
    </item>
    
  </channel>
</rss>
