Repository navigation
fix(mem_cache): eliminate chunked-prefill radix-insert race corrupting QSA KV pages (#38319) - #38355
Open
andreasknopke wants to merge 1 commit into
Conversation
…g QSA KV pages Fixes sgl-project#38319 ## Bug A race between chunked-prefill KV-page insertion into the Radix Cache and pool page deallocation (retract/abort) produces silently corrupted KV pages on QSA compressed-attention models (e.g. Qwen3.8-Flash-Next). Once a corrupt prefix is cached, every subsequent request matching the same prefix reads garbage attention data on its very first decode step, yielding an impossible-token loop (token 248319 = untrained lm_head padding row). ## Root cause During chunked prefill, MambaRadixCache.cache_unfinished_req() inserts each chunk's KV pages into the radix tree MID-prefill. The radix node then holds references to KV pages that still belong to the in-flight request. If the request is retracted (decode OOM) or aborted before prefill completes, release_kv_cache() -> cache_finished_req(is_insert=False) frees those pages back to the allocator. The radix node survives with dangling references: the pages are re-allocated to a different request (or hold uninitialized data), but the tree still points at them. For QSA the corruption is amplified: the compressed-KV slot is addressed as full_slot // compress_ratio, so a single freed full-KV page maps to a compressed slot that now contains another request's data. The corruption is: - stochastic (depends on chunk-insert vs retract timing), - persistent (the radix cache keeps the bad prefix), - prefix-specific (same slot range across repeated requests), - resolved temporarily by flush_cache (evicts the corrupt prefix). ## Fix Add a disable_chunked_radix_insert guard to MambaRadixCache. When set, cache_unfinished_req() skips the radix insert for chunked prefill (chunked=True) and only updates req.prefix_indices (the existing _skip_cache_unfinished_req path). The insert is deferred to cache_finished_req(), which runs after the request is fully committed and no longer subject to retraction. This closes the race window: the radix tree never references a request's KV pages while those pages can still be freed by a retract. The guard is auto-enabled for QSA compressed-attention models (parse_qsa_profile variant == "compressed") and exposed as the tri-state server arg --disable-chunked-radix-insert (None=auto, True/False=override). Non-QSA models are unaffected (flag defaults to False). ## Why it is safe - The skip path (_skip_cache_unfinished_req) already exists and is used when the cache is disabled or cache_len is None; it correctly maintains req.prefix_indices for chunk continuation via add_chunked_req. - req.last_node is only assigned by the initial match_prefix and by the normal cache_unfinished_req path; the skip path leaves it unchanged, so cache_finished_req's dec_lock_ref(req.last_node) balances the initial inc_lock_ref from _req_inc_lock_ref. - cache_finished_req calls the same insert(prev_prefix_len= req.cache_protected_len). insert() is idempotent: it matches what is already in the tree and inserts only the new tokens. The KV pages are the same (allocated by alloc_for_extend); only the timing of tree-node creation differs. - On retraction, cache_finished_req(is_insert=False) frees kv_indices[cache_protected_len:]; with no intermediate inserts the tree holds no nodes for that range, so the free is correct and the shared prefix (0..cache_protected_len) is untouched. - mamba_last_track_seqlen is set by the last prefill chunk's forward pass in both paths, so cache_finished_req reads the same value. ## Files - python/sglang/srt/server_args.py: new --disable-chunked-radix-insert arg - python/sglang/srt/mem_cache/cache_init_params.py: new field - python/sglang/srt/mem_cache/kv_cache_builder.py: QSA auto-detect + wiring - python/sglang/srt/mem_cache/mamba_radix_cache.py: the guard
andreasknopke
requested review from
Ying1123,
alphabetc1,
hanming-lu,
hnyls2002,
huangtingwei9988,
hzh0425,
ispobock,
merrymercy,
xiezhq-hermann and
yizhang2077
as code owners
September 7, 2026 16:27
detain
added a commit
to detain/sglang
that referenced
this pull request
Oct 29, 2026
…amilies' blocks deleted), sgl-project#38355 moved to Section R as MISDIAGNOSED (C11 entry closed, five-leg citation), sgl-project#33029 moved to Section R as NOT-NEEDED, S-RACE tripwire queued post-merge, sgl-project#46191 ring-decision cross-reference updated
detain
added a commit
to detain/sglang
that referenced
this pull request
Oct 29, 2026
sgl-project#38355 Convert the sgl-project#38355 refutation research (MISDIAGNOSED: the tree owns [0, cache_protected_len) and the request's release frees only [cache_protected_len, owned_kv_len) -- disjoint by contract) into a permanent registered CPU tripwire so the dangling-radix-insert class stays dead. T1 drives the real release_kv_cache with slot-recording fakes; T2 replays checkpoint-then-retract on the real UnifiedRadixCache (CPU pools, both TreeCore backends) and asserts match_prefix never answers with a released page; T3 trips if the PR's edit targets or the QSA variant auto-detect reappear. Registered cpu/base-a-test-cpu. Refs: PR sgl-project#38355, issue sgl-project#38319
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #38319 — a race between chunked-prefill KV-page insertion into the Radix Cache and pool page deallocation (retract/abort) that silently corrupts KV pages on QSA compressed-attention models (e.g. Qwen3.8-Flash-Next).
The bug
Once a corrupt prefix is cached in the radix tree, every subsequent request matching the same prefix reads garbage attention data on its very first decode step, producing an impossible-token loop (token
248319= an untrainedlm_headpadding row).Observed symptoms (from the issue's forensic dumps):
248319num_correct_drafts = 0,accept_len = 1— the target model independently produces the bad tokenflush_cacheresolves it temporarily (evicts the corrupt radix prefix)Root cause
During chunked prefill,
MambaRadixCache.cache_unfinished_req()inserts each chunk's KV pages into the radix tree mid-prefill. The radix node then holds references to KV pages that still belong to the in-flight request.If the request is retracted (decode OOM) or aborted before prefill completes,
release_kv_cache()→cache_finished_req(is_insert=False)frees those pages back to the allocator. The radix node survives with dangling references: the pages are re-allocated to a different request (or hold uninitialized data), but the tree still points at them.For QSA the corruption is amplified: the compressed-KV slot is addressed as
full_slot // compress_ratio, so a single freed full-KV page maps to a compressed slot that now contains another request's data. This explains why the corruption is:flush_cache.The fix
Add a
disable_chunked_radix_insertguard toMambaRadixCache. When set,cache_unfinished_req()skips the radix insert for chunked prefill (chunked=True) and only updatesreq.prefix_indices(the existing_skip_cache_unfinished_reqpath). The insert is deferred tocache_finished_req(), which runs after the request is fully committed and no longer subject to retraction.This closes the race window: the radix tree never references a request's KV pages while those pages can still be freed by a retract.
The guard is auto-enabled for QSA compressed-attention models (
parse_qsa_profilevariant =="compressed") and exposed as the tri-state server arg--disable-chunked-radix-insert(None=auto,True/False=override). Non-QSA models are unaffected (flag defaults toFalse).Why it is safe
_skip_cache_unfinished_req) already exists and is used when the cache is disabled orcache_len is None; it correctly maintainsreq.prefix_indicesfor chunk continuation viaadd_chunked_req.req.last_nodeis only assigned by the initialmatch_prefixand by the normalcache_unfinished_reqpath; the skip path leaves it unchanged, socache_finished_req'sdec_lock_ref(req.last_node)balances the initialinc_lock_reffrom_req_inc_lock_ref.cache_finished_reqcalls the sameinsert(prev_prefix_len=req.cache_protected_len).insert()is idempotent: it matches what is already in the tree and inserts only the new tokens. The KV pages are the same (allocated byalloc_for_extend); only the timing of tree-node creation differs.cache_finished_req(is_insert=False)freeskv_indices[cache_protected_len:]; with no intermediate inserts the tree holds no nodes for that range, so the free is correct and the shared prefix (0..cache_protected_len) is untouched.mamba_last_track_seqlenis set by the last prefill chunk's forward pass in both paths, socache_finished_reqreads the same value.Files changed
python/sglang/srt/server_args.py--disable-chunked-radix-insertarg (tri-state)python/sglang/srt/mem_cache/cache_init_params.pydisable_chunked_radix_insertfieldpython/sglang/srt/mem_cache/kv_cache_builder.pyCacheInitParamspython/sglang/srt/mem_cache/mamba_radix_cache.pycache_unfinished_reqTesting
Verified by static analysis and full code-path trace (lock-ref balance,
req_to_tokenprefix mapping viawrite_cache_indices,insertidempotency, retraction free range, mamba track-seqlen handling). Runtime verification on a DGX Spark (GB10) with the Qwen3.8-Flash-Next checkpoint is the remaining step — the corruption is silent, so a clean boot is not sufficient; Needle@240k + GSM8K under chunked prefill + retraction pressure is the real check.CI States
Latest PR Test (Base): ❌ Run #34829698030
Latest PR Test (Extra): ❌ Run #34829697659
Latest PR Test (AMD ROCm 10): ❌ Run #34829698084