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(desktop): guarantee restart watchdog exit

Ruben Lütkeb56e3c11846d
diff --git a/docs/BUGS.md b/docs/BUGS.md
index caf05bcbd4..41fdd2af21 100644
--- a/docs/BUGS.md
+++ b/docs/BUGS.md
@@ -3391,3 +3391,30 @@ paint-before-heavy-start contract.
not only a post-success cooldown. The suppressed interval must stop both the
expensive verifier and the candidate/recognizer producer; rate-limiting only the
last stage leaves the same resource storm alive one layer earlier.
+
+## BUG-046: Desktop restart closes Jarvis but never brings it back (HIGH, 2026-07-13)
+
+**Symptom.** `POST /api/settings/restart-app` returned success and closed the
+desktop, but the detached relauncher could not reliably acquire the
+single-instance lock because the old process remained alive without a window.
+The restart appeared to be complete while Jarvis was no longer reachable.
+
+**Root cause.** `run_restart_quit_sequence` described its hard exit as a
+watchdog, but armed it only after the synchronous `window.destroy()` call
+returned. A cross-thread WebView destroy is one of the operations that can
+block during teardown. In that exact failure mode, execution never reached the
+hard exit, the old process retained its mutex and port, and fresh launchers
+bounced off the supposedly active instance.
+
+**Fix.** The hard exit is now armed in an independent daemon thread before the
+GUI destroy call. Normal teardown still wins when it completes quickly; a
+blocked Cocoa, GTK, or WebView2 destroy can no longer prevent the old process
+from releasing the cross-platform single-instance and port resources.
+
+**Guard.** `tests/unit/ui/test_relauncher.py` blocks `destroy_window` on an
+event and proves the independent watchdog still reaches the injected process
+exit before the destroy call returns.
+
+**Class rule.** A shutdown watchdog must run independently of every operation
+it is intended to bound. Code placed after a possibly blocking call is a
+fallback, not a watchdog.
diff --git a/jarvis/ui/desktop_app.py b/jarvis/ui/desktop_app.py
index 2ac981ac9d..22d9fc2c4b 100644
--- a/jarvis/ui/desktop_app.py
+++ b/jarvis/ui/desktop_app.py
@@ -2128,8 +2128,8 @@ class DesktopApp:
target=_quit_soon, name="jarvis-restart-quit", daemon=True
).start()
logger.info(
- "Self-restart scheduled (relauncher spawned; quitting in ~0.3 s, "
- "hard-exit fallback at ~2 s)."
+ "Self-restart scheduled (relauncher spawned; quitting in ~0.2 s, "
+ "independent hard-exit watchdog at ~0.9 s)."
)
return True
diff --git a/jarvis/ui/relauncher.py b/jarvis/ui/relauncher.py
index f07f6ad89e..95b1419140 100644
--- a/jarvis/ui/relauncher.py
+++ b/jarvis/ui/relauncher.py
@@ -22,6 +22,7 @@ from __future__ import annotations
import os
import subprocess
import sys
+import threading
import time
LAUNCHER_MODULE = "jarvis.ui.web.launcher"
@@ -95,6 +96,20 @@ def wait_for_pid_exit(
return not _alive(pid)
+def _arm_hard_exit_watchdog(delay: float, exit_fn) -> None:
+ """Force-exit independently even when the GUI destroy call never returns."""
+
+ def _force_exit() -> None:
+ time.sleep(delay)
+ exit_fn(0)
+
+ threading.Thread(
+ target=_force_exit,
+ name="jarvis-restart-hard-exit",
+ daemon=True,
+ ).start()
+
+
def run_restart_quit_sequence(
*,
set_quit,
@@ -103,6 +118,7 @@ def run_restart_quit_sequence(
hard_exit_after: float = 0.7,
_sleep=time.sleep,
_exit=os._exit,
+ _arm_watchdog=_arm_hard_exit_watchdog,
) -> None:
"""Quit the DYING app for a restart, hard-exiting if shutdown stalls.
@@ -113,16 +129,14 @@ def run_restart_quit_sequence(
The hard exit is the load-bearing part: the relauncher's fresh instance can
only claim the single-instance mutex + TCP port once THIS process is gone.
- A stalled shutdown — or a cross-thread ``window.destroy`` that fails to
- unblock the GUI loop (the BUG-031 hazard) — would otherwise leave a
- windowless process holding the lock, and the new instance would bounce off
- it: the "shuts down but never comes back" bug. If the normal shutdown
- finishes first, the main thread exits the process and this daemon thread
- dies before reaching ``_exit`` — so the force-exit only fires when it must.
+ It is armed in an independent daemon thread *before* ``window.destroy``.
+ A cross-thread destroy can itself block forever (the BUG-031 hazard), so a
+ watchdog placed after that call is not a watchdog at all. If normal shutdown
+ finishes first, the process ends and takes the daemon thread with it.
Speed note (2026-06-21): for a RESTART the dying app does not need a full,
leisurely clean shutdown — the fresh instance re-initialises every subsystem
- anyway. So the hard-exit cap is tight (2 s, was 10 s): a slow or hanging
+ anyway. So the hard-exit cap is tight (0.7 s, was 10 s): a slow or hanging
teardown (MCP session close, the BUG-031 window-destroy hang) is force-exited
fast, freeing the lock + port for the fresh, fast-booting instance. The only
cost is some teardown skipped on restart (e.g. an MCP subprocess re-spawned
@@ -131,14 +145,13 @@ def run_restart_quit_sequence(
_sleep(pre_delay)
try:
set_quit()
- except Exception: # noqa: BLE001 — never block the quit on a callback error
+ except Exception: # noqa: BLE001, S110 — never block quit on callback error
pass
+ _arm_watchdog(hard_exit_after, _exit)
try:
destroy_window()
- except Exception: # noqa: BLE001 — destroy may already be impossible; force-exit anyway
+ except Exception: # noqa: BLE001, S110 — destroy may be impossible; watchdog exits
pass
- _sleep(hard_exit_after)
- _exit(0)
def _new_instance_settled(
diff --git a/tests/unit/ui/test_relauncher.py b/tests/unit/ui/test_relauncher.py
index 1256199b51..2b59a5b286 100644
--- a/tests/unit/ui/test_relauncher.py
+++ b/tests/unit/ui/test_relauncher.py
@@ -6,6 +6,7 @@ so they are tested without spawning real processes or actually exiting.
"""
from __future__ import annotations
+import threading
from types import SimpleNamespace
from jarvis.ui import relauncher
@@ -141,14 +142,20 @@ def test_new_instance_settled_false_when_it_dies():
def test_restart_quit_sequence_marks_quit_destroys_then_hard_exits():
order: list[object] = []
+
+ def arm_watchdog(delay, exit_fn):
+ order.append(("armed", delay))
+ exit_fn(0)
+
relauncher.run_restart_quit_sequence(
set_quit=lambda: order.append("quit"),
destroy_window=lambda: order.append("destroy"),
_sleep=lambda _s: order.append(("sleep", _s)),
_exit=lambda code: order.append(("exit", code)),
+ _arm_watchdog=arm_watchdog,
)
assert ("exit", 0) in order
- assert order.index("quit") < order.index("destroy") < order.index(("exit", 0))
+ assert order.index("quit") < order.index(("armed", 0.7)) < order.index("destroy")
def test_restart_quit_sequence_hard_exits_even_if_destroy_raises():
@@ -162,5 +169,36 @@ def test_restart_quit_sequence_hard_exits_even_if_destroy_raises():
destroy_window=boom,
_sleep=lambda _s: None,
_exit=lambda code: exited.append(code),
+ _arm_watchdog=lambda _delay, exit_fn: exit_fn(0),
)
assert exited == [0]
+
+
+def test_restart_watchdog_exits_while_destroy_window_is_blocked():
+ destroy_entered = threading.Event()
+ release_destroy = threading.Event()
+ exited = threading.Event()
+
+ def blocked_destroy():
+ destroy_entered.set()
+ release_destroy.wait(timeout=1.0)
+
+ worker = threading.Thread(
+ target=relauncher.run_restart_quit_sequence,
+ kwargs={
+ "set_quit": lambda: None,
+ "destroy_window": blocked_destroy,
+ "pre_delay": 0.0,
+ "hard_exit_after": 0.02,
+ "_exit": lambda _code: exited.set(),
+ },
+ daemon=True,
+ )
+ worker.start()
+
+ assert destroy_entered.wait(timeout=0.5)
+ assert exited.wait(timeout=0.5)
+
+ release_destroy.set()
+ worker.join(timeout=0.5)
+ assert not worker.is_alive()