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(awareness): three Win32 defects in the window-focus watcher

rubenluetke10-beep9aeb301d8c34
diff --git a/jarvis/awareness/watchers/window.py b/jarvis/awareness/watchers/window.py
index b4eeb45bbc..e1d5958ab0 100644
--- a/jarvis/awareness/watchers/window.py
+++ b/jarvis/awareness/watchers/window.py
@@ -176,6 +176,9 @@ class WindowFocusWatcher:
# Sync-Side (Win32)
self._pump_thread: threading.Thread | None = None
self._hook_handle: int | None = None
+ # Guards the read-and-clear of _hook_handle: the pump thread and stop()
+ # both release the hook, from different threads. See _take_hook_handle.
+ self._hook_lock: threading.Lock = threading.Lock()
self._stop_event_handle: int | None = None
# The WINEVENTPROC instance MUST be kept alive — otherwise it is
# GC'd, the C pointer dangles, and Win32 calls invalid memory.
@@ -298,7 +301,8 @@ class WindowFocusWatcher:
)
# P4: Defensive UnhookWinEvent (primary path is in pump-loop finally)
- if self._hook_handle is not None:
+ hook_handle = self._take_hook_handle()
+ if hook_handle is not None:
try:
import ctypes # noqa: PLC0415
@@ -307,12 +311,11 @@ class WindowFocusWatcher:
u32 = ctypes.WinDLL("user32", use_last_error=True)
u32.UnhookWinEvent.argtypes = [ctypes.c_void_p]
u32.UnhookWinEvent.restype = ctypes.c_int
- u32.UnhookWinEvent(self._hook_handle)
+ u32.UnhookWinEvent(hook_handle)
except Exception: # noqa: BLE001, S110
# Defensive Win32 cleanup — do not escalate errors,
# otherwise the next phase step in stop() would hang.
pass
- self._hook_handle = None
# P5: CloseHandle stop_event
if self._stop_event_handle is not None:
@@ -341,6 +344,22 @@ class WindowFocusWatcher:
self._started = False
self._stopping = False
+ def _take_hook_handle(self) -> int | None:
+ """Atomically claim the hook handle so exactly ONE caller unhooks it.
+
+ Both ``stop()``'s defensive P4 and the pump thread's ``finally`` release
+ the hook, and P3's join is explicitly allowed to TIME OUT — so a pump
+ thread that is merely slow can be inside its cleanup while ``stop()``
+ reads the very same handle. Calling ``UnhookWinEvent`` twice on one
+ handle has a use-after-free shape: the second call either no-ops or,
+ once Win32 has recycled that handle value for a hook installed
+ elsewhere in the process, tears down SOMEONE ELSE's hook. Read-and-clear
+ under a lock makes the loser of that race see ``None`` and do nothing.
+ """
+ with self._hook_lock:
+ handle, self._hook_handle = self._hook_handle, None
+ return handle
+
def _reset_frame_state(self) -> None:
"""Drop queued frames and per-run change-detection state.
@@ -498,15 +517,17 @@ class WindowFocusWatcher:
except Exception: # noqa: BLE001
logger.exception("WindowFocusWatcher pump-loop crashed")
finally:
- # Unregister hook — on the SAME thread that registered it.
- if self._hook_handle is not None:
+ # Unregister hook — on the SAME thread that registered it. Claimed
+ # atomically so a stop() whose join timed out cannot unhook the same
+ # handle a second time (see _take_hook_handle).
+ handle = self._take_hook_handle()
+ if handle is not None:
try:
- user32.UnhookWinEvent(self._hook_handle)
+ user32.UnhookWinEvent(handle)
except Exception: # noqa: BLE001, S110
# Defensive Win32 cleanup — do not escalate errors,
# otherwise the next phase step in stop() would hang.
pass
- self._hook_handle = None
self._wineventproc_ref = None
# ---- Async-Side ---------------------------------------------------------
@@ -546,7 +567,16 @@ class WindowFocusWatcher:
# Dedupe: Win32 sometimes emits 2-3 EVENT_SYSTEM_FOREGROUND events
# within <50 ms during Alt+Tab. We filter here instead of in the
# callback (HN4).
- if hwnd == self._last_hwnd and (ts_ns - self._last_emit_ns) < _HWND_DEDUPE_NS:
+ #
+ # The lower bound matters: these timestamps come from time.time_ns(),
+ # a WALL clock that can step BACKWARDS (an NTP correction, the user
+ # fixing the clock, a VM resuming from suspend). A negative delta is
+ # also "< 50 ms", so a single backward step used to swallow every
+ # further event for the window that happened to be focused across it —
+ # focus tracking silently froze on that app until the clock caught up.
+ # A backward jump is not a duplicate, so it must not dedupe.
+ since_last_ns = ts_ns - self._last_emit_ns
+ if hwnd == self._last_hwnd and 0 <= since_last_ns < _HWND_DEDUPE_NS:
return
# Retrieve window title + PID + process name from hwnd — ctypes
@@ -559,6 +589,24 @@ class WindowFocusWatcher:
logger.debug("Window-meta lookup failed for hwnd=%d", hwnd, exc_info=True)
return
+ if not window_title.strip() and not process_name.strip():
+ # Nothing at all could be read about this window. _resolve_window_meta
+ # answers ("", 0, "") both for a genuine probe failure and for a
+ # window whose owner exited mid-lookup, and that is NOT a frame:
+ # publishing it handed PrivacyFilter two empty strings, which match
+ # no block pattern and therefore fall through to
+ # "default_allow_for_unknown" — so an unidentifiable window was
+ # waved through AND overwrote state.current_frame, blanking the
+ # foreground context the brain reads until the next app switch.
+ # The POSIX poller in this same class already refuses exactly this
+ # ("the privacy filter must never be handed an empty title to wave
+ # through"); the Win32 path never got the same guard.
+ logger.debug(
+ "Win32 window meta unresolvable for hwnd=%d — probe discarded, "
+ "frame not published", hwnd,
+ )
+ return
+
await self._emit_frame(
ts_ns=ts_ns, handle=hwnd, window_title=window_title,
pid=pid, process_name=process_name,
diff --git a/tests/unit/awareness/test_window_watcher.py b/tests/unit/awareness/test_window_watcher.py
index 5e54e6e731..dc3d3fb44f 100644
--- a/tests/unit/awareness/test_window_watcher.py
+++ b/tests/unit/awareness/test_window_watcher.py
@@ -187,6 +187,84 @@ async def test_stop_idempotent_no_start() -> None:
await watcher.stop()
+async def test_unresolvable_window_is_not_published_as_a_frame() -> None:
+ """``_resolve_window_meta`` answers ("", 0, "") for a probe that failed.
+
+ Publishing that handed PrivacyFilter two empty strings, which match no
+ block pattern and fall through to ``default_allow_for_unknown`` — so an
+ unidentifiable window was waved through AND overwrote
+ ``state.current_frame``, blanking the foreground context the brain reads.
+ The POSIX poller already refused exactly this; Win32 never did.
+ """
+ bus, manager, privacy = _make_components()
+ received: list[FrameUpdated] = []
+ blocked: list[AwarenessCaptureBlocked] = []
+ bus.subscribe(FrameUpdated, _async_collect(received))
+ bus.subscribe(AwarenessCaptureBlocked, _async_collect(blocked))
+
+ watcher = WindowFocusWatcher(manager=manager, privacy=privacy, bus=bus)
+ with patch.object(
+ WindowFocusWatcher, "_resolve_window_meta",
+ staticmethod(lambda hwnd: ("", 0, "")),
+ ):
+ watcher._loop = asyncio.get_running_loop()
+ watcher._safe_enqueue((time.time_ns(), 100))
+ await watcher._drain_once()
+
+ assert received == []
+ assert blocked == []
+ assert manager.state.current_frame is None
+
+
+async def test_a_title_less_window_still_publishes_when_the_process_is_known() -> None:
+ """Only a TOTALLY unreadable probe is discarded: a window with no caption
+ but a known owner still says which app is focused, and the process-based
+ privacy rules can act on it."""
+ bus, manager, privacy = _make_components()
+ received: list[FrameUpdated] = []
+ bus.subscribe(FrameUpdated, _async_collect(received))
+
+ watcher = WindowFocusWatcher(manager=manager, privacy=privacy, bus=bus)
+ with patch.object(
+ WindowFocusWatcher, "_resolve_window_meta",
+ staticmethod(lambda hwnd: ("", 4321, "notepad.exe")),
+ ):
+ watcher._loop = asyncio.get_running_loop()
+ watcher._safe_enqueue((time.time_ns(), 100))
+ await watcher._drain_once()
+
+ assert len(received) == 1
+ assert received[0].process_name == "notepad.exe"
+
+
+async def test_a_backward_clock_step_does_not_dedupe_away_later_frames() -> None:
+ """The dedupe delta comes from the WALL clock, which can step backwards
+ (NTP correction, a resumed VM). A negative delta is also "< 50 ms", so one
+ backward step used to swallow every further event for the focused window
+ and freeze focus tracking on that app until the clock caught up."""
+ bus, manager, privacy = _make_components()
+ received: list[FrameUpdated] = []
+ bus.subscribe(FrameUpdated, _async_collect(received))
+
+ watcher = WindowFocusWatcher(manager=manager, privacy=privacy, bus=bus)
+ with patch.object(
+ WindowFocusWatcher, "_resolve_window_meta",
+ staticmethod(lambda hwnd: ("My Doc - Notepad", 1, "notepad.exe")),
+ ):
+ watcher._loop = asyncio.get_running_loop()
+ ts = time.time_ns()
+ watcher._safe_enqueue((ts, 100))
+ await watcher._drain_once()
+ # The clock jumps a minute back; the same window is focused again.
+ watcher._safe_enqueue((ts - 60_000_000_000, 100))
+ await watcher._drain_once()
+
+ assert len(received) == 2
+
+
@pytest.mark.asyncio
async def test_resolve_meta_failure_skips_frame_no_crash() -> None:
"""If _resolve_window_meta raises: the frame is dropped, no crash."""