From b4f7dd75af605e705604603d07b7b2be6bf06eed Mon Sep 17 00:00:00 2001 From: Will Miao Date: Tue, 12 May 2026 22:50:10 +0800 Subject: [PATCH] fix(persistent-cache): persist scanner cache after model deletion After deleting a model, the in-memory scanner cache was updated but the SQLite persistent cache was not. On server restart, the stale persistent cache caused check_model_version_exists() to return True, blocking re-download with 'Model version already exists'. Add _persist_current_cache() calls in both deletion paths: - ModelLifecycleService.delete_model() (used by versions tab delete) - delete_model_version handler in MiscHandlers --- py/routes/handlers/misc_handlers.py | 11 +++++++++++ py/services/model_lifecycle_service.py | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/py/routes/handlers/misc_handlers.py b/py/routes/handlers/misc_handlers.py index 638b9929..671d7950 100644 --- a/py/routes/handlers/misc_handlers.py +++ b/py/routes/handlers/misc_handlers.py @@ -2139,6 +2139,17 @@ class ModelLibraryHandler: ] await found_cache.resort() + scanner_map = { + "lora": lora_scanner, + "checkpoint": checkpoint_scanner, + "embedding": embedding_scanner, + } + scanner = scanner_map.get(found_type) + if scanner: + persist = getattr(scanner, "_persist_current_cache", None) + if callable(persist): + await persist() + history_service = await self._get_download_history_service() await history_service.mark_not_downloaded(found_type, model_version_id) diff --git a/py/services/model_lifecycle_service.py b/py/services/model_lifecycle_service.py index 001752d9..8c4fe5cf 100644 --- a/py/services/model_lifecycle_service.py +++ b/py/services/model_lifecycle_service.py @@ -111,6 +111,11 @@ class ModelLifecycleService: self._scanner._hash_index.remove_by_path(file_path) await self._sync_update_for_model(model_id) + + persist_current_cache = getattr(self._scanner, "_persist_current_cache", None) + if callable(persist_current_cache): + await persist_current_cache() + return {"success": True, "deleted_files": deleted_files} @staticmethod