status_lock_dir.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. """The unusable scoped-lock dir report (#134889 W2). :mod:`gateway.status` raises it from
  2. ``acquire_scoped_lock``; nothing here reads back into that module."""
  3. from __future__ import annotations
  4. import logging
  5. from pathlib import Path
  6. from hermes_dir_acl import unusable_dir_remedy
  7. log = logging.getLogger("gateway.status")
  8. class ScopedLockDirUnusable(PermissionError):
  9. """This token cannot create, read or replace locks in the scoped-lock dir (#134889 W2).
  10. On Windows an elevated install/update creates ``gateway-locks`` owned by Administrators with
  11. no entry for the user; the same user's non-elevated gateway then gets ``Permission denied`` on
  12. every token lock. The adapter reports it once and names the repair; it stays retryable (the
  13. dir is shared by every token-locked adapter), so a repair is picked up without a restart. A
  14. ``PermissionError`` subclass so existing callers keep their handling.
  15. """
  16. # Lock dirs already reported unusable by this process: WARNING once per dir, DEBUG after, since the
  17. # reconnect watcher and every adapter sharing the dir re-enter the acquire path.
  18. _warned: set[Path] = set()
  19. def unusable_lock_dir(lock_dir: Path, exc: PermissionError) -> ScopedLockDirUnusable:
  20. """Log *lock_dir* as unusable (WARNING once, then DEBUG) and return the error to raise."""
  21. level = logging.DEBUG if lock_dir in _warned else logging.WARNING
  22. _warned.add(lock_dir)
  23. log.log(level, "Gateway lock directory %s is not usable by this process (%s); it was likely created "
  24. "by an elevated (administrator) Hermes. Platforms that need a token lock stay down until it "
  25. "is repaired. To repair, %s", lock_dir, exc, unusable_dir_remedy(lock_dir))
  26. return ScopedLockDirUnusable(exc.errno, f"gateway lock directory is not usable: {exc}", str(lock_dir))
  27. def unusable_lock_dir_recovered(lock_dir: Path) -> None:
  28. """A lock was taken in *lock_dir*: warn again if it ever becomes unusable later."""
  29. _warned.discard(lock_dir)
  30. def reset_unusable_lock_dir_reports() -> None:
  31. """Forget every report (a fresh process's view; tests)."""
  32. _warned.clear()
  33. def unusable_lock_dir_reported(lock_dir: Path) -> bool:
  34. return lock_dir in _warned