models_cache_lock.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. """Cross-process lock for ``provider_models_cache.json`` read-modify-write.
  2. Parallel prefetch workers, SWR refresh threads and other Hermes processes (dashboard, desktop,
  3. CLI) all rewrite the one cache file; each write must re-read it under this lock or a slow writer
  4. puts back the snapshot it loaded before its fetch, dropping rows written meanwhile.
  5. """
  6. import logging
  7. import threading
  8. from contextlib import contextmanager
  9. from pathlib import Path
  10. from typing import Callable
  11. logger = logging.getLogger(__name__)
  12. _thread_lock = threading.Lock()
  13. _holder = threading.local()
  14. @contextmanager
  15. def provider_models_cache_lock(path: Path):
  16. from hermes_cli.auth import _file_lock
  17. with _thread_lock, _file_lock(path.with_name(path.name + ".lock"), _holder, 10.0,
  18. f"Timed out waiting for the provider models cache lock ({path})"):
  19. yield
  20. def write_under_cache_lock(path: Path, write: Callable[[], None]) -> None:
  21. """Run ``write`` (re-read, set one row, save) under the thread + cross-process lock.
  22. The file is a cache: an unwritable home or a wedged lock skips the write instead of failing
  23. the model listing that triggered it."""
  24. try:
  25. with provider_models_cache_lock(path):
  26. write()
  27. except (OSError, TimeoutError):
  28. logger.debug("provider models cache write skipped (%s)", path, exc_info=True)