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(jarvisbar): stop a pending respawn from resurrecting a dropped overlay

Ruben Lütke7f1e2540cb9b
diff --git a/docs/BUGS.md b/docs/BUGS.md
index 033e1ba053..369597b093 100644
--- a/docs/BUGS.md
+++ b/docs/BUGS.md
@@ -8240,3 +8240,51 @@ that a transparency path works, check it in the state the report describes —
an unstalled probe cannot falsify a stall-triggered defect. When a UI defect's
rectangle matches a window exactly and its colour is one the renderer never
emits, suspect a substituted window before suspecting the renderer.
+
+---
+
+## BUG-119: a unit run leaves a SECOND, permanently visible JarvisBar on the developer's desktop (MEDIUM, FIXED 2026-07-28)
+
+**Symptom (maintainer field report).** Two bars on screen at once, slightly
+offset and of different sizes. The second one belonged to no Jarvis instance:
+`python -m jarvis.ui.jarvisbar.host`, parented to
+`pytest tests/unit -q --timeout=180`, still visible long after that test had
+moved on. Different geometry from the app's own bar (72x30 vs 93x39) because it
+booted from a different scale — the tell that it was a foreign process, not a
+duplicated window.
+
+**Root cause.** `SubprocessBarOverlay` schedules its bounded auto-respawn as
+`threading.Thread(target=self._respawn_after_backoff, ...)`. A **bound method
+keeps the surface alive**, so the thread holds the whole overlay across its
+5 s backoff. In a test the fake host's scripted stdout EOFs immediately, the
+pump reads "host is gone", and a respawn is scheduled — but by the time the
+sleep ends, `monkeypatch` has reverted and `subprocess.Popen` is REAL again.
+The respawn then starts a genuine host process, which on Windows builds a real
+Tk bar (`QT_QPA_PLATFORM=offscreen` binds Qt only, and the Qt host test is
+Darwin-gated anyway) and shows it on the developer's desktop, where nothing
+ever reaps it.
+
+`test_subprocess_overlay.py` already carried an autouse fixture marking every
+live proxy `_stopping` for exactly this reason — but it is per-file, so any
+other module that constructs one is unprotected, and it only papers over a
+lifetime bug in the production path.
+
+**Fix.** The backoff is served by a module-level
+`_respawn_after_backoff_weakly(weakref.ref(surface), attempt, backoff)`, which
+sleeps holding only a **weak** reference and abandons the attempt when the
+overlay has been released. "Nobody holds this overlay" now means "there is no
+bar to restore, so do not spawn a host". The live path is unchanged: the
+desktop app holds its surface, so real respawns still fire.
+
+**Guards.** `tests/unit/ui/jarvisbar/test_subprocess_overlay.py` — a released
+surface abandons its pending respawn, a live one still respawns, and (the real
+regression) a proxy with a respawn pending is still garbage-collectable once
+dropped. Verified sharp: with the previous bound-method target that last
+assertion fails.
+
+**Class rule.** A thread that sleeps on behalf of an object must not own it.
+Any delayed retry/backoff/watchdog that targets a bound method silently extends
+its subject's lifetime past the point where anything wants it — and when the
+retry's side effect is spawning a process or a window, the leak becomes
+user-visible on a machine nobody is testing on. Hold the subject weakly and
+treat collection as cancellation.
diff --git a/jarvis/ui/jarvisbar/subprocess_overlay.py b/jarvis/ui/jarvisbar/subprocess_overlay.py
index 9450878499..123c3e1a49 100644
--- a/jarvis/ui/jarvisbar/subprocess_overlay.py
+++ b/jarvis/ui/jarvisbar/subprocess_overlay.py
@@ -34,11 +34,37 @@ import subprocess
import sys
import threading
import time
+import weakref
from collections.abc import Callable
from typing import IO, Any
log = logging.getLogger("jarvis.ui.jarvisbar")
+
+def _respawn_after_backoff_weakly(
+ weak_surface: weakref.ReferenceType[SubprocessBarOverlay],
+ attempt: int,
+ backoff: float,
+) -> None:
+ """Serve a respawn backoff without keeping the overlay alive.
+
+ Deliberately a module function taking a weak reference, not a bound
+ method: a sleeping thread that holds the surface strongly resurrects an
+ overlay nobody wants anymore, and spawns a REAL host process — with a real
+ window — once the sleep ends. An overlay that has been dropped has no bar
+ to restore, so the correct respawn is none.
+ """
+ time.sleep(backoff)
+ surface = weak_surface()
+ if surface is None:
+ log.debug(
+ "JarvisBar respawn attempt %d abandoned — the overlay it belonged "
+ "to was released during the backoff.",
+ attempt,
+ )
+ return
+ surface._respawn_after_backoff(attempt)
+
# Kept in sync with renderer.MODES without importing numpy/PIL into the
# parent for a pure IPC proxy; the host-side bar re-validates every mode.
_MODES = ("idle", "listen", "speak", "think")
@@ -380,22 +406,30 @@ class SubprocessBarOverlay:
self._RESPAWN_MAX_ATTEMPTS,
self._RESPAWN_BACKOFF_SECONDS,
)
+ # The thread must NOT hold this surface alive across the backoff.
+ # A bound method would: the sleeping thread keeps a strong ref, so
+ # an overlay nobody wants anymore still reaches Popen minutes later
+ # and puts a real bar window on the user's screen. That is exactly
+ # how a unit run leaked a second, permanently visible bar — the
+ # test's Popen fake was long unpatched by the time the sleep ended.
+ # A weakref makes "nobody holds this overlay" mean "no respawn".
threading.Thread(
- target=self._respawn_after_backoff,
- args=(attempt,),
+ target=_respawn_after_backoff_weakly,
+ args=(weakref.ref(self), attempt, self._RESPAWN_BACKOFF_SECONDS),
name=f"{self._RESPAWN_THREAD_NAME}-{attempt}",
daemon=True,
).start()
def _respawn_after_backoff(self, attempt: int) -> None:
- """Wait out the backoff, then respawn — runs on its own daemon thread.
-
- Never touches the caller's thread or the app's event loop: the sleep
- and the Popen call both happen here. A death within the backoff
- window (``stop()`` called while waiting) aborts the attempt instead
- of spawning a host nobody wants anymore.
+ """Respawn the host — runs on its own daemon thread, backoff already served.
+
+ Never touches the caller's thread or the app's event loop: the Popen
+ call happens here. The backoff itself is served by
+ :func:`_respawn_after_backoff_weakly`, which holds only a weak
+ reference while it sleeps. A death within that window (``stop()``
+ called while waiting) aborts the attempt instead of spawning a host
+ nobody wants anymore.
"""
- time.sleep(self._RESPAWN_BACKOFF_SECONDS)
if self._stopping:
return
diff --git a/tests/unit/ui/jarvisbar/test_subprocess_overlay.py b/tests/unit/ui/jarvisbar/test_subprocess_overlay.py
index adc1142ab7..1ad390085c 100644
--- a/tests/unit/ui/jarvisbar/test_subprocess_overlay.py
+++ b/tests/unit/ui/jarvisbar/test_subprocess_overlay.py
@@ -12,6 +12,7 @@ import gc
import io
import json
import threading
+import weakref
import pytest
@@ -584,3 +585,61 @@ def test_mascot_surface_contract_matches_the_bar_proxy() -> None:
assert getattr(SubprocessMascotOverlay, name, None) is not None, name
# Same reset-path contract as NullOverlay: no _root instance attribute.
assert not hasattr(SubprocessMascotOverlay(), "_root")
+
+
+# --------------------------------------------------------------------------- #
+# A released overlay must never respawn (leaked a second visible bar) #
+# --------------------------------------------------------------------------- #
+def test_a_released_overlay_abandons_its_pending_respawn() -> None:
+ """The backoff thread must hold the surface WEAKLY.
+
+ A bound-method thread target keeps a dropped overlay alive across the
+ backoff and then calls the real ``subprocess.Popen`` — which is how a unit
+ run left a second, permanently visible bar on the developer's desktop long
+ after the test that created it had finished.
+ """
+ calls: list[int] = []
+
+ class _Surface:
+ def _respawn_after_backoff(self, attempt: int) -> None:
+ calls.append(attempt)
+
+ surface = _Surface()
+ ref = weakref.ref(surface)
+ del surface
+ gc.collect()
+ assert ref() is None, "the fake surface must be collectable"
+
+ mod._respawn_after_backoff_weakly(ref, 1, 0.0)
+ assert calls == [], "a released overlay must not spawn a host"
+
+
+def test_a_live_overlay_still_respawns_after_the_backoff() -> None:
+ """The weakref must not break the feature it guards."""
+ calls: list[int] = []
+
+ class _Surface:
+ def _respawn_after_backoff(self, attempt: int) -> None:
+ calls.append(attempt)
+
+ surface = _Surface()
+ mod._respawn_after_backoff_weakly(weakref.ref(surface), 2, 0.0)
+ assert calls == [2]
+
+
+def test_a_pending_respawn_does_not_keep_the_overlay_alive(monkeypatch) -> None:
+ """End to end on the real proxy: schedule a respawn, drop every reference,
+ and the overlay must still be collectable — proof no thread holds it."""
+ monkeypatch.setattr(SubprocessBarOverlay, "_RESPAWN_BACKOFF_SECONDS", 30.0)
+ surface, proc = _started_proxy(monkeypatch)
+ proc._returncode = 1 # the host died -> a respawn gets scheduled
+ surface._log_dead_once()
+ assert surface._respawn_attempts == 1, "the respawn must actually be pending"
+
+ ref = weakref.ref(surface)
+ del surface, proc
+ gc.collect()
+ assert ref() is None, (
+ "a thread is still holding the overlay across its backoff — it will "
+ "spawn a real host window once the sleep ends"
+ )