Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(opencode): stop tool commands in their own sessions with the server (#2312)

Call White537c506f390a
diff --git a/modules/agents/opencode/server.py b/modules/agents/opencode/server.py
index ab9b0d3ea..7b4ba1be9 100644
--- a/modules/agents/opencode/server.py
+++ b/modules/agents/opencode/server.py
@@ -24,12 +24,13 @@ from asyncio.subprocess import Process
from typing import Any, Awaitable, Callable, Dict, List, Optional
import aiohttp
+import psutil
from config import paths
from config.atomic_io import write_atomic
from config.v2_config import V2Config, is_model_hub_enabled
from core.handlers.model_hub.identifiers import OPENCODE_PROVIDER_BY_NATIVE_PROTOCOL
-from core.process_isolation import isolated_subprocess_kwargs, terminate_process_tree
+from core.process_isolation import isolated_subprocess_kwargs
from core.resource_governance import is_controller_resource_governor
from modules.agents.opencode.caller_context import ensure_plugin_installed, server_environment
from modules.agents.opencode.config_reconciler import OpenCodeConfigReconciler
@@ -265,13 +266,12 @@ class OpenCodeServerManager:
self._start_attempt_generation = 0
# The event loop ``_process`` was created on. Subprocess transports
# bind their internal Future / wait helpers to the creating loop;
- # ``process.wait()`` or ``terminate_process_tree(process)`` from a
- # different loop raises ``RuntimeError: got Future attached to a
- # different loop``. The singleton outlives ``asyncio.run`` calls
- # (Flask UI server creates a new loop per request), so any code
- # that touches ``_process`` from a non-creating loop has to
- # detach it first. ``_process_loop`` is set alongside every
- # ``_process`` assignment.
+ # ``process.wait()`` from a different loop raises ``RuntimeError: got
+ # Future attached to a different loop``. The singleton outlives
+ # ``asyncio.run`` calls (Flask UI server creates a new loop per
+ # request), so any code that touches ``_process`` from a
+ # non-creating loop has to detach it first. ``_process_loop`` is set
+ # alongside every ``_process`` assignment.
self._process_loop: Optional[asyncio.AbstractEventLoop] = None
self._base_url: Optional[str] = None
self._http_session: Optional[aiohttp.ClientSession] = None
@@ -1689,50 +1689,102 @@ class OpenCodeServerManager:
if not stopped and self._pid_exists(pid):
logger.debug("Failed to terminate OpenCode server pid=%s", pid)
+ async def _terminate_tracked_process(self, process: Process, reason: str) -> None:
+ """Stop a server this loop started, then let its exit be reaped."""
+ await self._terminate_pid(process.pid, reason=reason)
+ try:
+ await asyncio.wait_for(process.wait(), timeout=5)
+ except asyncio.TimeoutError:
+ logger.warning("OpenCode server pid=%s did not exit after it was stopped", process.pid)
+
@staticmethod
def _terminate_pid_tree_sync(pid: int, timeout: float = 5.0) -> bool:
- """Stop the dedicated OpenCode process group, including tool children."""
+ """Stop the OpenCode server and every process it started.
+
+ OpenCode starts each tool command detached, as the leader of a new
+ session and process group, so the server's own group never reaches it.
+ Only a live parent ties a process to the server, so the tree is walked
+ before anything is signalled and again before escalating. Every process
+ is held as a ``psutil.Process``, which refuses a pid reused since.
+ """
if os.name == "nt":
return runtime.stop_pid(pid, timeout=timeout)
try:
- pgid = os.getpgid(pid)
- except (ProcessLookupError, PermissionError):
+ server = psutil.Process(pid)
+ except psutil.NoSuchProcess:
+ return True
+ except psutil.Error:
return not runtime.pid_alive(pid)
- if pgid != pid:
- return runtime.stop_pid(pid, timeout=timeout)
-
- def group_alive() -> bool:
- try:
- os.killpg(pgid, 0)
- return True
- except ProcessLookupError:
- return False
- except PermissionError:
- return True
- try:
- os.killpg(pgid, signal.SIGTERM)
- except ProcessLookupError:
- return True
- except PermissionError:
- return False
- deadline = time.monotonic() + timeout
- while time.monotonic() < deadline:
- if not group_alive():
+ def running(processes: list[psutil.Process]) -> list[psutil.Process]:
+ alive = []
+ for process in processes:
+ try:
+ # A zombie has exited; reaping it belongs to its parent.
+ if process.is_running() and process.status() != psutil.STATUS_ZOMBIE:
+ alive.append(process)
+ except psutil.NoSuchProcess:
+ continue
+ except psutil.Error:
+ alive.append(process)
+ return alive
+
+ own_group = os.getpgrp()
+ founded_groups: set[int] = set()
+ tree = [server]
+ for sig in (signal.SIGTERM, signal.SIGKILL):
+ found = dict.fromkeys(tree)
+ for process in tree:
+ try:
+ found.update(dict.fromkeys(process.children(recursive=True)))
+ except psutil.Error:
+ continue
+ tree = running(list(found))
+ if not tree:
return True
- time.sleep(0.1)
- try:
- os.killpg(pgid, signal.SIGKILL)
- except ProcessLookupError:
- return True
- except PermissionError:
- return False
- deadline = time.monotonic() + timeout
- while time.monotonic() < deadline:
- if not group_alive():
+ groups = {}
+ for process in tree:
+ try:
+ groups[process] = os.getpgid(process.pid)
+ except OSError:
+ continue
+ # A group is the tree's when a process of the tree founded it. It is
+ # signalled only while a live process of the tree is still in it,
+ # proof that its id was not recycled, and the group signal also
+ # reaches whatever those processes started since the walk.
+ founded_groups.update(process.pid for process, pgid in groups.items() if process.pid == pgid)
+ owned_groups = (set(groups.values()) & founded_groups) - {own_group}
+ if sig == signal.SIGKILL:
+ logger.warning("Escalating to SIGKILL for %d process(es) of OpenCode server pid=%s", len(tree), pid)
+ elif len(tree) > 1:
+ logger.info("OpenCode server pid=%s has %d descendant process(es) to stop", pid, len(tree) - 1)
+ signalled_groups = set()
+ for pgid in owned_groups:
+ try:
+ os.killpg(pgid, sig)
+ except ProcessLookupError:
+ pass
+ except OSError:
+ # Fall back to signalling its known members one by one.
+ continue
+ signalled_groups.add(pgid)
+ for process in tree:
+ if groups.get(process) in signalled_groups:
+ continue
+ try:
+ process.send_signal(sig)
+ except psutil.NoSuchProcess:
+ continue
+ except psutil.Error:
+ logger.debug("Failed to signal OpenCode process pid=%s", process.pid, exc_info=True)
+ deadline = time.monotonic() + timeout
+ tree = running(tree)
+ while tree and time.monotonic() < deadline:
+ time.sleep(0.1)
+ tree = running(tree)
+ if not tree:
return True
- time.sleep(0.1)
- return not group_alive()
+ return False
async def _cleanup_orphaned_managed_server(self) -> None:
info = self._read_pid_file()
@@ -1903,13 +1955,12 @@ class OpenCodeServerManager:
# ``self._process`` may be a stale subprocess from a previous
# ``asyncio.run()`` call (the Flask UI server creates a new loop
# per request, while ``OpenCodeServerManager`` is a singleton).
- # ``terminate_process_tree`` calls ``process.wait()`` which uses
- # the transport's internal Future — that Future is bound to the
- # loop that created the subprocess, and awaiting it from any
- # other loop raises "got Future attached to a different loop".
- # The OS-level pid signaling below (``_terminate_pid`` via
- # ``runtime.stop_pid``) is loop-agnostic and is the correct
- # cleanup path; just detach the dangling Python object first.
+ # ``process.wait()`` uses the transport's internal Future — that
+ # Future is bound to the loop that created the subprocess, and
+ # awaiting it from any other loop raises "got Future attached to a
+ # different loop". The OS-level pid signaling below
+ # (``_terminate_pid``) is loop-agnostic and is the correct cleanup
+ # path; just detach the dangling Python object first.
current_loop = asyncio.get_running_loop()
self._start_attempt_generation += 1
self._last_start_failure_pid = None
@@ -1918,7 +1969,7 @@ class OpenCodeServerManager:
and self._process.returncode is None
and self._process_loop is current_loop
):
- await terminate_process_tree(self._process, logger, "OpenCode server", terminate_timeout=5)
+ await self._terminate_tracked_process(self._process, reason="stale server")
elif self._process and self._process_loop is not current_loop:
# Foreign-loop subprocess. We can't trust ``returncode`` here
# — the transport callbacks fire on the original (now-closed)
@@ -2024,12 +2075,7 @@ class OpenCodeServerManager:
if exit_code is None:
# A late-starting process must not become a healthy but unmanaged
# server after this call reports failure and clears its PID file.
- await terminate_process_tree(
- process,
- logger,
- "OpenCode server after startup timeout",
- terminate_timeout=5,
- )
+ await self._terminate_tracked_process(process, reason="startup timeout")
self._clear_pid_file()
self._process = None
self._process_loop = None
diff --git a/tests/test_opencode_server.py b/tests/test_opencode_server.py
index 9ec95e2f4..78597755c 100644
--- a/tests/test_opencode_server.py
+++ b/tests/test_opencode_server.py
@@ -4,13 +4,20 @@ import importlib.util
import io
import json
import os
+import signal
+import subprocess
import sys
import tempfile
+import threading
import types
import unittest
+from contextlib import suppress
from pathlib import Path
from unittest.mock import AsyncMock, Mock, patch
+import psutil
+import pytest
+
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from storage import message_deliveries as delivery_store
@@ -194,7 +201,7 @@ class OpenCodeServerTests(unittest.IsolatedAsyncioTestCase):
manager._write_pid_file = Mock() # type: ignore[method-assign]
manager._clear_pid_file = Mock() # type: ignore[method-assign]
manager._apply_resource_governance = Mock() # type: ignore[method-assign]
- terminate = AsyncMock()
+ manager._terminate_tracked_process = AsyncMock() # type: ignore[method-assign]
create_process = AsyncMock(return_value=process)
user_config = '{"permission":"ask"}'
@@ -207,18 +214,12 @@ class OpenCodeServerTests(unittest.IsolatedAsyncioTestCase):
patch.object(SERVER_MODULE.asyncio, "sleep", AsyncMock()),
patch.object(SERVER_MODULE.time, "monotonic", side_effect=[0.0, 0.0, 121.0]),
patch.object(SERVER_MODULE, "server_environment", return_value={}),
- patch.object(SERVER_MODULE, "terminate_process_tree", terminate),
patch.dict(os.environ, {"OPENCODE_CONFIG_CONTENT": user_config, "AVIBE_OPENCODE_MODEL_HUB": "1"}),
):
with self.assertRaisesRegex(RuntimeError, "failed to start within 120s"):
await manager._start_server()
- terminate.assert_awaited_once_with(
- process,
- SERVER_MODULE.logger,
- "OpenCode server after startup timeout",
- terminate_timeout=5,
- )
+ manager._terminate_tracked_process.assert_awaited_once_with(process, reason="startup timeout")
self.assertEqual(manager._clear_pid_file.call_count, 2)
self.assertEqual(create_process.await_args.kwargs["env"]["AVIBE_OPENCODE_MODEL_HUB"], "0")
self.assertIsNone(manager._process)
@@ -3225,6 +3226,73 @@ async def _async_none():
return None
+# Starts its tool the way OpenCode's shell tool does (``detached``, so setsid):
+# the tool leads a new session and process group, out of the server's reach.
+_SERVER_WITH_DETACHED_TOOL = """
+import subprocess, sys
+tool = subprocess.Popen(["/bin/sh", "-c", sys.argv[1], "tool", sys.argv[2]], start_new_session=True)
+print(tool.pid, flush=True)
+tool.wait()
+"""
+# Records SIGTERM and keeps going, starting a new child each second, so only
+# SIGKILL ends it and some of its children postdate any single walk.
+_TERM_SURVIVING_TOOL = 'trap \'echo TERM > "$1"\' TERM; while :; do sleep 1; done'
+
+
+def _exited(process: psutil.Process) -> bool:
+ try:
+ return not process.is_running() or process.status() == psutil.STATUS_ZOMBIE
+ except psutil.NoSuchProcess:
+ return True
+
+
+def _running_group_members(pgid: int) -> list[int]:
+ members = []
+ for process in psutil.process_iter():
+ with suppress(OSError, psutil.Error):
+ if os.getpgid(process.pid) == pgid and not _exited(process):
+ members.append(process.pid)
+ return members
+
+
[email protected](os.name == "nt", reason="POSIX sessions and process groups")
+def test_stopping_the_server_stops_tool_commands_in_their_own_session(tmp_path):
+ term_marker = tmp_path / "tool-received-sigterm"
+ # Avibe starts the server as the leader of its own session, as here.
+ server = subprocess.Popen(
+ [sys.executable, "-c", _SERVER_WITH_DETACHED_TOOL, _TERM_SURVIVING_TOOL, str(term_marker)],
+ stdout=subprocess.PIPE,
+ text=True,
+ start_new_session=True,
+ )
+ bystander = subprocess.Popen(["/bin/sh", "-c", "while :; do sleep 1; done"], start_new_session=True)
+ tool_pid = None
+ try:
+ tool_pid = int(server.stdout.readline())
+ tool = psutil.Process(tool_pid)
+ assert os.getsid(tool_pid) == tool_pid != os.getsid(server.pid)
+ # Reaps the server the moment it exits, as Avibe's asyncio child watcher does.
+ threading.Thread(target=server.wait, daemon=True).start()
+
+ assert OpenCodeServerManager._terminate_pid_tree_sync(server.pid, timeout=2.0)
+
+ assert _exited(tool)
+ assert _running_group_members(tool_pid) == []
+ # SIGTERM came first, and only then SIGKILL.
+ assert term_marker.read_text().strip() == "TERM"
+ assert server.wait(timeout=5) == -signal.SIGTERM
+ assert bystander.poll() is None
+ finally:
+ if tool_pid is not None:
+ with suppress(ProcessLookupError):
+ os.killpg(tool_pid, signal.SIGKILL)
+ for process in (server, bystander):
+ if process.poll() is None:
+ process.kill()
+ process.wait(timeout=5)
+ server.stdout.close()
+
+
def test_mh_runtime_002_matching_overlay_does_not_wait_for_an_unrelated_active_run():
"""MH-RUNTIME-002: unchanged direct mode cannot head-of-line block another Session."""