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(terminal): reap the POSIX child so a clean exit stops reading as -1

rubenluetke10-beepe0e8e894f067
diff --git a/jarvis/terminal/backend.py b/jarvis/terminal/backend.py
index 6f93a395a3..bbde68c484 100644
--- a/jarvis/terminal/backend.py
+++ b/jarvis/terminal/backend.py
@@ -179,6 +179,23 @@ class _UnixPtyHandle:
@property
def exitstatus(self) -> int | None:
status = getattr(self._proc, "exitstatus", None)
+ if status is None:
+ # ptyprocess fills ``exitstatus`` only as a side effect of
+ # ``isalive()``/``wait()`` (both call waitpid) — it is never set
+ # eagerly the way pywinpty sets it. The reader loop leaves through
+ # EOFError on POSIX, so without this reap every pane whose agent
+ # ended BY ITSELF reported the unknown-exit ``-1``: a clean /exit
+ # showed as a crash and the resume self-healing restarted agents
+ # the user had deliberately closed (the exact bug the manager's
+ # ``normalize_exit_code`` docstring fixed on Windows). After EOF
+ # ptyprocess's waitpid is blocking, so this returns the real code
+ # rather than racing the child's exit.
+ try:
+ if self._proc.isalive(): # type: ignore[attr-defined]
+ return None
+ except Exception: # noqa: BLE001 - already-reaped/ECHILD reads as unknown
+ return None
+ status = getattr(self._proc, "exitstatus", None)
return None if status is None else int(status)
def write(self, data: str) -> None:
diff --git a/tests/unit/terminal/test_unix_pty.py b/tests/unit/terminal/test_unix_pty.py
index 959abe9aba..b6504b63f7 100644
--- a/tests/unit/terminal/test_unix_pty.py
+++ b/tests/unit/terminal/test_unix_pty.py
@@ -189,6 +189,63 @@ def test_unix_handle_setwinsize_terminate_exitstatus_pid():
assert h.exitstatus == 0
+def test_unix_handle_reaps_a_lazy_exitstatus_like_ptyprocess():
+ """ptyprocess sets ``exitstatus`` only inside isalive()/wait(), never eagerly.
+
+ The reader loop leaves through EOFError on POSIX and then reads
+ ``exitstatus`` — without a reap in the handle, a clean /exit reported the
+ unknown ``-1`` and the resume self-healing restarted deliberately closed
+ agents. This fake mirrors the real laziness: the code appears only after
+ ``isalive()`` has run its waitpid.
+ """
+ from jarvis.terminal.backend import _UnixPtyHandle
+
+ class _LazyExitProc:
+ pid = 7
+
+ def __init__(self) -> None:
+ self.exitstatus = None
+ self.isalive_calls = 0
+
+ def isalive(self) -> bool:
+ # waitpid side effect: the first call reaps and stores the code.
+ self.isalive_calls += 1
+ self.exitstatus = 0
+ return False
+
+ proc = _LazyExitProc()
+ h = _UnixPtyHandle(proc)
+ assert h.exitstatus == 0
+ assert proc.isalive_calls == 1
+
+
+def test_unix_handle_exitstatus_is_none_while_child_lives():
+ from jarvis.terminal.backend import _UnixPtyHandle
+
+ class _LiveProc:
+ pid = 8
+ exitstatus = None
+
+ def isalive(self) -> bool:
+ return True
+
+ assert _UnixPtyHandle(_LiveProc()).exitstatus is None
+
+
+def test_unix_handle_exitstatus_survives_an_already_reaped_child():
+ """ECHILD inside isalive() (ptyprocess raises) must read as unknown, not throw."""
+ from jarvis.terminal.backend import _UnixPtyHandle
+
+ class _ReapedProc:
+ pid = 9
+ exitstatus = None
+
+ def isalive(self) -> bool:
+ raise RuntimeError("cannot wait: ECHILD")
+
+ assert _UnixPtyHandle(_ReapedProc()).exitstatus is None
+
+
def test_winpty_handle_passes_str_through_unchanged():
"""winpty already deals in str — the handle must not double-decode."""
from jarvis.terminal.backend import _WinptyHandle