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(tests): refuse signals to processes a test did not start (#2258)

Call Whitefb18756aaa28
diff --git a/pyproject.toml b/pyproject.toml
index 573887a5b..bd216fd97 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -163,6 +163,7 @@ markers = [
"integration: tests excluded from the unit shards because they need external infrastructure (Docker + platform tokens) or minutes of real filesystem work",
"uses_real_paths: opt out of the autouse home/config isolation so the test can exercise real path resolvers (must remain read-only)",
"no_sqlite_template: opt out of the pre-migrated SQLite template for migration/bootstrap tests",
+ "allow_foreign_signals(reason): opt out of the autouse guard that refuses os.kill/os.killpg to processes the test did not start (state the reason)",
]
[dependency-groups]
diff --git a/tests/conftest.py b/tests/conftest.py
index 87bd32e18..84b6a20e2 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -44,10 +44,14 @@ write to ``~/.avibe/`` or legacy ``~/.vibe_remote/``).
from __future__ import annotations
import ast
+import errno
import inspect
import os
+import re
import shutil
+import signal
import sqlite3
+import subprocess
import sys
import unittest
import warnings
@@ -55,6 +59,7 @@ from contextlib import closing, contextmanager
from functools import wraps
from pathlib import Path
+import psutil
import pytest
from sqlalchemy.exc import SAWarning
@@ -344,6 +349,238 @@ def _reset_cached_sqlite_engines():
_reset()
+# Captured when pytest loads this file, before any test can replace them: tests
+# stub `os.getpid` and `runtime.psutil.Process` -- the psutil module global that
+# psutil's own `parents()` resolves through -- and the guard must not ask a fake.
+_REAL_OS_KILL = getattr(os, "kill", None)
+_REAL_OS_KILLPG = getattr(os, "killpg", None)
+_REAL_OS_GETPID = os.getpid
+_REAL_OS_GETPGID = getattr(os, "getpgid", None)
+_REAL_OS_GETSID = getattr(os, "getsid", None)
+_REAL_OS_GETPGRP = getattr(os, "getpgrp", None)
+_REAL_PSUTIL_PROCESS = psutil.Process
+_REAL_PSUTIL_PIDS = psutil.pids
+_REAL_POPEN_INIT = subprocess.Popen.__init__
+
+
+def _describe_pid(pid: int) -> str:
+ try:
+ return f"{pid} ({_REAL_PSUTIL_PROCESS(pid).name()})"
+ except psutil.Error:
+ return f"{pid} (no such process)"
+
+
+class _ForeignSignalGuard:
+ """One test's record of the processes it may signal.
+
+ The contract: refuse any signal that could reach a process outside this
+ pytest run. That is this pytest process, its process group, ``-1``, and any
+ process that neither descends from this pytest process, nor sits in a
+ process group or session founded by a process the test owns, nor names this
+ test's own ``tmp_path`` in its command line -- so pytest's ancestors, the CI
+ runner and a developer's live Avibe service are all refused. It does not
+ isolate tests in the same run from each other: a process started by a
+ module- or session-scoped fixture, or left running by an earlier test,
+ descends from this pytest process and stays signalable.
+
+ The test owns its ``subprocess.Popen`` children and any process it has
+ signalled as its own; the founder rule keeps an orphan in a spawned child's
+ group signalable after reparenting hides it from ancestry. The ``tmp_path``
+ rule covers a detached grandchild, which keeps nothing else that ties it to
+ the test and is what harnesses such as the Model Hub e2e driver clean up by.
+ It is the only grant over processes outside the tree, so it names this
+ test's directory, never the shared temp root. A pid that names no process
+ gets the ESRCH the real call would raise, delivered to nothing, because a
+ descendant the test collected may exit before it is signalled. Signal 0 is
+ a liveness probe and passes.
+ """
+
+ def __init__(self, test_temp: Path) -> None:
+ self.me = _REAL_OS_GETPID()
+ # The whole final component: `test_x1` must not claim `test_x10`.
+ self.names_test_temp = re.compile(re.escape(str(test_temp)) + r"(?![\w.-])").search
+ self.owned: set[int] = set()
+ self.violations: list[str] = []
+
+ def _owns(self, pid: int) -> bool:
+ if pid == self.me:
+ return False
+ if pid in self.owned:
+ return True
+ ancestor, seen = pid, set()
+ while ancestor > 0 and ancestor not in seen:
+ seen.add(ancestor)
+ try:
+ ancestor = _REAL_PSUTIL_PROCESS(ancestor).ppid()
+ except psutil.Error:
+ break
+ if ancestor == self.me:
+ self.owned.add(pid)
+ return True
+ for founder_of in (_REAL_OS_GETPGID, _REAL_OS_GETSID):
+ try:
+ founder = founder_of(pid)
+ except OSError:
+ continue
+ if founder in self.owned:
+ self.owned.add(pid)
+ return True
+ try:
+ cmdline = _REAL_PSUTIL_PROCESS(pid).cmdline()
+ except psutil.Error:
+ cmdline = []
+ if any(self.names_test_temp(part) for part in cmdline):
+ self.owned.add(pid)
+ return True
+ return False
+
+ @staticmethod
+ def _in_group(pid: int, pgid: int) -> bool:
+ try:
+ return _REAL_OS_GETPGID(pid) == pgid
+ except OSError:
+ return False
+
+ def _group_reason(self, pgid: int) -> str | None:
+ if pgid == _REAL_OS_GETPGRP():
+ return f"process group {pgid} is this pytest process's own"
+ members = [pid for pid in _REAL_PSUTIL_PIDS() if self._in_group(pid, pgid)]
+ # A member can exit between the listing and its ownership check, as the
+ # short-lived commands a shell group runs do; only one still there is foreign.
+ foreign = [pid for pid in members if not self._owns(pid) and self._in_group(pid, pgid)]
+ if foreign:
+ return f"process group {pgid} holds processes this test did not start: " + ", ".join(
+ _describe_pid(pid) for pid in foreign
+ )
+ if not members and pgid not in self.owned:
+ raise ProcessLookupError(errno.ESRCH, os.strerror(errno.ESRCH))
+ self.owned.add(pgid)
+ return None
+
+ def _reason(self, pid: int) -> str | None:
+ """Why ``kill(pid, ...)`` could reach a process this test did not start.
+
+ Raises the ``ProcessLookupError`` the real call would when nothing
+ answers to ``pid``.
+ """
+
+ if pid == -1:
+ return "pid -1 addresses every process this user may signal"
+ if pid <= 0:
+ return self._group_reason(-pid if pid else _REAL_OS_GETPGRP())
+ if pid == self.me:
+ return f"pid {pid} is this pytest process itself"
+ if self._owns(pid):
+ return None
+ # Probed before refusing, so a vanished target is not mistaken for a
+ # foreign one; raised here rather than by the real call, which could
+ # reach a process that took the pid in between.
+ try:
+ _REAL_OS_KILL(pid, 0)
+ except ProcessLookupError:
+ raise ProcessLookupError(errno.ESRCH, os.strerror(errno.ESRCH)) from None
+ except PermissionError:
+ pass
+ return f"pid {_describe_pid(pid)} is not a process this test started"
+
+ def refuse_foreign(self, primitive: str, target, sig, kill_target) -> None:
+ __tracebackhide__ = True
+ if sig == 0 or not isinstance(target, int):
+ return
+ reason = self._reason(kill_target)
+ if reason is None:
+ return
+ try:
+ name = signal.Signals(sig).name
+ except ValueError:
+ name = str(sig)
+ message = (
+ f"os.{primitive}({target}, {name}) was not delivered: {reason}. A test may "
+ "signal only processes it started; stub the stop path that reached this "
+ "instead of letting it act on a fake pid."
+ )
+ self.violations.append(message)
+ # A BaseException, so a production `except Exception` around the signal
+ # cannot swallow it; the teardown check covers anything broader.
+ pytest.fail(message)
+
+
+# The running test's guard, or None between tests and in opted-out tests.
+_active_signal_guard: _ForeignSignalGuard | None = None
+
+
+def _guarded_kill(pid, sig):
+ __tracebackhide__ = True
+ guard = _active_signal_guard
+ if guard is not None:
+ guard.refuse_foreign("kill", pid, sig, pid)
+ return _REAL_OS_KILL(pid, sig)
+
+
+def _guarded_killpg(pgid, sig):
+ __tracebackhide__ = True
+ guard = _active_signal_guard
+ if guard is not None:
+ # libc's killpg(pgid) is kill(-pgid): on macOS a negative pgid names one pid.
+ guard.refuse_foreign("killpg", pgid, sig, -pgid if isinstance(pgid, int) else pgid)
+ return _REAL_OS_KILLPG(pgid, sig)
+
+
+@wraps(_REAL_POPEN_INIT)
+def _recording_popen_init(popen, *args, **kwargs):
+ _REAL_POPEN_INIT(popen, *args, **kwargs)
+ guard = _active_signal_guard
+ if guard is not None:
+ guard.owned.add(popen.pid)
+
+
+# Installed once for the whole run rather than per test through `monkeypatch`:
+# a test's own `monkeypatch.undo()` would otherwise remove the guard mid-test.
+# psutil's `send_signal`/`terminate`/`kill` and `subprocess.Popen`'s all end in
+# `os.kill` on POSIX, so replacing the `os` attributes covers them. POSIX only:
+# on Windows `os.kill` is TerminateProcess (and signal 0 is CTRL_C_EVENT, not a
+# probe), `os.killpg` does not exist, and the product's Windows stop path calls
+# TerminateProcess through ctypes, so wrapping `os.kill` there would guard
+# nothing the product reaches.
+_SIGNAL_GUARD_SUPPORTED = os.name != "nt" and _REAL_OS_KILLPG is not None
+if _SIGNAL_GUARD_SUPPORTED:
+ os.kill = _guarded_kill
+ os.killpg = _guarded_killpg
+ subprocess.Popen.__init__ = _recording_popen_init
+
+
[email protected](autouse=True)
+def _foreign_signal_guard(request, tmp_path):
+ """Fail any test that signals a process it did not start, and deliver nothing.
+
+ Tests routinely make ``pid_alive`` true for fake pids such as 1234 or 5678.
+ One stop path left unstubbed then signals whatever real process holds that
+ pid -- on a CI runner that was the pytest process itself, on a developer
+ machine it can be the live Avibe service. Processes a test starts stay
+ signalable; ``allow_foreign_signals(reason=...)`` opts a test out.
+ """
+
+ global _active_signal_guard
+ marker = request.node.get_closest_marker("allow_foreign_signals")
+ if marker is not None:
+ if not (marker.kwargs.get("reason") or marker.args):
+ pytest.fail("@pytest.mark.allow_foreign_signals needs a reason")
+ yield None
+ return
+ if not _SIGNAL_GUARD_SUPPORTED:
+ yield None
+ return
+ guard = _ForeignSignalGuard(tmp_path)
+ _active_signal_guard = guard
+ try:
+ yield guard
+ finally:
+ _active_signal_guard = None
+ __tracebackhide__ = True
+ if guard.violations:
+ pytest.fail("signals to processes this test did not start were blocked:\n" + "\n".join(guard.violations))
+
+
@pytest.fixture
def hold_migration_lock_elsewhere():
"""Hold a migration lock path from a thread that is genuinely not the caller.
diff --git a/tests/test_conftest_signal_guard.py b/tests/test_conftest_signal_guard.py
new file mode 100644
index 000000000..b809dbdbd
--- /dev/null
+++ b/tests/test_conftest_signal_guard.py
@@ -0,0 +1,248 @@
+"""Regression guard: the autouse signal guard must refuse processes a test did
+not start, deliver nothing to them, and leave the test's own children signalable.
+
+Without it, a fixture that made ``pid_alive`` true for fake pids let a failed
+start roll back through the real ``stop_ui()``, which sent SIGTERM to pid 5678:
+on a CI runner that pid was the pytest process itself, and the shard died with
+exit code 143.
+"""
+
+from __future__ import annotations
+
+import os
+import signal
+import subprocess
+import sys
+import time
+from contextlib import suppress
+
+import psutil
+import pytest
+
+from tests.conftest import _REAL_OS_KILL
+
+pytestmark = pytest.mark.skipif(os.name == "nt", reason="the guard is POSIX-only; see tests/conftest.py")
+
+_SLEEP = [sys.executable, "-c", "import time; time.sleep(60)"]
+# Above every pid_max Linux and macOS allow, so no process can ever hold it.
+_NO_SUCH_PID = 2**22 + 1
+
+
+def _spawn_detached(
+ *, sleeper_session: bool, sleeper: list[str] = _SLEEP, **popen_kwargs
+) -> tuple[subprocess.Popen, int]:
+ """Start a child that starts a sleeper and exits, orphaning the sleeper."""
+
+ spawner = subprocess.Popen(
+ [
+ sys.executable,
+ "-c",
+ "import subprocess, sys; "
+ f"print(subprocess.Popen({sleeper!r}, start_new_session={sleeper_session}, "
+ "stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).pid)",
+ ],
+ stdout=subprocess.PIPE,
+ text=True,
+ **popen_kwargs,
+ )
+ stdout, _ = spawner.communicate(timeout=10)
+ return spawner, int(stdout)
+
+
+def _alive(pid: int) -> bool:
+ try:
+ return psutil.Process(pid).status() != psutil.STATUS_ZOMBIE
+ except psutil.NoSuchProcess:
+ return False
+
+
+def _wait_until_gone(pid: int) -> bool:
+ deadline = time.monotonic() + 10
+ while _alive(pid) and time.monotonic() < deadline:
+ time.sleep(0.05)
+ return not _alive(pid)
+
+
+def stranger():
+ """A live process outside this test's tree: reparented, leading its own session."""
+
+ _, pid = _spawn_detached(sleeper_session=True)
+ yield pid
+ with suppress(ProcessLookupError):
+ _REAL_OS_KILL(pid, signal.SIGKILL)
+
+
+ "send",
+ [
+ pytest.param(lambda pid: os.kill(pid, signal.SIGTERM), id="os.kill"),
+ pytest.param(lambda pid: os.killpg(pid, signal.SIGTERM), id="os.killpg"),
+ pytest.param(lambda pid: psutil.Process(pid).terminate(), id="psutil"),
+ ],
+)
+def test_a_process_the_test_did_not_start_is_refused_and_left_alone(stranger, send, _foreign_signal_guard):
+ with pytest.raises(pytest.fail.Exception, match="was not delivered"):
+ send(stranger)
+ _foreign_signal_guard.violations.clear()
+
+ time.sleep(0.2)
+ assert _alive(stranger), "the refused signal reached the process anyway"
+
+
+def test_the_guard_survives_a_tests_own_monkeypatch_undo(stranger, monkeypatch, _foreign_signal_guard):
+ monkeypatch.undo()
+ with pytest.raises(pytest.fail.Exception, match="was not delivered"):
+ os.kill(stranger, signal.SIGTERM)
+ _foreign_signal_guard.violations.clear()
+
+ time.sleep(0.2)
+ assert _alive(stranger), "the refused signal reached the process anyway"
+
+
+ "send",
+ [
+ pytest.param(lambda: os.kill(os.getpid(), signal.SIGURG), id="own-pid"),
+ pytest.param(lambda: os.kill(0, signal.SIGURG), id="own-group-via-kill"),
+ pytest.param(lambda: os.killpg(os.getpgrp(), signal.SIGURG), id="own-group"),
+ ],
+)
+def test_this_pytest_process_is_refused(send, _foreign_signal_guard):
+ # SIGURG is ignored by default, so a guard that let it through would harm
+ # nothing else in this process group; the handler makes delivery visible.
+ received = []
+ previous = signal.signal(signal.SIGURG, lambda *_: received.append(True))
+ try:
+ with pytest.raises(pytest.fail.Exception, match="was not delivered"):
+ send()
+ _foreign_signal_guard.violations.clear()
+ finally:
+ signal.signal(signal.SIGURG, previous)
+
+ assert received == []
+
+
+ "send",
+ [
+ pytest.param(lambda: os.kill(_NO_SUCH_PID, signal.SIGTERM), id="os.kill"),
+ pytest.param(lambda: os.killpg(_NO_SUCH_PID, signal.SIGTERM), id="os.killpg"),
+ ],
+)
+def test_a_target_that_names_no_process_gets_the_real_outcome(send, _foreign_signal_guard):
+ # Process-tree teardown signals descendants it collected earlier, and some
+ # have exited by then; that is ESRCH, as without the guard, not a failure.
+ with pytest.raises(ProcessLookupError):
+ send()
+ assert _foreign_signal_guard.violations == []
+
+
+ "stop",
+ [
+ pytest.param(lambda child: os.kill(child.pid, signal.SIGTERM), id="os.kill"),
+ pytest.param(lambda child: os.killpg(child.pid, signal.SIGTERM), id="os.killpg"),
+ pytest.param(lambda child: psutil.Process(child.pid).terminate(), id="psutil"),
+ ],
+)
+def test_a_child_the_test_started_stays_signalable(stop):
+ child = subprocess.Popen(_SLEEP, start_new_session=True)
+ try:
+ stop(child)
+ assert child.wait(timeout=10) == -signal.SIGTERM
+ # Signalling it once reaped keeps the real outcome instead of a guard failure.
+ with pytest.raises(ProcessLookupError):
+ os.kill(child.pid, signal.SIGTERM)
+ finally:
+ if child.poll() is None:
+ child.kill()
+ child.wait(timeout=10)
+
+
+ "stop",
+ [
+ pytest.param(lambda leader, orphan: os.killpg(leader, signal.SIGTERM), id="its-group"),
+ pytest.param(lambda leader, orphan: os.kill(orphan, signal.SIGTERM), id="itself"),
+ ],
+)
+def test_an_orphan_left_in_a_group_the_test_started_stays_signalable(stop):
+ leader, orphan = _spawn_detached(sleeper_session=False, start_new_session=True)
+ try:
+ assert psutil.Process(orphan).ppid() != os.getpid(), "the sleeper was not reparented"
+ stop(leader.pid, orphan)
+ assert _wait_until_gone(orphan)
+ finally:
+ with suppress(ProcessLookupError):
+ _REAL_OS_KILL(orphan, signal.SIGKILL)
+
+
+def test_a_group_member_that_exits_during_the_check_is_not_foreign():
+ # Like the Docker entrypoint's shell, the group keeps starting commands that
+ # exit at once, so a member listed for the check can be gone when asked about.
+ churn = subprocess.Popen(
+ ["/bin/sh", "-c", "for _ in 1 2 3 4 5 6 7 8; do (while :; do /bin/true; done) & done; wait"],
+ start_new_session=True,
+ )
+ try:
+ time.sleep(0.2)
+ for _ in range(1000):
+ # Ignored by default, so every round reaches the whole group harmlessly.
+ os.killpg(churn.pid, signal.SIGURG)
+ finally:
+ os.killpg(churn.pid, signal.SIGKILL)
+ churn.wait(timeout=10)
+
+
+ "stop",
+ [
+ pytest.param(lambda pid: os.kill(pid, signal.SIGTERM), id="os.kill"),
+ pytest.param(lambda pid: os.killpg(pid, signal.SIGTERM), id="os.killpg"),
+ ],
+)
+def test_a_detached_process_naming_this_tests_tmp_path_stays_signalable(stop, tmp_path):
+ # Its spawner has exited and it leads its own session, so only the temp
+ # directory in its command line ties it to this test.
+ _, detached = _spawn_detached(sleeper_session=True, sleeper=[*_SLEEP, str(tmp_path)])
+ try:
+ stop(detached)
+ assert _wait_until_gone(detached)
+ finally:
+ with suppress(ProcessLookupError):
+ _REAL_OS_KILL(detached, signal.SIGKILL)
+
+
+ "named",
+ [
+ pytest.param(lambda tmp_path: tmp_path.parent / "another_test0", id="another-tests-dir"),
+ pytest.param(lambda tmp_path: f"{tmp_path}0", id="a-longer-name-it-prefixes"),
+ ],
+)
+def test_a_detached_process_naming_another_tests_temp_dir_is_refused(named, tmp_path, _foreign_signal_guard):
+ # Every test's tmp_path shares the session's temp root, so that root proves nothing.
+ _, detached = _spawn_detached(sleeper_session=True, sleeper=[*_SLEEP, str(named(tmp_path))])
+ try:
+ with pytest.raises(pytest.fail.Exception, match="was not delivered"):
+ os.kill(detached, signal.SIGTERM)
+ _foreign_signal_guard.violations.clear()
+
+ time.sleep(0.2)
+ assert _alive(detached), "the refused signal reached the process anyway"
+ finally:
+ with suppress(ProcessLookupError):
+ _REAL_OS_KILL(detached, signal.SIGKILL)
+
+
+def test_signal_zero_still_probes_any_pid(stranger):
+ os.kill(stranger, 0)
+ with pytest.raises(ProcessLookupError):
+ os.kill(_NO_SUCH_PID, 0)
+
+
[email protected]_foreign_signals(reason="asserts the opt-out delivers what the guard would refuse")
+def test_the_opt_out_marker_delivers_a_signal_the_guard_would_refuse(stranger):
+ os.kill(stranger, signal.SIGTERM)
+ assert _wait_until_gone(stranger)
diff --git a/tests/test_vibe_cli.py b/tests/test_vibe_cli.py
index 3de4d88b8..0cd36fb64 100644
--- a/tests/test_vibe_cli.py
+++ b/tests/test_vibe_cli.py
@@ -1454,9 +1454,11 @@ def receipt_runtime(monkeypatch):
monkeypatch.setattr(runtime, "wait_for_ui_server", lambda host, port: True)
monkeypatch.setattr(runtime, "spawn_service_background_process", spawn_service)
monkeypatch.setattr(runtime, "spawn_background", spawn_ui)
- # A failed start now rolls the service back, and the real stop_service()
- # resolves its target from the live process table rather than from anything
- # this fixture controls. Record the call instead of making it.
+ # A failed start rolls back the UI and then the service it spawned. The
+ # real stops act on the process table, where the fake pids above can name
+ # someone else's live process: the real stop_ui() would signal pid 5678.
+ # Record both calls instead of making them.
+ monkeypatch.setattr(runtime, "stop_ui", lambda **kwargs: stopped.append("stop_ui") or True)
monkeypatch.setattr(runtime, "stop_service", lambda: stopped.append("stop_service") or True)
return SimpleNamespace(
service_path=service_path, ui_path=ui_path, process_times=process_times,
@@ -1579,8 +1581,8 @@ def test_cmd_start_does_not_emit_a_receipt_with_unreadable_identity(monkeypatch,
# The far end of the pre-receipt region, and the only failure in it that
# needs no monkeypatched failure to reach: the receipt builder rejects an
# identity it cannot read, with the service this command started already up.
- assert receipt_runtime.stopped == ["stop_service"], (
- f"a start that could not build its receipt left its own service running: {receipt_runtime.stopped}"
+ assert receipt_runtime.stopped == ["stop_ui", "stop_service"], (
+ f"a start that could not build its receipt must stop its UI, then its service: {receipt_runtime.stopped}"
)