Skip to content

[Fix] hicache: Continue eviction after a failed file unlink - #42488

Open
apex-mochen wants to merge 3 commits into
sgl-project:mainfrom
apex-mochen:codex/hicache-eviction-error-progress
Open

apex-mochen wants to merge 3 commits into
sgl-project:mainfrom
apex-mochen:codex/hicache-eviction-error-progress

Conversation

@apex-mochen

@apex-mochen apex-mochen commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

With a bounded HiCache file backend, one undeletable oldest cache page prevents eviction of every later page. _evict_one_lru_locked restores that entry at the LRU head and returns stop on OSError. Every later capacity-constrained write retries the same head and is refused, even when another page can be reclaimed.

Reproduction on official main 29bcb74: fill a 128-byte cap with two real 64-byte files, inject PermissionError only for the oldest file's unlink, then cache another 64-byte page. The new write returns False and the second, deletable file is never tried.

Modifications

Retain the failed entry and its charged bytes at the end of the eviction queue, returning skipped so the existing bounded skip loop can try other victims. A failed unlink contributes zero reclaimed bytes and does not invalidate positive metadata. Update the helper's outcome documentation.

Two CPU regressions check:

  • Selective unlink failure: a later victim is evicted, the new file contains the expected bytes, the failed file and metadata remain, and accounting stays at 128 bytes.
  • All unlinks fail: the write is refused after trying both candidates, no reservation or byte refund occurs, and a later retry succeeds once unlink is allowed.

This changes eviction ordering after an unlink failure. It does not retry forever or assume the failed file was removed. It shares the existing skip budget with pending writes. Scope is one file-backend evictor, not cross-process coordination.

Accuracy Tests

Python 3.12, PyTorch 2.13.0+cu130, CUDA hidden; normal SGLang imports with isolated test dependencies. Real CPU tensors/files; unlink failures injected, not a claim that a physical filesystem failure was reproduced.

  • Before production fix: 2 failed, 34 deselected, 16 warnings (23.21s).
  • After fix, full existing file-cache suite: 36 passed, 16 warnings (24.17s).
CUDA_VISIBLE_DEVICES=9 PYTHONPATH=python:<test-dependencies> python -m pytest test/registered/unit/mem_cache/test_hicache_file_lru_unit.py -q

No full GPU server, distributed restore or model-accuracy test was run.

Speed Tests and Profiling

No benchmark or throughput claim. This repairs admission progress under an unlink error, leaving the successful eviction path unchanged.

Checklist

  • Format with pre-commit.
  • Add tests to an existing registered CPU suite.
  • Update helper documentation; no new configuration or public API.
  • GPU model accuracy / performance benchmark (not run; fault recovery change).
  • Follow existing code style.

Review and Merge Process

Ready for KV Cache review and authorized CI. AI assistance used for investigation, implementation, tests and this description. Distinct from #42482 (pending write ownership), #41686 (free-space reservations) and #39269 (file read errors).


CI States

Latest PR Test (Base): ⊘ Skipped -- a maintainer comments /tag-and-rerun-ci to run CI tests.
Latest PR Test (Extra): ⊘ Skipped -- a maintainer comments /tag-and-rerun-ci to run CI tests.
Latest PR Test (AMD ROCm 10): ⚠️ Run #47288275693

Tip

Base CI only covers the core cases, and its workload may not exercise your change. Use /rerun-test to run the tests your change touches, or copy this prompt for an agent:

Find the registered tests this PR affects and run them with /rerun-test, following https://github.com/sgl-project/sglang/blob/main/.agents/skills/rerun-test/SKILL.md

Latest-main validation (2026-10-06)

Rebased onto upstream main bb4d0a13c128400fcbed97096e3d823f33563084, which includes the maintenance gate's required base fea3c088a141. Production/test patch IDs match the previous head; no behavior or scope change. Current head: 4456e4562473fc3db2a24e8ff901b4f49faa7eeb.

The existing registered CPU file-cache suite was run on an isolated latest-main tree with this exact patch: 36 passed, 3 warnings in 22.48s. git diff --check passed. Command:

CUDA_VISIBLE_DEVICES=9 PYTHONPATH=python /home/dcl/venvs/sglang-cpu/bin/python -m pytest test/registered/unit/mem_cache/test_hicache_file_lru_unit.py -q

The first attempt with an empty CUDA_VISIBLE_DEVICES failed at test collection because test_utils indexes its first character; it ran no tests. The retry above passed. No GPU serving, performance benchmark, or model-accuracy claim. GitHub CI must complete separately; previous red summaries came from a required-base maintenance check before tests ran.

@apex-mochen

Copy link
Copy Markdown
Contributor Author

@ispobock Could you review this small HiCache file eviction fix? One oldest page with a failing unlink currently stops all eviction, even when later pages are reclaimable. The change keeps its bytes charged and uses the existing bounded skip loop to try other victims. Two regressions fail before the fix; the complete file-cache CPU suite passes (36 tests). Coverage includes all candidates failing and successful retry after the error clears. No GPU serving or performance claims. This is independent of the pending-write ownership fix in #42482.

@github-actions github-actions Bot added the hicache Hierarchical Caching for SGLang label Oct 4, 2026
@apex-mochen apex-mochen closed this Oct 6, 2026
@apex-mochen apex-mochen reopened this Oct 6, 2026
@apex-mochen
apex-mochen force-pushed the codex/hicache-eviction-error-progress branch from 45563c5 to 4456e45 Compare October 6, 2026 08:45
@apex-mochen

Copy link
Copy Markdown
Contributor Author

The run for the pr-gate check on the HiCache change is blocked on the external-contributor authorization gate (call-gate). Please add the run-ci label so the full platform test matrix can run. The change is Python-only and covered by unit tests; the failing jobs are authorization-skips, not test failures.

@apex-mochen

Copy link
Copy Markdown
Contributor Author

@xiezhq-hermann Thanks for reviewing #42077. This is another small HiCache file-backend correctness fix, so I wanted to ask if you’d be willing to take a look when convenient.

#42488 fixes eviction stopping after one failed file unlink. The failed page remains charged, while eviction continues through the existing bounded victim loop so other reclaimable pages are still considered.

Focused regressions cover both:

  • all unlink candidates failing;
  • eviction succeeding again after the unlink error clears.

The complete CPU file-cache suite passes locally.

If someone else owns this path more directly, happy to route it there instead. Thanks!

@apex-mochen
apex-mochen force-pushed the codex/hicache-eviction-error-progress branch from 4456e45 to 803e448 Compare October 13, 2026 01:15
@xiezhq-hermann xiezhq-hermann self-assigned this Oct 13, 2026
@apex-mochen apex-mochen changed the title [HiCache] Continue eviction after a failed file unlink [Fix] hicache: Continue eviction after a failed file unlink Oct 16, 2026
detain added a commit to detain/sglang that referenced this pull request Oct 29, 2026
fix(hicache): continue eviction after a failed unlink

One undeletable oldest page pinned the LRU head and every later
capacity write refused. A failed unlink is now a bounded skip
(accounting retained, entry re-inserted at MRU) so the eviction loop
keeps trying other victims.

(cherry picked from commit 803e448)
detain added a commit to detain/sglang that referenced this pull request Oct 29, 2026

@xiezhq-hermann xiezhq-hermann 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.

Thanks for the fix and the regression tests, and sorry for the slow review. I checked it against current main (a478cd21e0):

  • Both new tests fail on main. set("new") is refused with "no evictable space ... not caching", and remove is called once instead of twice.
  • Both pass with your change, and the whole file passes (45 tests).
  • The PR still merges cleanly.

#31485 reported the same wedge earlier with the same approach, but the idle-PR bot closed it before anyone reviewed it, so thanks for picking this up.

A few asks:

  1. Please drop TestEnvDefaults from the test file. #43503 removed that class on purpose, and the rebase brought it back.
  2. Please bound failed unlinks per eviction pass. When the whole cache directory stops allowing unlink (read-only remount, lost permission, dead FUSE/NFS mount), main gives up after one failed unlink. This version tries every tracked file and logs a warning for each, all while holding the evictor lock, which also blocks touch() on reads.
    • Measured with 20,000 tracked files and the directory chmod 0555 (as a non-root user): one refused reserve goes from 1 unlink attempt (about 0.1 ms) to 20,000 attempts and 20,000 warnings (about 170 ms). The cost grows with the cap.
    • A fix: add a separate "failed" outcome that stops _evict_while after a small constant number of failures in one pass (I tried 8). That brings the case back to 8 attempts and about 0.1 ms, and all 45 tests (including both of yours) stay green. Because the bad entry is re-pinned at MRU, later passes still rotate it out of the LRU head.
  3. Optional: the fast_file backend has the same stop-at-LRU-head behavior in storage/fast_file/lru_file_evictor.py (_evict_one_lru_locked, the except OSError branch). Mirroring the fix there would be welcome, but it is also fine to keep this PR scoped to file.

Once these are in, I'm happy to add run-ci.

Review asks on sgl-project#42488:

- Drop TestEnvDefaults; sgl-project#43503 removed it and the rebase brought it back.
- Add a distinct failed outcome for a refused unlink and stop _evict_while after 8 consecutive failures. The failed entry is re-inserted at MRU, so later passes rotate other victims in; bytes stay charged.
- Add a regression with 12 tracked files and every unlink failing: 8 attempts, accounting unchanged.

Signed-off-by: apex-mochen <[email protected]>
@apex-mochen

Copy link
Copy Markdown
Contributor Author

Thanks @xiezhq-hermann — all three asks are addressed in c2a1154.

  1. TestEnvDefaults removed from the test file.
  2. Bounded failed unlinks. _evict_one_lru_locked() now returns a distinct ("failed", 0); _evict_while() stops the pass after _MAX_CONSECUTIVE_UNLINK_FAILURES = 8 consecutive failures and resets the budget on a successful eviction. The failed entry is re-inserted after the pop, which re-pins it at MRU, so later passes rotate other victims in; its bytes stay charged. Added test_failed_unlinks_are_bounded_per_pass: 12 tracked files, every os.remove refused → exactly 8 attempts and _total_bytes unchanged.
  3. fast_file mirror: python/sglang/srt/mem_cache/storage/fast_file/ does not exist at 803e448 (the storage/ listing has file, mmap, mooncake_store, … but no fast_file). If it lives on current main or under another path, point me at it and I will mirror the same bound.

Verification: py_compile passes. The repository test was not run here (no sglang environment); a method-level harness loading the real LRUFileEvictor methods with only the sglang.srt imports stubbed passes 2/2 — 8 attempts for 12 failing files, and one failure followed by success re-pins the failed key at MRU and reclaims 8 bytes.

Head c2a1154. Please add run-ci when you are happy with the shape.

@apex-mochen

Copy link
Copy Markdown
Contributor Author

Thanks for the approval. The remaining red checks (pr-gate, pr-test-npu-finish, pr-test-musa-finish, pr-test-mlx-finish, pr-test-amd-finish) are the external-contributor authorization gate, not test failures: the annotations are only Process completed with exit code 1 from .github with no code path. Is there anything else you need from me before merge, or should I wait for a maintainer to add run-ci / merge this as-is? I won't re-push or change the head.

@apex-mochen

apex-mochen commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor Author

I checked the current merge conflict and prepared a sync commit, b754ee2, on main 034117d. It preserves the reviewed head as its first parent. The diff against main still covers only the evictor and registered LRU tests. The production fix is unchanged and the three regressions were moved to fit the current fixture layout.

I reran 29 backend-local cases with real CPU tensors and files in an isolated Windows runner. All 29 passed. This is local evidence and the upstream CI run is still pending.

GitHub has the sync commit, but the branch update did not go through, so this PR still points to c2a1154. If you are able to update the PR branch, the linked commit can be applied as a fast-forward. The resulting head then needs the repository's normal CI approval.

Update: the branch is now at be21d7e and GitHub reports it merges cleanly. The new commit only relocates the existing regression cases, and all 29 scoped backend cases passed on the locally merged source. GitHub still reports the previous review as approved. Could a maintainer enable the required CI for this head?

I also fetched the storage implementation, evictor, and registered unit file directly from GitHub's test merge 422fb59 (base 88c5c23, PR head be21d7e). All 29 scoped backend cases passed locally on those exact source files. This remains a local CPU result, separate from GitHub Actions.

Additional failure-path check on 422fb59: I exercised both the byte cap and the free-space watermark with eight undeletable files followed by one reclaimable file. The first admission stopped after eight attempts. The next admission evicted only the reclaimable file and completed the write. I also let unlink succeed while the modeled free-space probe remained unchanged; the watermark still refused the write until the probe reported enough space. These new cases failed on main 54c7614 and the 31 scoped cases passed on the test merge. The Windows harness used real files and a controlled free-space probe.

Keep the original regression cases in a separate test class so this branch merges with main's updated test layout.

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

Labels

hicache Hierarchical Caching for SGLang

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants