fix: prevent false timeout failures and release 1.2.2
This commit is contained in:
1 parent
1dc2b0c686
commit
e1e26772d9
18 files changed
+189
-22
No files matched your search
@@ -113,22 +113,28 @@ class ScreenAwakeLease:
|
||||
}
|
||||
|
||||
@contextmanager
|
||||
def _mutex(self):
|
||||
def _mutex(self, *, wait_timeout_seconds: float | None = None):
|
||||
self.mutex_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
started_at = time.monotonic()
|
||||
timeout = (
|
||||
self._MUTEX_TIMEOUT_SECONDS
|
||||
if wait_timeout_seconds is None
|
||||
else max(0.0, wait_timeout_seconds)
|
||||
)
|
||||
acquired = False
|
||||
while not acquired:
|
||||
try:
|
||||
fd = os.open(self.mutex_path, os.O_CREAT | os.O_EXCL | os.O_WRONLY)
|
||||
except FileExistsError:
|
||||
if time.monotonic() - started_at >= timeout:
|
||||
raise TimeoutError("Timed out acquiring the Artemis awake-lease mutex.")
|
||||
if self._path_is_stale(self.mutex_path):
|
||||
try:
|
||||
self.mutex_path.unlink(missing_ok=True)
|
||||
except OSError:
|
||||
pass
|
||||
continue
|
||||
if time.monotonic() - started_at >= self._MUTEX_TIMEOUT_SECONDS:
|
||||
raise TimeoutError("Timed out acquiring the Artemis awake-lease mutex.")
|
||||
else:
|
||||
continue
|
||||
time.sleep(self._MUTEX_POLL_SECONDS)
|
||||
else:
|
||||
try:
|
||||
@@ -274,10 +280,15 @@ class ScreenAwakeLease:
|
||||
def cleanup_unowned_references(self) -> bool:
|
||||
"""Drain legacy shell wake locks only when no live old host owns them."""
|
||||
try:
|
||||
with self._mutex():
|
||||
# Migration cleanup is optional. A busy owner must not delay every
|
||||
# queued task by the normal 10-second lease-acquisition deadline.
|
||||
with self._mutex(wait_timeout_seconds=1.0):
|
||||
if self._live_leases():
|
||||
return False
|
||||
return self._drain_references()
|
||||
except TimeoutError:
|
||||
logger.debug(f"Skipped optional wake-lock cleanup on {self.device_id}: mutex busy")
|
||||
return False
|
||||
except Exception as exc:
|
||||
logger.warning(f"Could not clean unowned screen wake locks on {self.device_id}: {exc}")
|
||||
return False
|
||||
@@ -95,9 +95,10 @@ def _configure_usb_stay_awake(device_id: str) -> str | None:
|
||||
if not _awake_enabled() or os.environ.get("ARTEMIS_CLOUD_MODE") == "1":
|
||||
return None
|
||||
|
||||
# Migration cleanup: previous Artemis versions used a persistent shell
|
||||
# wake lock. Drain it only after every live legacy owner has exited.
|
||||
ScreenAwakeLease(device_id).cleanup_unowned_references()
|
||||
# Migration cleanup belongs to the long-lived host. A queued task worker
|
||||
# must not wait on another process's optional legacy wake-lock cleanup.
|
||||
if os.environ.get("ARTEMIS_TASK_WORKER") != "1":
|
||||
ScreenAwakeLease(device_id).cleanup_unowned_references()
|
||||
|
||||
commands = (
|
||||
(
|
||||
|
||||
Reference in new issue
Block a user