DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
fix(settings): serialize catalog migration reads with updates (#1984)
Bingxi Zhao (Frank)8a228e40f795
diff --git a/deeptutor/services/config/model_catalog.py b/deeptutor/services/config/model_catalog.pyindex 3d9b6c8da..5763171d6 100644--- a/deeptutor/services/config/model_catalog.py+++ b/deeptutor/services/config/model_catalog.py@@ -267,23 +267,27 @@ class ModelCatalogService:return cls._instances[key]def load(self) -> dict[str, Any]:- loaded = self._read_existing_catalog()- if loaded:+ # Reads may migrate defaults. Serialize the complete read/migrate/write+ # with settings updates so an older snapshot cannot overwrite a save+ # or hold the destination open during this instance's replace (#1984).+ with self._lock:+ loaded = self._read_existing_catalog()+ if loaded:+ catalog = _default_catalog()+ catalog.update({k: v for k, v in loaded.items() if k != "services"})+ catalog["services"].update(loaded.get("services", {}))+ merged_defaults = catalog != loaded+ before = deepcopy(catalog)+ self._normalize(catalog)+ if merged_defaults or catalog != before:+ self.save(catalog)+ return catalog+catalog = _default_catalog()- catalog.update({k: v for k, v in loaded.items() if k != "services"})- catalog["services"].update(loaded.get("services", {}))- merged_defaults = catalog != loaded- before = deepcopy(catalog)self._normalize(catalog)- if merged_defaults or catalog != before:- self.save(catalog)+ self.save(catalog)return catalog- catalog = _default_catalog()- self._normalize(catalog)- self.save(catalog)- return catalog-def _read_existing_catalog(self) -> dict[str, Any]:if not self.path.exists() or self.path.stat().st_size == 0:return {}diff --git a/tests/services/test_model_catalog.py b/tests/services/test_model_catalog.pyindex 911c50bf3..946015ecd 100644--- a/tests/services/test_model_catalog.py+++ b/tests/services/test_model_catalog.py@@ -1,6 +1,7 @@from concurrent.futures import ThreadPoolExecutorimport jsonfrom pathlib import Path+import threadingfrom typing import Anyimport pytest@@ -116,6 +117,51 @@ def test_load_recovers_invalid_catalog_with_defaults(tmp_path: Path):assert set(saved["services"]) == expected_services+def test_catalog_migration_read_cannot_overwrite_concurrent_settings_update(tmp_path, monkeypatch):+ """A runtime read may migrate defaults while the settings endpoint saves."""+ path = tmp_path / "model_catalog.json"+ path.write_text(json.dumps({"version": 1, "services": {}}), encoding="utf-8")+ service = ModelCatalogService(path)+ read = service._read_existing_catalog+ captured = threading.Event()+ release = threading.Event()+ updating = threading.Event()+ reader_id = None++ def paused_read():+ value = read()+ if threading.get_ident() == reader_id:+ captured.set()+ assert release.wait(5)+ return value++ def reader():+ nonlocal reader_id+ reader_id = threading.get_ident()+ return service.load()++ def writer():+ updating.set()+ return service.update(lambda catalog: catalog.update({"saved_selection": "new-model"}))++ monkeypatch.setattr(service, "_read_existing_catalog", paused_read)+ with ThreadPoolExecutor(max_workers=2) as pool:+ loading = pool.submit(reader)+ try:+ assert captured.wait(5)+ saving = pool.submit(writer)+ assert updating.wait(5)+ try:+ saving.result(timeout=1)+ except TimeoutError:+ pass # Correct serialization waits until the migration read completes.+ finally:+ release.set()+ loading.result(timeout=5)+ saving.result(timeout=5)+ assert json.loads(path.read_text())["saved_selection"] == "new-model"++@pytest.mark.parametrize("operation", ["load", "update", "apply"])@pytest.mark.parametrize("error_type", [OSError, PermissionError])def test_catalog_read_error_preserves_existing_file(