From 41628dc1b110681811df53615e1e221acd9c8e30 Mon Sep 17 00:00:00 2001 From: Teng Ma <805522925@qq.com> Date: Sun, 7 Sep 2025 13:59:58 +0800 Subject: [PATCH] [HiCache] fix: check clear() method for storage backend (#10096) Co-authored-by: hzh0425 --- python/sglang/srt/mem_cache/hiradix_cache.py | 21 ++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/python/sglang/srt/mem_cache/hiradix_cache.py b/python/sglang/srt/mem_cache/hiradix_cache.py index d97b0033a..a861e233e 100644 --- a/python/sglang/srt/mem_cache/hiradix_cache.py +++ b/python/sglang/srt/mem_cache/hiradix_cache.py @@ -134,11 +134,24 @@ class HiRadixCache(RadixCache): height += 1 return height - def clear_storage_backend(self): + def clear_storage_backend(self) -> bool: if self.enable_storage: - self.cache_controller.storage_backend.clear() - logger.info("Hierarchical cache storage backend cleared successfully!") - return True + try: + # Check if the storage backend has a clear method (for nixl backends) + if hasattr(self.cache_controller.storage_backend, "clear"): + self.cache_controller.storage_backend.clear() + logger.info( + "Hierarchical cache storage backend cleared successfully!" + ) + return True + else: + logger.warning( + f"Storage backend {type(self.cache_controller.storage_backend).__name__} does not support clear operation." + ) + return False + except Exception as e: + logger.error(f"Failed to clear hierarchical cache storage backend: {e}") + return False else: logger.warning("Hierarchical cache storage backend is not enabled.") return False