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(runtime): prevent duplicate managed service starts

cyhhaoc45e44850f6d
diff --git a/tests/test_runtime_service_lock.py b/tests/test_runtime_service_lock.py
new file mode 100644
index 000000000..120aab55c
--- /dev/null
+++ b/tests/test_runtime_service_lock.py
@@ -0,0 +1,28 @@
+import sys
+import tempfile
+import unittest
+from pathlib import Path
+from unittest.mock import patch
+
+sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
+
+from vibe import runtime
+
+
+class RuntimeServiceLockTests(unittest.TestCase):
+ def test_start_service_reuses_existing_live_pid(self):
+ with tempfile.TemporaryDirectory() as tmpdir:
+ pid_path = Path(tmpdir) / "service.pid"
+ pid_path.write_text("12345", encoding="utf-8")
+
+ 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()
+
+ self.assertEqual(pid, 12345)
+ spawn_background.assert_not_called()
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/vibe/runtime.py b/vibe/runtime.py
index 36dfd6e4f..01811d725 100644
--- a/vibe/runtime.py
+++ b/vibe/runtime.py
@@ -3,6 +3,7 @@ import os
import signal
import subprocess
import sys
+import threading
import time
from pathlib import Path
@@ -75,6 +76,7 @@ def get_working_dir() -> Path:
ROOT_DIR = get_project_root() # For backward compatibility
MAIN_PATH = get_service_main_path()
+_SERVICE_LOCK = threading.Lock()
def ensure_dirs():
@@ -197,13 +199,24 @@ def render_status():
def start_service():
- main_path = get_service_main_path()
- return spawn_background(
- [sys.executable, str(main_path)],
- paths.get_runtime_pid_path(),
- "service_stdout.log",
- "service_stderr.log",
- )
+ with _SERVICE_LOCK:
+ pid_path = paths.get_runtime_pid_path()
+ if pid_path.exists():
+ try:
+ existing_pid = int(pid_path.read_text(encoding="utf-8").strip())
+ except Exception:
+ existing_pid = 0
+ if existing_pid and pid_alive(existing_pid):
+ return existing_pid
+ pid_path.unlink(missing_ok=True)
+
+ main_path = get_service_main_path()
+ return spawn_background(
+ [sys.executable, str(main_path)],
+ pid_path,
+ "service_stdout.log",
+ "service_stderr.log",
+ )
def start_ui(host, port):
@@ -217,7 +230,8 @@ def start_ui(host, port):
def stop_service():
- return stop_process(paths.get_runtime_pid_path())
+ with _SERVICE_LOCK:
+ return stop_process(paths.get_runtime_pid_path())
def stop_ui():