Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(permissions): quarantine a hung Automation probe when the wait ran out (#609)

Ruben Lütke595fb391958f
diff --git a/jarvis/platform/permission_service.py b/jarvis/platform/permission_service.py
index 2a368ff042..0cf26a2a85 100644
--- a/jarvis/platform/permission_service.py
+++ b/jarvis/platform/permission_service.py
@@ -148,6 +148,10 @@ _ORACLE_EVERY_S: Final = 10.0
_AUTOMATION_READ_TIMEOUT_S: Final = 5.0
_AUTOMATION_QUARANTINE_S: Final = 600.0
_AUTOMATION_REFRESH_WAIT_S: Final = 0.25
+# A timed wait can end a clock step before the deadline it was given: the
+# monotonic clock of Python 3.11 moves in 15.6 ms steps on Windows. A wait that
+# comes this close to a deadline counts as having reached it.
+_DEADLINE_SLACK_S: Final = 0.02
# After a native request FAILED (the call raised or the symbol was missing) a retry
# loop must not hammer it: the next automatic attempt waits this long. An explicit
@@ -682,8 +686,13 @@ class _AutomationGuard:
name="permission-automation-probe",
daemon=True,
).start()
+ waited_out = False
if wait_s > 0:
- call.done.wait(min(wait_s, max(0.0, call.deadline - time.monotonic())))
+ remaining = max(0.0, call.deadline - time.monotonic())
+ answered = call.done.wait(min(wait_s, remaining))
+ # A reader whose wait reached the deadline has seen the timeout,
+ # whatever the clock reads afterwards.
+ waited_out = not answered and remaining - wait_s <= _DEADLINE_SLACK_S
with self._lock:
if call.done.is_set():
if self._inflight.get(target) is call:
@@ -691,7 +700,7 @@ class _AutomationGuard:
if target in self._failed:
return _GuardRead("failed")
return _GuardRead("value", self._last.get(target))
- if time.monotonic() >= call.deadline:
+ if waited_out or time.monotonic() >= call.deadline:
self._quarantine_locked(target, self._clock())
return _GuardRead("quarantined")
return _GuardRead("pending", self._last.get(target))
diff --git a/tests/unit/platform/test_permission_service_hardening.py b/tests/unit/platform/test_permission_service_hardening.py
index ed30bf6b19..6634f56d74 100644
--- a/tests/unit/platform/test_permission_service_hardening.py
+++ b/tests/unit/platform/test_permission_service_hardening.py
@@ -169,6 +169,49 @@ def test_a_hung_automation_read_times_out_and_the_player_is_quarantined(
probe.release.set()
+class _ClockBetweenTwoSteps:
+ """Stands in for ``time`` in the service: a monotonic clock that has not moved on.
+
+ On Windows the monotonic clock of Python 3.11 advances in 15.6 ms steps, so a
+ wait can run out a moment before the clock reaches the deadline it was given.
+ Every other attribute is the real ``time``.
+ """
+
+ def __init__(self) -> None:
+ self._now = time.monotonic()
+
+ def monotonic(self) -> float:
+ return self._now
+
+ def __getattr__(self, name: str) -> Any:
+ return getattr(time, name)
+
+
+def test_a_read_that_waited_out_the_timeout_quarantines_whatever_the_clock_reads(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ monkeypatch.setattr(service_module, "time", _ClockBetweenTwoSteps())
+ probe = HungProbe()
+
+ def hung() -> PermissionState:
+ probe(_MUSIC, False)
+ return PermissionState.NOT_DETERMINED
+
+ try:
+ guard = service_module._AutomationGuard(
+ timeout_s=0.05, quarantine_s=600.0, clock=lambda: 0.0
+ )
+ assert guard.read(_MUSIC, hung, wait_s=0.05).kind == "quarantined"
+
+ # A reader that gave the probe less than its budget has seen no timeout.
+ patient = service_module._AutomationGuard(
+ timeout_s=5.0, quarantine_s=600.0, clock=lambda: 0.0
+ )
+ assert patient.read(_MUSIC, hung, wait_s=0.02).kind == "pending"
+ finally:
+ probe.release.set()
+
+
def test_an_automation_read_without_a_player_asks_nobody(make_env: Callable[..., Env]) -> None:
env = make_env(installed_players=[_MUSIC], running_players=[_MUSIC])