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.

Validate service pid ownership before reuse

cyh516661cf403a
diff --git a/tests/test_runtime_service_lock.py b/tests/test_runtime_service_lock.py
index 120aab55c..bd189f104 100644
--- a/tests/test_runtime_service_lock.py
+++ b/tests/test_runtime_service_lock.py
@@ -17,12 +17,35 @@ class RuntimeServiceLockTests(unittest.TestCase):
with patch("vibe.runtime.paths.get_runtime_pid_path", return_value=pid_path):
with patch("vibe.runtime.pid_alive", return_value=True):
- with patch("vibe.runtime.spawn_background") as spawn_background:
- pid = runtime.start_service()
+ with patch(
+ "vibe.runtime.get_process_command",
+ return_value=f"{sys.executable} {runtime.get_service_main_path()}",
+ ):
+ with patch("vibe.runtime.spawn_background") as spawn_background:
+ pid = runtime.start_service()
self.assertEqual(pid, 12345)
spawn_background.assert_not_called()
+ def test_start_service_ignores_reused_unrelated_pid(self):
+ with tempfile.TemporaryDirectory() as tmpdir:
+ pid_path = Path(tmpdir) / "service.pid"
+ pid_path.write_text("12345", encoding="utf-8")
+
+ def fake_spawn(args, target_pid_path, stdout_name, stderr_name, env=None):
+ target_pid_path.write_text("67890", encoding="utf-8")
+ return 67890
+
+ with patch("vibe.runtime.paths.get_runtime_pid_path", return_value=pid_path):
+ with patch("vibe.runtime.pid_alive", return_value=True):
+ with patch("vibe.runtime.get_process_command", return_value="/usr/bin/unrelated --work"):
+ with patch("vibe.runtime.spawn_background", side_effect=fake_spawn) as spawn_background:
+ pid = runtime.start_service()
+
+ self.assertEqual(pid, 67890)
+ spawn_background.assert_called_once()
+ self.assertEqual(pid_path.read_text(encoding="utf-8"), "67890")
+
if __name__ == "__main__":
unittest.main()
diff --git a/vibe/runtime.py b/vibe/runtime.py
index e0b205bab..25bc7ccca 100644
--- a/vibe/runtime.py
+++ b/vibe/runtime.py
@@ -432,6 +432,28 @@ def read_status():
return read_json(paths.get_runtime_status_path()) or {}
+def _command_references_path(command: str | None, expected_path: Path) -> bool:
+ if not command:
+ return False
+ try:
+ args = shlex.split(command, posix=(os.name != "nt"))
+ except ValueError:
+ return False
+ expected_resolved = expected_path.resolve()
+ for arg in args:
+ cleaned_arg = arg.strip("\"'")
+ try:
+ if Path(cleaned_arg).resolve() == expected_resolved:
+ return True
+ except (OSError, RuntimeError):
+ continue
+ return False
+
+
+def _pid_matches_service(pid: int) -> bool:
+ return _command_references_path(get_process_command(pid), get_service_main_path())
+
+
def render_status():
status = read_status()
pid_path = paths.get_runtime_pid_path()
@@ -451,7 +473,12 @@ def start_service():
except Exception:
existing_pid = 0
if existing_pid and pid_alive(existing_pid):
- return existing_pid
+ if _pid_matches_service(existing_pid):
+ return existing_pid
+ logger.warning(
+ "Ignoring stale service pid file pid=%s because it does not match the Vibe service",
+ existing_pid,
+ )
pid_path.unlink(missing_ok=True)
main_path = get_service_main_path()