Skip to content

[HiCache]: Fix host lock ownership across radix splits - #38480

Merged
alphabetc1 merged 2 commits into
sgl-project:mainfrom
lluki:fix/hicache-stale-set-split
Oct 4, 2026
Merged

alphabetc1 merged 2 commits into
sgl-project:mainfrom
lluki:fix/hicache-stale-set-split

Conversation

@lluki

@lluki lluki commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

HiCache can corrupt a value being written to L3 storage when a radix-tree node is split while its write-back SET is still in flight.
The SET reads directly from host KV-cache blocks. A split creates a new prefix node for the shared tokens, but that fragment does not inherit the parent's full host lock. Duplicate-host reclamation may therefore free and reuse the prefix blocks before the storage backend has finished reading them, this causes wrong data to be written to storage. The backend behaves correctly: it stores exactly what is in those buffers when its SET runs. The stale write is caused by SGLang changing ownership of the buffers too early.

Modifications

  • Propagate a full host lock from the original node to the prefix fragment created by a split.
  • When the asynchronous SET finishes, release the lock across every fragment created after the SET began, stopping at the recorded pre-split parent.
  • Require the acquisition result when releasing a host lock. Without its pre-split boundary, a parameterless release could unlock only the original child and permanently leak the copied prefix lock.
  • Apply the same ownership rule to the Python and Rust unified radix-tree implementations.

This keeps every zero-copy SET source block owned by the SET until the backend call has returned, even if the radix node is split in the meantime.

Accuracy Tests

I tested this with both unit tests (in this PR) an end2end reproducer (not in this PR).

The e2e verifier runs SGLang with hicache nixl backend and slows down the batch_set_* artificially to reliably trigger the bug:

  • Send an 832-token victim prompt whose deterministic answer is ZEBRA4821.
  • Send a 3500-token unrelated prompt. This evicts the victim to host and starts its delayed POSIX SET.
  • Send a prompt that shares the first 704 victim tokens and then diverges.
  • This splits the host-backed node while its SET is in flight.
  • Send a 3000-token unrelated prompt to create host pressure. On buggy code, the split prefix is reclaimed and its host blocks are reused.
  • Wait for the SET, flush the memory tiers, and send the original victim completion request again so it must be loaded from POSIX storage.

The second request reports a 768-token storage hit, proving that it exercised L3. On buggy upstream code its answer changes from ZEBRA4821 to repeated Answer: tokens. This is visible entirely through the completion API.

I ran this validation and testsuite with both python and rust cores.

Speed Tests and Profiling

N/A

Checklist

Review and Merge Process

  1. Ping Merge Oncalls to start the process. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • Common commands include /tag-and-rerun-ci, /tag-run-ci-label, /rerun-failed-ci
  4. After green CI and required approvals, ask Merge Oncalls or people with Write permission to merge the PR.

CI States

Latest PR Test (Base): ✅ Run #36804765986
Latest PR Test (Extra): ✅ Run #36804765970
Latest PR Test (AMD ROCm 10): ❌ Run #36804765995

@ishandhanani

Copy link
Copy Markdown
Collaborator

/tag-and-rerun-ci

@github-actions github-actions Bot added the run-ci CI: run the baseline test suite on this PR label Sep 8, 2026
Comment on lines +278 to +282
if node.parent is not None:
# A later split inserts nodes between this anchor and its current
# parent. Remember that boundary so release also unlocks those
# fragments without touching pre-existing ancestors.
result.skip_lock_node_ids.setdefault(ct, set()).add(node.parent.id)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CC @xiezhq-hermann who is trying to removing skip_lock_node_ids in #36848

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a quick LLM chat, the bug I'm addressing here (split node not being accounted host_lock_ref) seems currently present in #36848 too. Maybe host_lock_ref should also become a segment lock?

@alphabetc1

Copy link
Copy Markdown
Collaborator

please resolve the conflicts

@lluki
lluki force-pushed the fix/hicache-stale-set-split branch from 7a05ee7 to 94855cf Compare September 11, 2026 09:53
@github-actions github-actions Bot added the documentation Improvements or additions to documentation label Sep 11, 2026
@lluki

lluki commented Sep 11, 2026

Copy link
Copy Markdown
Contributor Author

Rebased after merge, can confirm the bug is still present after #36848

Implementation changed quite a bit:

  • Full host locks now use segment UUID receipts, following the existing SWA locking model.
  • A radix split copies the Full host lock count and UUID boundary to the new prefix.
  • Unlocking walks only to the receipt’s UUID boundary, correctly handling nested locks.
  • Python and Rust receipt conversions preserve the Full host UUID.

Testing:

  • With the E2E verifier checked that the bug is still present and that this PR fixes the E2E test as well. Tested rust core and python core
  • A new Python-only regression directly verifies split-node lock counts and complete host-pool recovery.
  • The Rust suite validates the underlying lock and split primitives

@lluki
lluki requested a review from Jialin September 11, 2026 13:42
@lluki
lluki force-pushed the fix/hicache-stale-set-split branch from 94855cf to 01d2f97 Compare September 14, 2026 07:29
@lluki

lluki commented Sep 14, 2026

Copy link
Copy Markdown
Contributor Author

rebased

@alphabetc1 alphabetc1 added bypass-fastfail run-ci-extra CI: also run the extra suite (requires run-ci) labels Sep 14, 2026
@alphabetc1 alphabetc1 self-assigned this Sep 14, 2026

@Jialin Jialin left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to explain the motivation of this PR with my own words to confirm my understanding.

"""
Originally, a Full host lock pinned only its anchor node, and host_lock_ref wasn’t propagated during splits. This PR copies the refcount to the new prefix, preserving protection for all original buffers. It also moves the UUID boundary to that prefix, so unlock releases exactly the chain of fragments covered by the original lock—preventing leaked host references.
"""

CC @ZYHowell @xiezhq-hermann

Comment on lines +832 to +835
/// Mint the next component lock-segment uuid.
pub(crate) fn next_component_uuid_(&mut self) -> i64 {
self.component_uuid_counter += 1;
self.component_uuid_counter

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's introduce separate counters per component type? (As the counter is on tree core layer, the memory overhead should be negaligible).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add this. Although maybe for debugging it would be easier to have disjoint ID spaces

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, good point. Currently, we have multiple monotonic growth counters which are all starts from 0. I think a proper way out is to

  1. still using separate i64 counters for different use case
  2. use different and fixed initial offset to achieve disjointness
  3. with that being built, it's also easier for the debugger to tell what does these belongs to.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I can add this. n * (1B = 1,000,000,000) as start offsets?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I can add this. n * (1B = 1,000,000,000) as start offsets?

Maybe prepare for the worse case, counter got bump every 1ns, and the server live for a week. With that, 1E14 is able to cover all ns changes for a week.

(n+1)*1E14 should be safe.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@lluki
lluki force-pushed the fix/hicache-stale-set-split branch from 01d2f97 to d6550c7 Compare September 15, 2026 14:36
@lluki
lluki requested a review from Jialin September 15, 2026 15:24

@Jialin Jialin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM. For easier long term maintainance, let's further enhance the test coverage.

For example, ensure there's no leakage with E2E test (e.g. acuiqre, split and then release)

@lluki

lluki commented Sep 16, 2026

Copy link
Copy Markdown
Contributor Author

Great, I'll think about extending the testcases too

@lluki
lluki force-pushed the fix/hicache-stale-set-split branch from d6550c7 to 87eacfd Compare September 17, 2026 14:17
@lluki

lluki commented Sep 17, 2026

Copy link
Copy Markdown
Contributor Author

Ok, l think the test coverage of the hicache looking mechanism is already quite good. I added in the last push:

 - Added explicit lock_ref checks across acquire -> split -> release, ensuring every copied ref returns to zero.
 - Added test for (SWA) host_lock_ref, verifying split fragments remain pinned and release cleanly without pool-accounting leakage.
 - Added a native Rust SWA test covering receipt acquisition, splitting, release, zeroed refs, and host-LRU re-entry.
 - Made tests not assume the leaf starts under root.

@alphabetc1

Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@ispobock

Copy link
Copy Markdown
Collaborator

@lluki could you resolve the conflict?

@lluki
lluki force-pushed the fix/hicache-stale-set-split branch from 492fdb3 to 232a3ee Compare September 28, 2026 13:02
@lluki

lluki commented Sep 28, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto upstream’s generic lock API and updated the tests.

@lluki
lluki force-pushed the fix/hicache-stale-set-split branch from 232a3ee to d0f7fbf Compare September 29, 2026 08:09
@lluki
lluki force-pushed the fix/hicache-stale-set-split branch from d0f7fbf to 103d7fb Compare September 30, 2026 09:25
@lluki

lluki commented Sep 30, 2026

Copy link
Copy Markdown
Contributor Author

In my opinion the CI is green enough, the hicache tests are green for both python and Rust, the remaining ones are skipped runs or infrastructure failures. But that is some maintainers call @xiezhq-hermann @alphabetc1

@ishandhanani

Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@alphabetc1
alphabetc1 merged commit 4611ea3 into sgl-project:main Oct 4, 2026
519 of 591 checks passed
detain added a commit to detain/sglang that referenced this pull request Oct 7, 2026
…receipt test

test_host_lock_refs_round_trip (from upstream sgl-project#41276) asserted the SWA
leaf's host-lock receipt holds only an SWA entry. sgl-project#38480 makes the Full
component record its host-lock boundary uuid in the same receipt, so the
exact-dict assertion failed for both SWA cases on a freshly built
extension (also on the sgl-project#38480 head itself). Assert the SWA leg is still
windowless and the Full leg is present instead.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
detain added a commit to detain/sglang that referenced this pull request Oct 10, 2026
Brings in 699 upstream commits up to 87845cd (sgl-project#43301, 2026-10-09).

Conflict resolutions (67 files):
- multimodal_gen quality tiers: adopt upstream exact/lossless/high migration
  (old lossless=bit-exact -> new "exact"; fusion moves into "lossless", which is
  now the DEFAULT). Migrated the stack's teacache guard (minimax_h3) to the
  "exact" tier and ltx_2 mask-skip to quality_allows(q, "lossless").
- video saving: take upstream's one-pass _save_video_ffmpeg (plugin-free),
  superseding the stack's PyAV/ffmpeg-pipe two-pass path (c16d89e/1b66dcb374);
  video_api generate_audio re-expressed over their request_extra_value.
- teacache: union API -- keep our get_thresh/get_skip_boundaries/is_step_boundary
  alongside their get_skip_step_range (wanvideo/longcat/minimax + tests use all).
- orchestrator.py: kept OURS wholesale (their refactor targets classic
  dispatchers our DAG sgl-project#139 removed); glm_distributed NotImplementedError lives in
  scheduler_mixin.
- schedule_batch: retained our grammar_needs_sync field.
- pool_host / mamba naming: took theirs.
- unified_radix_cache: took their prefix-hit API, re-porting our bounded-evict
  logic into allocate_storage_hit.
- hybrid_cache_controller: kept sgl-project#39830 degrade-to-miss guard over their changes.
- hybrid_pool_assembler: took theirs (sgl-project#39893 absorbed our earlier patch).
- qwen4_exp: rebuilt stage-boundary logic + re-applied PLE deltas.
- qsa_sparse_attn_backend: integrated upstream CP / breakable-graphs /
  verify-fusion over our SM120 triton-decode and FP8-descale paths.
- sgl-project#38480 + sgl-project#39862 arrived via upstream (stack dupes dropped).
- tests: upstream deleted test_scheduler_decision_batch_params; ported that
  coverage to test_scheduler_grammar_mixed_tail_sync.py.
- quantization import test (merge fallout): upstream renamed kitchen_int8 ->
  convrot_int8 and dropped the package-level config-class __getattr__ shim, so
  test_quantization_imports now asserts get_quantization_config dispatch against
  the concrete modules (canonical convrot names) plus an explicit
  kitchen_int8->convrot alias check.

Deferred: GPU end-to-end verification owed (all 4 GPUs are running production
servers; this battery was CPU-forced). Stack fixes land live only after the next
diffusion/AR server restarts.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bypass-fastfail documentation Improvements or additions to documentation run-ci CI: run the baseline test suite on this PR run-ci-extra CI: also run the extra suite (requires run-ci) unified-radix-cache

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants