Skip to content

fix(mem_cache): eliminate chunked-prefill radix-insert race corrupting QSA KV pages (#38319) - #38355

Open
andreasknopke wants to merge 1 commit into
sgl-project:qwen4-main-squashedfrom
andreasknopke:fix/chunked-prefill-radix-insert-race-38319
Open

andreasknopke wants to merge 1 commit into
sgl-project:qwen4-main-squashedfrom
andreasknopke:fix/chunked-prefill-radix-insert-race-38319

Conversation

@andreasknopke

@andreasknopke andreasknopke commented Sep 7, 2026 •

Copy link
Copy Markdown

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 untrained lm_head padding row).

Observed symptoms (from the issue's forensic dumps):

  • First decode token after prefill is always the impossible token 248319
  • num_correct_drafts = 0, accept_len = 1 — the target model independently produces the bad token
  • The corrupted KV slot range is identical across repeated requests with the same prefix
  • flush_cache resolves 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:

  • 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.

The 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 changed

File Change
python/sglang/srt/server_args.py new --disable-chunked-radix-insert arg (tri-state)
python/sglang/srt/mem_cache/cache_init_params.py new disable_chunked_radix_insert field
python/sglang/srt/mem_cache/kv_cache_builder.py QSA auto-detect + wiring into CacheInitParams
python/sglang/srt/mem_cache/mamba_radix_cache.py the guard in cache_unfinished_req

Testing

Verified by static analysis and full code-path trace (lock-ref balance, req_to_token prefix mapping via write_cache_indices, insert idempotency, 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

…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
@Jiminator
Jiminator deleted the branch sgl-project:qwen4-main-squashed September 14, 2026 04:45
@Jiminator Jiminator closed this Sep 14, 2026
@hnyls2002 hnyls2002 reopened this Sep 14, 2026
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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants