backend_retirement.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. """Process-owned admission fence for cooperative Desktop backend retirement.
  2. The gate belongs to the process, not a profile or transport. Diagnostic ledgers alone cannot
  3. close the check/use race: every source of new work must reserve admission before dispatch.
  4. """
  5. from contextlib import contextmanager
  6. import logging
  7. import secrets
  8. import threading
  9. import time
  10. class RetirementFence:
  11. """Exclusive 30-second prepare permit; commit closes admission permanently.
  12. Tokens are random and recognized only by this process instance. A committed prepare
  13. returns the same token so a client can recover a lost commit response without reopening.
  14. """
  15. def __init__(self):
  16. self._lock = threading.Lock()
  17. self._active = 0
  18. self._preparing = False
  19. self._token = None
  20. self._committed = False
  21. self._expires_at = 0.0
  22. self._now = time.monotonic
  23. def _expire(self):
  24. if self._token is not None and not self._committed and self._now() >= self._expires_at:
  25. self._token = None
  26. def acquire(self):
  27. with self._lock:
  28. self._expire()
  29. if self._preparing or self._token is not None:
  30. return False
  31. self._active += 1
  32. return True
  33. def release(self):
  34. with self._lock:
  35. self._active -= 1
  36. def active_count(self):
  37. with self._lock:
  38. return self._active
  39. @contextmanager
  40. def work(self):
  41. admitted = self.acquire()
  42. try:
  43. yield admitted
  44. finally:
  45. if admitted:
  46. self.release()
  47. def prepare(self):
  48. from hermes_cli.web_server_idle_proof import idle_proof
  49. with self._lock:
  50. self._expire()
  51. if self._committed:
  52. return {"ok": True, "idle": True, "token": self._token}
  53. if self._preparing or self._token is not None or self._active:
  54. return {"ok": False, "idle": False}
  55. self._preparing = True
  56. # No admissions can cross the freeze. Do not hold our lock while reading ledgers:
  57. # some callers already hold a session/cron lock when reserving their work.
  58. verdict = None
  59. try:
  60. verdict = idle_proof()["idle"]
  61. except Exception:
  62. logging.getLogger(__name__).warning("Retirement idle probe unavailable", exc_info=True)
  63. finally:
  64. with self._lock:
  65. self._preparing = False
  66. if verdict is True:
  67. self._token = token = secrets.token_urlsafe(32)
  68. self._expires_at = self._now() + 30.0
  69. if verdict is not True:
  70. return {"ok": False, "idle": verdict}
  71. return {"ok": True, "idle": True, "token": token}
  72. def commit(self, token):
  73. with self._lock:
  74. self._expire()
  75. if not token or token != self._token:
  76. return {"ok": False}
  77. self._committed = True
  78. return {"ok": True}
  79. def cancel(self, token):
  80. with self._lock:
  81. self._expire()
  82. if not token or token != self._token or self._committed:
  83. return {"ok": False}
  84. self._token = None
  85. return {"ok": True}
  86. retirement = RetirementFence()