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(process): survive psutil's pidfd EINVAL race when waiting on a process tree

rubenluetke10-beepc3fe234334a5
diff --git a/jarvis/core/process_utils.py b/jarvis/core/process_utils.py
index c00012d98d..523fa6ffc7 100644
--- a/jarvis/core/process_utils.py
+++ b/jarvis/core/process_utils.py
@@ -32,12 +32,18 @@ parameter is silently ignored by the subprocess machinery.
from __future__ import annotations
+import errno
+import logging
import os
import shutil
import subprocess
import sys
+import time
+from collections.abc import Sequence
from pathlib import Path
-from typing import TextIO
+from typing import Any, TextIO
+
+log = logging.getLogger(__name__)
_NULL_STANDARD_STREAMS: list[TextIO] = []
@@ -266,6 +272,42 @@ def ensure_normal_process_priority() -> str | None:
return None
+def wait_procs(procs: Sequence[Any], timeout: float) -> tuple[list[Any], list[Any]]:
+ """``psutil.wait_procs`` that survives psutil's pidfd race on Linux.
+
+ psutil 7.2 waits on Linux with ``pidfd_open()`` and lets ``EINVAL``
+ through. The kernel answers ``EINVAL`` for a process whose main thread has
+ already exited while other threads still run — a normal moment while a
+ browser or model server shuts down. The whole wait then raised
+ ``OSError: [Errno 22]`` instead of reporting which processes were gone
+ (seen in the Linux installer smoke, 2026-09-30). On that error this polls
+ the same processes until they are gone or ``timeout`` runs out, and
+ returns ``(gone, alive)`` exactly like psutil does.
+ """
+ import psutil # noqa: PLC0415 — keep psutil off this module's import floor
+
+ try:
+ return psutil.wait_procs(procs, timeout=timeout)
+ except OSError as exc:
+ if exc.errno != errno.EINVAL:
+ raise
+ log.debug("psutil.wait_procs hit the pidfd EINVAL race; polling instead")
+
+ def running(proc: Any) -> bool:
+ try:
+ return bool(proc.is_running()) and proc.status() != psutil.STATUS_ZOMBIE
+ except psutil.Error: # gone or no longer inspectable: it is not running
+ return False
+
+ deadline = time.monotonic() + max(0.0, timeout)
+ alive = [proc for proc in procs if running(proc)]
+ while alive and time.monotonic() < deadline:
+ time.sleep(0.05)
+ alive = [proc for proc in alive if running(proc)]
+ gone = [proc for proc in procs if proc not in alive]
+ return gone, alive
+
+
__all__ = [
"NO_WINDOW_CREATIONFLAGS",
"disable_windows_app_ghosting",
@@ -273,5 +315,6 @@ __all__ = [
"ensure_standard_streams",
"resolve_executable",
"thread_message_loop_wake_supported",
+ "wait_procs",
"wake_thread_message_loop",
]
diff --git a/jarvis/missions/workers/api_agent_tools.py b/jarvis/missions/workers/api_agent_tools.py
index 287b71bf07..54b87883ee 100644
--- a/jarvis/missions/workers/api_agent_tools.py
+++ b/jarvis/missions/workers/api_agent_tools.py
@@ -756,7 +756,9 @@ async def _terminate_windows_tree(proc: asyncio.subprocess.Process) -> None:
for process in reversed(processes):
with contextlib_suppress(psutil.Error):
process.terminate()
- _, alive = await asyncio.to_thread(psutil.wait_procs, processes, timeout=0.25)
+ from jarvis.core.process_utils import wait_procs # noqa: PLC0415
+
+ _, alive = await asyncio.to_thread(wait_procs, processes, 0.25)
for process in alive:
with contextlib_suppress(psutil.Error):
process.kill()
diff --git a/jarvis/realtime/local_server/supervisor.py b/jarvis/realtime/local_server/supervisor.py
index 2dc3bfa26b..936b3d13ce 100644
--- a/jarvis/realtime/local_server/supervisor.py
+++ b/jarvis/realtime/local_server/supervisor.py
@@ -2320,7 +2320,9 @@ def _kill_by_install_root(root: Path) -> tuple[int, int]:
)
if not signalled:
return vanished, failed
- gone, alive = psutil.wait_procs(signalled, timeout=10)
+ from jarvis.core.process_utils import wait_procs # noqa: PLC0415
+
+ gone, alive = wait_procs(signalled, timeout=10)
for proc in alive:
log.warning("supervisor: managed pid %s survived forced stop", proc.pid)
return vanished + len(gone), failed + len(alive)
diff --git a/jarvis/setup/uninstall.py b/jarvis/setup/uninstall.py
index af16869607..ed86b8e6dd 100644
--- a/jarvis/setup/uninstall.py
+++ b/jarvis/setup/uninstall.py
@@ -49,6 +49,7 @@ from rich.theme import Theme
from jarvis.core import config as cfg
from jarvis.core.branding import CONFIG_FILE_NAME
+from jarvis.core.process_utils import wait_procs
from jarvis.setup.wizard import SECRETS
# Same Charcoal + Gold palette as install/installer.py and the first-run wizard,
@@ -282,14 +283,14 @@ def _stop_running_instances(install_dir: Path) -> int:
proc.terminate()
except (psutil.NoSuchProcess, psutil.AccessDenied):
continue
- _gone, alive = psutil.wait_procs(procs, timeout=6)
+ _gone, alive = wait_procs(procs, timeout=6)
for proc in alive:
try:
proc.kill()
except (psutil.NoSuchProcess, psutil.AccessDenied):
continue
if alive:
- _gone, alive = psutil.wait_procs(alive, timeout=4)
+ _gone, alive = wait_procs(alive, timeout=4)
stopped = len(procs) - len(alive)
if stopped:
diff --git a/scripts/ci/check_frozen_browser.py b/scripts/ci/check_frozen_browser.py
index a10a7ee9a0..3dd59a9ceb 100644
--- a/scripts/ci/check_frozen_browser.py
+++ b/scripts/ci/check_frozen_browser.py
@@ -30,7 +30,7 @@ sys.path.insert(0, str(ROOT))
from jarvis.core.config_writer import _WRITE_LOCK, _atomic_write # noqa: E402
from jarvis.core.instance import DEV_PORT_OFFSET # noqa: E402
-from jarvis.core.process_utils import NO_WINDOW_CREATIONFLAGS # noqa: E402
+from jarvis.core.process_utils import NO_WINDOW_CREATIONFLAGS, wait_procs # noqa: E402
def isolated_env(profile: Path, port: int, key: str) -> dict[str, str]:
@@ -88,13 +88,13 @@ def stop_owned_tree(process: subprocess.Popen) -> None:
child.terminate()
except psutil.NoSuchProcess:
pass # Browser containment can close descendants concurrently.
- _, alive = psutil.wait_procs(owned, timeout=8)
+ _, alive = wait_procs(owned, timeout=8)
for child in alive:
try:
child.kill()
except psutil.NoSuchProcess:
pass # A pending graceful shutdown may win the race.
- _, alive = psutil.wait_procs(alive, timeout=5)
+ _, alive = wait_procs(alive, timeout=5)
if alive:
raise RuntimeError("Frozen app left owned processes running")
process.wait(timeout=5)
diff --git a/tests/unit/core/test_process_utils_wait_procs.py b/tests/unit/core/test_process_utils_wait_procs.py
new file mode 100644
index 0000000000..c9db557678
--- /dev/null
+++ b/tests/unit/core/test_process_utils_wait_procs.py
@@ -0,0 +1,72 @@
+"""``wait_procs`` survives psutil's pidfd EINVAL race (Linux installer smoke 2026-09-30)."""
+
+from __future__ import annotations
+
+import errno
+
+import psutil
+import pytest
+
+from jarvis.core import process_utils
+
+
+class FakeProc:
+ """Stands in for ``psutil.Process``: gone after ``live_polls`` checks."""
+
+ def __init__(self, live_polls: int, *, zombie: bool = False) -> None:
+ self.live_polls = live_polls
+ self.zombie = zombie
+
+ def is_running(self) -> bool:
+ if self.live_polls <= 0:
+ return False
+ self.live_polls -= 1
+ return True
+
+ def status(self) -> str:
+ return psutil.STATUS_ZOMBIE if self.zombie else psutil.STATUS_RUNNING
+
+
+class VanishedProc:
+ def is_running(self) -> bool:
+ raise psutil.NoSuchProcess(4242)
+
+ def status(self) -> str: # pragma: no cover - never reached
+ return psutil.STATUS_RUNNING
+
+
+def _einval(*_args: object, **_kwargs: object) -> object:
+ raise OSError(errno.EINVAL, "Invalid argument")
+
+
+def test_the_einval_race_falls_back_to_polling(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr(psutil, "wait_procs", _einval)
+ quick, zombie, vanished = FakeProc(2), FakeProc(5, zombie=True), VanishedProc()
+ stuck = FakeProc(10_000)
+
+ gone, alive = process_utils.wait_procs([quick, zombie, vanished, stuck], timeout=0.3)
+
+ assert alive == [stuck]
+ assert gone == [quick, zombie, vanished]
+
+
+def test_other_os_errors_still_raise(monkeypatch: pytest.MonkeyPatch) -> None:
+ def _eperm(*_args: object, **_kwargs: object) -> object:
+ raise OSError(errno.EPERM, "Operation not permitted")
+
+ monkeypatch.setattr(psutil, "wait_procs", _eperm)
+ with pytest.raises(OSError):
+ process_utils.wait_procs([FakeProc(1)], timeout=0.1)
+
+
+def test_the_normal_path_is_psutil_itself(monkeypatch: pytest.MonkeyPatch) -> None:
+ seen: list[float] = []
+
+ def _psutil(procs: list[object], timeout: float) -> tuple[list[object], list[object]]:
+ seen.append(timeout)
+ return list(procs), []
+
+ monkeypatch.setattr(psutil, "wait_procs", _psutil)
+ proc = FakeProc(1)
+ assert process_utils.wait_procs([proc], timeout=3) == ([proc], [])
+ assert seen == [3]