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(tray): never create the pystray status item off the main thread on macOS (BUG-056)

Ruben Lütke072115aa8999
diff --git a/docs/BUGS.md b/docs/BUGS.md
index 40ed82ffab..9d616cf697 100644
--- a/docs/BUGS.md
+++ b/docs/BUGS.md
@@ -4015,3 +4015,50 @@ executed-tool evidence. Guards:
enumeration tool; search + single-read cannot answer it groundedly, and a
schema/contract page served without provenance becomes hallucination fuel.
Voice-facing agentic loops need a TIME bound, not just a round bound.
+
+## BUG-056: First macOS boot aborts natively — pystray NSStatusItem created off the main thread (HIGH, FIXED 2026-07-14)
+
+**Symptom.** Fresh install on a real Mac (install.sh → Python 3.14 venv →
+first launch): the app dies during boot with the macOS crash dialog
+"Python quit unexpectedly". Terminal shows
+``objc.error: NSInternalInconsistencyException — NSWindow drag regions
+should only be invalidated on the Main Thread!`` followed by a native
+AppKit assertion (``NSViewSetCurrentlyBuildingLayerTreeForDisplay``,
+NSView.m:13412) and a hard process abort. "Reopen"/"Ignore" do nothing —
+every relaunch dies at the same point. First-boot is bricked on EVERY Mac,
+deterministically; not machine-specific.
+
+**Root cause.** ``JarvisTray.start()`` spawns ``_run()`` on a worker thread
+(``jarvis-tray``); ``_run()`` constructs ``pystray.Icon`` whose darwin
+backend creates an ``NSStatusItem`` in ``Icon.__init__``
+(``pystray/_darwin.py:60``). AppKit is main-thread-only: the first objc
+exception IS raised into Python (and would be caught by ``_run``'s
+try/except), but the half-built status item then trips a **native C-level
+assertion inside AppKit — an ``abort()`` below Python that no try/except
+can catch**. Windows/Linux tolerate off-main-thread tray icons, so the bug
+was invisible on the maintainer's machine and in CI; the cross-platform
+plan had classified ``jarvis/ui/tray.py`` as "already cross-platform" and
+macOS was ``unverified-on-real-desktop`` (JARVIS-20 CP-13..15). The first
+real-Mac run found the gap. Python 3.14 is NOT the cause (install.sh's
+candidate order is correct); the crash is purely the thread violation.
+
+**Fix (2026-07-14).** ``JarvisTray.start()`` gates on
+``sys.platform == "darwin"`` and degrades to a logged English no-op
+(AD-6/AD-11: degrade, never crash) — the single choke point covering all
+four call sites (``__main__.py`` tray app, ``desktop_app.py``,
+``overlay/tray_surface.py``, ``ui/shell/shell.py``). The desktop window and
+Dock icon remain the macOS surface. Guard:
+``tests/unit/ui/test_tray.py::test_tray_start_is_noop_on_macos``.
+
+**Follow-up (needs real Mac hardware).** A real macOS menu-bar icon
+requires main-thread hosting: ``pystray.Icon(...,
+darwin_nsapplication=<pywebview's NSApplication>)`` + ``run_detached()``,
+with the icon constructed on the main thread (e.g. PyObjC
+``performSelectorOnMainThread``). Tracked in
+``docs/plans/cross-platform-mac-linux/FIX-TRACKER.md``.
+
+**Class rule.** ANY AppKit/UI object on macOS (status items, windows,
+menus) must be created and driven on the main thread; a worker-thread
+violation is a native abort, not a catchable exception. "No platform
+marker in the code" does not mean cross-platform — threading contracts
+differ per OS, and only a real-device boot proves them.
diff --git a/docs/plans/cross-platform-mac-linux/FIX-TRACKER.md b/docs/plans/cross-platform-mac-linux/FIX-TRACKER.md
index 4f9c52931a..ec6d980f4c 100644
--- a/docs/plans/cross-platform-mac-linux/FIX-TRACKER.md
+++ b/docs/plans/cross-platform-mac-linux/FIX-TRACKER.md
@@ -73,3 +73,23 @@ and every fix here were proven at the seam level on a Windows host via platform
fakes / forced `detect_platform`; none was run on real macOS/Linux hardware. Nothing
is pushed (local `main` only). Out of scope for this loop: the B2 browser-audio
bridge (a feature, not a fix) and the real-hardware sign-off.
+
+## POST-LOOP ADDENDUM (2026-07-14) — first real-Mac boot falsified M5's macOS assumption
+
+The first fresh-install boot on real Mac hardware (the live sign-off owed above)
+aborted natively: pystray's darwin backend creates an `NSStatusItem` in
+`Icon.__init__`, and AppKit is main-thread-only — from the `jarvis-tray` worker
+thread that is an uncatchable C-level abort ("Python quit unexpectedly"), not a
+Python exception, so M5's `_run` try/except never helps. M5's line
+"Windows/macOS `display_present()`=True, so the tray still starts normally" was
+wrong for macOS: the tray *started* and killed the whole app. Full forensics:
+`docs/BUGS.md` **BUG-056**.
+
+- **Fix (committed):** `JarvisTray.start()` now gates on `sys.platform ==
+ "darwin"` → logged English no-op (AD-6/AD-11); desktop window + Dock icon
+ remain the macOS surface. Guard:
+ `tests/unit/ui/test_tray.py::test_tray_start_is_noop_on_macos`.
+- [ ] **Follow-up (needs real Mac hardware):** real menu-bar icon via
+ main-thread hosting — construct the icon on the main thread and
+ `run_detached(darwin_nsapplication=<pywebview's NSApplication>)`; verify
+ against a live pywebview run loop before lifting the gate.
diff --git a/jarvis/ui/tray.py b/jarvis/ui/tray.py
index 3b4924f30d..7b20248209 100644
--- a/jarvis/ui/tray.py
+++ b/jarvis/ui/tray.py
@@ -5,12 +5,15 @@ Higher-quality .ico files can later be placed under assets/icons/.
Threading: pystray runs on its own thread (not asyncio-capable).
It communicates with the event loop via a thread-safe queue.
+macOS: AppKit forbids that worker thread (main-thread-only UI), so start()
+is a logged no-op there until the icon is hosted on the main thread.
"""
from __future__ import annotations
import asyncio
import logging
import queue
+import sys
import threading
from collections.abc import Callable
from dataclasses import dataclass
@@ -154,6 +157,21 @@ class JarvisTray:
def start(self) -> None:
if self._thread is not None and self._thread.is_alive():
return
+ if sys.platform == "darwin":
+ # pystray's darwin backend builds an NSStatusItem in Icon.__init__;
+ # AppKit allows UI objects on the MAIN thread only. Created from
+ # this worker thread, the first real-Mac boot died with a native
+ # AppKit assertion (NSInternalInconsistencyException → process
+ # abort, "Python quit unexpectedly") that no try/except can catch.
+ # Degrade to a logged no-op (AD-6): the desktop window + Dock icon
+ # remain. A real menu-bar icon needs main-thread hosting
+ # (pystray run_detached + the pywebview NSApplication) — tracked
+ # in docs/plans/cross-platform-mac-linux/FIX-TRACKER.md.
+ log.info(
+ "Tray not started: macOS allows status items on the main "
+ "thread only — running without a menu-bar icon."
+ )
+ return
if not display_present():
# Headless, or a Linux/Wayland session without an AppIndicator /
# notification-area host: pystray would spawn a daemon thread that
diff --git a/tests/unit/ui/test_tray.py b/tests/unit/ui/test_tray.py
index fab08d3eae..9dc6c2a459 100644
--- a/tests/unit/ui/test_tray.py
+++ b/tests/unit/ui/test_tray.py
@@ -26,8 +26,25 @@ def test_tray_start_is_noop_without_display(monkeypatch, caplog) -> None:
assert "tray not started" in caplog.text.lower()
+def test_tray_start_is_noop_on_macos(monkeypatch, caplog) -> None:
+ # AppKit allows UI objects (the NSStatusItem behind pystray's darwin
+ # backend) on the main thread ONLY; created from the tray worker thread
+ # the process dies with a native, uncatchable AppKit assertion — the
+ # first real-Mac boot aborted exactly there ("Python quit unexpectedly").
+ # start() must degrade to a logged no-op instead of spawning the thread.
+ monkeypatch.setattr("sys.platform", "darwin")
+ monkeypatch.setattr(tray_mod, "display_present", lambda: True, raising=False)
+ monkeypatch.setattr(JarvisTray, "_run", lambda self: None)
+ t = JarvisTray()
+ with caplog.at_level(logging.INFO):
+ t.start()
+ assert t._thread is None # gated: no tray thread spawned
+ assert "tray not started" in caplog.text.lower()
+
+
def test_tray_start_spawns_thread_with_display(monkeypatch) -> None:
- # AD-7: with a display present (Windows/macOS/Linux-X11) the tray still starts.
+ # AD-7: with a display present (Windows/Linux-X11) the tray still starts.
+ # (macOS is gated separately — see test_tray_start_is_noop_on_macos.)
monkeypatch.setattr(tray_mod, "display_present", lambda: True, raising=False)
ran: list[bool] = []
monkeypatch.setattr(JarvisTray, "_run", lambda self: ran.append(True))