From 434c2e3adcd77afe42d6604b693d97aef71938e6 Mon Sep 17 00:00:00 2001 From: Shuwen Wang <47200617+alphabetc1@users.noreply.github.com> Date: Fri, 25 Sep 2026 15:06:24 +0800 Subject: [PATCH] [HiCache] fix: Drain pending backups before internal Mamba write-back (#41092) --- .../srt/mem_cache/unified_radix_cache.py | 4 ++ .../test_unified_radix_cache_unittest.py | 45 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/python/sglang/srt/mem_cache/unified_radix_cache.py b/python/sglang/srt/mem_cache/unified_radix_cache.py index 59c75ef0fc..e8665f5cbe 100644 --- a/python/sglang/srt/mem_cache/unified_radix_cache.py +++ b/python/sglang/srt/mem_cache/unified_radix_cache.py @@ -1561,6 +1561,10 @@ class UnifiedRadixCache(BasePrefixCache): loop runs for leaves, reusable by component evictors ahead of an internal-state tombstone. Returns True once the backup is committed. """ + # An auxiliary backup may already cover this node under another ack. + # Finish it before building a new transfer and claiming ack ownership. + if self.ongoing_write_through: + self.writing_check(write_back=True) written = self._execute_and_commit_kv_backup( BackupKV(node_ids=[node_id]), write_back=True ) diff --git a/test/registered/unit/mem_cache/test_unified_radix_cache_unittest.py b/test/registered/unit/mem_cache/test_unified_radix_cache_unittest.py index 190eb6a344..79cdf03ca8 100644 --- a/test/registered/unit/mem_cache/test_unified_radix_cache_unittest.py +++ b/test/registered/unit/mem_cache/test_unified_radix_cache_unittest.py @@ -6248,6 +6248,51 @@ class UnifiedRadixCacheSuite: self._release_ongoing_load_back_locks(cache) cache.sanity_check() + def test_hicache_internal_mamba_backup_waits_for_pending_swa(self): + if not (self.cfg.has_swa and self.cfg.has_mamba): + self.skipTest("requires Full, SWA and Mamba components") + if _selected_tree_core_test_backend() == "rust": + self.skipTest("internal-node state demote is Python-core only") + page = self.cfg.page_size + cache, allocator, req_pool = build_fixture( + replace(self.cfg, sliding_window_size=3 * page) + ) + + def insert(length): + return self._insert( + cache, allocator, req_pool, list(range(length)) + ).last_device_node + + nodes = [insert(size) for size in range(page, 5 * page, page)] + grandparent, _, anchor, leaf = nodes + self._init_hicache(cache, write_policy="write_back") + cache.evict(EvictParams(mamba_num=3)) + cache.evict_host(page, ComponentType.FULL) + cache.evict(EvictParams(swa_num_tokens=3 * page)) + cache.evict_host(3 * page, ComponentType.SWA) + + # The backed parent stops the Full backup chain, while the SWA window + # reaches the unbacked grandparent and marks it under the anchor's ack. + insert(3 * page) + self.assertEqual( + cache.ongoing_write_through[anchor].publish_node_ids, nodes[:3] + ) + insert(page) + self.assertFalse(cache.tree_core.is_backuped(grandparent)) + self.assertIn( + ComponentType.MAMBA, cache.tree_core.build_backup_spec(grandparent)[1] + ) + leaf_lock = cache.inc_lock_ref(leaf).to_dec_params() + try: + result = cache.evict(EvictParams(mamba_num=1)) + self.assertEqual(result.mamba_num_evicted, 1) + self.assertIsNone(_device_value(cache, grandparent, ComponentType.MAMBA)) + self.assertIsNotNone(_host_value(cache, grandparent, ComponentType.MAMBA)) + self.assertFalse(cache.ongoing_write_through) + finally: + cache.dec_lock_ref(leaf, leaf_lock) + cache.sanity_check() + def test_hicache_write_through_internal_mamba_evict_keeps_drop(self): """Non-write_back policies keep the legacy tombstone-and-drop.""" if not self.cfg.has_mamba: