Personal AI, On Personal Devices
Python
fix(cli): register real daemon pid on Windows (#1050)
Carlos Henrique Quadradof590c3f435c5
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlindex d57b774e..75612bfd 100644--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@ -132,10 +132,10 @@ jobs:run: |uv run python -c "from openjarvis.core.config import _total_ram_gb; ram = _total_ram_gb(); print(f'GlobalMemoryStatusEx RAM = {ram} GB'); assert ram > 0, f'Windows RAM detection returned {ram}, expected > 0'"- - name: Run Windows-specific tests (hardware + CLI)+ - name: Run Windows-specific tests (hardware + CLI + daemon)shell: bashrun: |- uv run pytest tests/hardware/test_hardware_profiles.py tests/cli/test_cli.py -v -m "not live and not cloud"+ uv run pytest tests/hardware/test_hardware_profiles.py tests/cli/test_cli.py tests/cli/test_daemon_cmd.py tests/cli/test_daemon_state.py tests/core/test_utils.py -v -m "not live and not cloud"- name: Install Rust toolchainuses: dtolnay/rust-toolchain@stablediff --git a/src/openjarvis/cli/daemon_cmd.py b/src/openjarvis/cli/daemon_cmd.pyindex a5c347b9..4a6a8e18 100644--- a/src/openjarvis/cli/daemon_cmd.py+++ b/src/openjarvis/cli/daemon_cmd.py@@ -4,6 +4,7 @@ from __future__ import annotationsimport jsonimport os+import secretsimport subprocessimport sysimport time@@ -23,6 +24,7 @@ _LOG_FILE = DEFAULT_CONFIG_DIR / "server.log"# and `restart` fall back to the config defaults and misreport (or silently# move) the port whenever `start` was given an explicit --host/--port._STATE_FILE = DEFAULT_CONFIG_DIR / "server.json"+_LAUNCH_TOKEN_ENV = "OPENJARVIS_DAEMON_LAUNCH_TOKEN"def _pid_alive(pid: int) -> bool:@@ -74,40 +76,74 @@ def _clear_state_unlocked(pid: int) -> None:_STATE_FILE.unlink(missing_ok=True)+def _read_pid_unlocked() -> int | None:+ """Read and clean up daemon state while the state lock is held."""+ pid = _read_pid_file()+ if pid is None:+ _PID_FILE.unlink(missing_ok=True)+ return None+ if not _pid_alive(pid):+ _clear_state_unlocked(pid)+ return None+ return pid++def _read_pid() -> int | None:"""Read PID from pid file, return None if not found or stale."""with _state_lock():- pid = _read_pid_file()- if pid is None:- _PID_FILE.unlink(missing_ok=True)- return None- if not _pid_alive(pid):- _clear_state_unlocked(pid)- return None- return pid+ return _read_pid_unlocked()def _write_pid(- pid: int, host: str = "", port: int | None = None, *, ready: bool = True+ pid: int,+ host: str = "",+ port: int | None = None,+ *,+ ready: bool = True,+ launch_token: str | None = None,) -> None:"""Write PID, plus the address the daemon actually bound to."""with _state_lock():- existing = _read_pid_file()- if existing is not None and existing != pid and _pid_alive(existing):+ _write_pid_unlocked(pid, host, port, ready=ready, launch_token=launch_token)+++def _write_pid_unlocked(+ pid: int,+ host: str = "",+ port: int | None = None,+ *,+ ready: bool = True,+ launch_token: str | None = None,+) -> None:+ """Update the state while holding the lock, allowing one launch to promote."""+ existing = _read_pid_file()+ current = _read_state()+ same_launch = (+ launch_token is not None+ and current.get("launch_token") == launch_token+ and current.get("pid") == existing+ )+ if (+ not ready+ and current.get("ready", True)+ and (current.get("pid") == pid or same_launch)+ ):+ # The server may have bound before its launcher recorded pending state.+ # Keep the actual PID and bound address in either ordering.+ return+ if existing is not None and existing != pid and _pid_alive(existing):+ if not (ready and same_launch and current.get("ready") is False):raise RuntimeError(f"Another server is already registered (PID {existing})")- current = _read_state()- if not ready and current.get("pid") == pid and current.get("ready", True):- # The child may have finished binding before its parent records- # the spawn. Never replace that actual address with a request.- return- secure_write_text(_PID_FILE, str(pid))- if host or port is not None:- state = {"pid": pid, "host": host, "port": port}- if not ready:- state["ready"] = False- secure_write_json(_STATE_FILE, state)- else:- _STATE_FILE.unlink(missing_ok=True)+ secure_write_text(_PID_FILE, str(pid))+ if host or port is not None:+ state = {"pid": pid, "host": host, "port": port}+ if not ready:+ state["ready"] = False+ if launch_token is not None:+ state["launch_token"] = launch_token+ secure_write_json(_STATE_FILE, state)+ else:+ _STATE_FILE.unlink(missing_ok=True)def _read_state() -> dict:@@ -126,6 +162,12 @@ def _read_state() -> dict:or type(state.get("port")) is not intor not 0 <= state["port"] <= 65535or type(state.get("ready", True)) is not bool+ or (+ "launch_token" in state+ and (+ not isinstance(state["launch_token"], str) or not state["launch_token"]+ )+ )):return {}return state@@ -154,7 +196,7 @@ def record_server_state(pid: int, host: str, port: int) -> None:Called by `jarvis serve` itself, so a server supervised by launchd/systemd(which never goes through `jarvis start`) is still reported as running."""- _write_pid(pid, host, port)+ _write_pid(pid, host, port, launch_token=os.environ.get(_LAUNCH_TOKEN_ENV))def clear_server_state(pid: int) -> None:@@ -216,7 +258,6 @@ def start(# daemon. DETACHED_PROCESS gives it no console at all; the new process# group additionally stops a Ctrl-C in the parent reaching it.DEFAULT_CONFIG_DIR.mkdir(parents=True, exist_ok=True)- log_fh = open(_LOG_FILE, "a") # noqa: SIM115spawn_kwargs: dict = {}if sys.platform == "win32":spawn_kwargs["creationflags"] = (@@ -224,20 +265,45 @@ def start()else:spawn_kwargs["start_new_session"] = True- proc = subprocess.Popen(- cmd,- stdout=log_fh,- stderr=log_fh,- **spawn_kwargs,- )- try:- _write_pid(proc.pid, bind_host, bind_port, ready=False)- except RuntimeError as exc:+ launch_token = secrets.token_hex(16)+ proc = None+ registration_error = None+ # Serialize the final check, spawn, and pending registration. The server+ # waits on this same lock if it binds before its launcher finishes writing.+ with _state_lock():+ existing = _read_pid_unlocked()+ if existing is None:+ with open(_LOG_FILE, "a") as log_fh:+ proc = subprocess.Popen(+ cmd,+ stdout=log_fh,+ stderr=log_fh,+ env={**os.environ, _LAUNCH_TOKEN_ENV: launch_token},+ **spawn_kwargs,+ )+ try:+ _write_pid_unlocked(+ proc.pid,+ bind_host,+ bind_port,+ ready=False,+ launch_token=launch_token,+ )+ except (OSError, RuntimeError) as exc:+ registration_error = exc++ if existing is not None:+ console.print(f"[yellow]Server already running (PID {existing}).[/yellow]")+ console.print("Use 'jarvis stop' to stop it first, or 'jarvis restart'.")+ sys.exit(1)+ assert proc is not None+ if registration_error is not None:terminate_process(proc.pid, grace_seconds=10.0)- raise click.ClickException(str(exc)) from exc+ clear_server_state(proc.pid)+ raise click.ClickException(str(registration_error)) from registration_errorconsole.print(- f"[green]OpenJarvis server starting[/green] (PID {proc.pid})\n"+ f"[green]OpenJarvis server starting[/green] (launch PID {proc.pid})\n"f" Requested URL: {_server_url(bind_host, bind_port)}\n"f" Log: {_LOG_FILE}")diff --git a/src/openjarvis/core/utils.py b/src/openjarvis/core/utils.pyindex 9611deb6..54180131 100644--- a/src/openjarvis/core/utils.py+++ b/src/openjarvis/core/utils.py@@ -146,16 +146,20 @@ def terminate_process(pid: int | None, *, grace_seconds: float = 3.0) -> None:"""Terminate *pid* gracefully, escalating to a forced kill (cross-platform).POSIX sends ``SIGTERM`` then, after *grace_seconds*, ``SIGKILL``. Windows- has neither; it uses ``taskkill`` (graceful) then ``taskkill /F /T`` (force,- whole tree). ``signal.SIGKILL`` does not exist on Windows, so it is only- referenced inside the POSIX branch.+ has neither; it uses ``taskkill /T`` (graceful, whole tree) then+ ``taskkill /F /T`` (force). ``signal.SIGKILL`` does not exist on Windows,+ so it is only referenced inside the POSIX branch."""if not process_alive(pid):returnis_windows = platform.system() == "Windows"if is_windows:- subprocess.run(["taskkill", "/PID", str(pid)], capture_output=True, check=False)+ subprocess.run(+ ["taskkill", "/T", "/PID", str(pid)],+ capture_output=True,+ check=False,+ )else:try:os.kill(pid, signal.SIGTERM)diff --git a/tests/cli/test_daemon_cmd.py b/tests/cli/test_daemon_cmd.pyindex b194a093..79a672ee 100644--- a/tests/cli/test_daemon_cmd.py+++ b/tests/cli/test_daemon_cmd.py@@ -3,16 +3,23 @@from __future__ import annotationsimport os+import secretsimport subprocessimport sysimport time+from contextlib import nullcontextfrom pathlib import Pathfrom unittest.mock import MagicMock, patchfrom click.testing import CliRunnerfrom openjarvis.cli import cli-from openjarvis.cli.daemon_cmd import _pid_alive, _read_pid, _write_pid+from openjarvis.cli.daemon_cmd import (+ _pid_alive,+ _read_pid,+ _write_pid,+)+from openjarvis.core.utils import terminate_processclass TestDaemonCommands:@@ -151,9 +158,12 @@ class TestDaemonDetachment:"""with (patch("openjarvis.cli.daemon_cmd._read_pid", return_value=None),- patch("openjarvis.cli.daemon_cmd._write_pid"),+ patch("openjarvis.cli.daemon_cmd._read_pid_unlocked", return_value=None),+ patch("openjarvis.cli.daemon_cmd._state_lock", return_value=nullcontext()),+ patch("openjarvis.cli.daemon_cmd._write_pid_unlocked"),patch("openjarvis.cli.daemon_cmd.load_config"),patch("openjarvis.cli.daemon_cmd.sys.platform", platform),+ patch("openjarvis.cli.daemon_cmd.DEFAULT_CONFIG_DIR"),patch("openjarvis.cli.daemon_cmd.subprocess.Popen") as popen,patch("builtins.open", MagicMock()),):@@ -166,6 +176,81 @@ class TestDaemonDetachment:assert spawns, f"start did not spawn the server: {popen.call_args_list}"return spawns[-1].kwargs+ def test_windows_start_registers_spawned_pid_immediately(+ self, tmp_path: Path+ ) -> None:+ """Both direct Python and launchers can protect the startup interval."""+ with (+ patch("openjarvis.cli.daemon_cmd._read_pid", return_value=None),+ patch("openjarvis.cli.daemon_cmd._read_pid_unlocked", return_value=None),+ patch("openjarvis.cli.daemon_cmd._state_lock", return_value=nullcontext()),+ patch("openjarvis.cli.daemon_cmd.load_config"),+ patch("openjarvis.cli.daemon_cmd.subprocess.Popen") as popen,+ patch("openjarvis.cli.daemon_cmd.DEFAULT_CONFIG_DIR", tmp_path),+ patch("openjarvis.cli.daemon_cmd._LOG_FILE", tmp_path / "server.log"),+ patch("openjarvis.cli.daemon_cmd.sys.platform", "win32"),+ patch("openjarvis.cli.daemon_cmd._write_pid_unlocked") as write_pid,+ patch.object(subprocess, "DETACHED_PROCESS", 0x00000008, create=True),+ patch.object(+ subprocess, "CREATE_NEW_PROCESS_GROUP", 0x00000200, create=True+ ),+ ):+ popen.return_value = MagicMock(pid=4321)++ result = CliRunner().invoke(cli, ["start"])++ assert result.exit_code == 0, result.output+ assert "launch PID 4321" in result.output+ write_pid.assert_called_once()+ assert write_pid.call_args.args[0] == 4321+ assert write_pid.call_args.kwargs["ready"] is False+ token = write_pid.call_args.kwargs["launch_token"]+ assert token+ assert (+ popen.call_args.kwargs["env"]["OPENJARVIS_DAEMON_LAUNCH_TOKEN"] == token+ )++ def test_second_start_rechecks_under_lock(self, tmp_path: Path) -> None:+ """A second start must not spawn after another caller registered."""+ with (+ patch("openjarvis.cli.daemon_cmd._read_pid", return_value=None),+ patch("openjarvis.cli.daemon_cmd._read_pid_unlocked", return_value=4321),+ patch("openjarvis.cli.daemon_cmd._state_lock", return_value=nullcontext()),+ patch("openjarvis.cli.daemon_cmd.load_config"),+ patch("openjarvis.cli.daemon_cmd.DEFAULT_CONFIG_DIR", tmp_path),+ patch("openjarvis.cli.daemon_cmd.subprocess.Popen") as popen,+ ):+ result = CliRunner().invoke(cli, ["start"])+ assert result.exit_code != 0+ assert "already running" in result.output+ popen.assert_not_called()++ def test_failed_registration_terminates_spawned_tree(self, tmp_path: Path) -> None:+ """A failed launch must clean up its launcher and any descendants."""+ with (+ patch("openjarvis.cli.daemon_cmd._read_pid", return_value=None),+ patch("openjarvis.cli.daemon_cmd._read_pid_unlocked", return_value=None),+ patch("openjarvis.cli.daemon_cmd._state_lock", return_value=nullcontext()),+ patch("openjarvis.cli.daemon_cmd.load_config"),+ patch("openjarvis.cli.daemon_cmd.DEFAULT_CONFIG_DIR", tmp_path),+ patch("openjarvis.cli.daemon_cmd._LOG_FILE", tmp_path / "server.log"),+ patch(+ "openjarvis.cli.daemon_cmd._write_pid_unlocked",+ side_effect=RuntimeError("conflict"),+ ),+ patch(+ "openjarvis.cli.daemon_cmd.subprocess.Popen",+ return_value=MagicMock(pid=4321),+ ),+ patch("openjarvis.cli.daemon_cmd.terminate_process") as terminate,+ patch("openjarvis.cli.daemon_cmd.clear_server_state") as clear,+ ):+ result = CliRunner().invoke(cli, ["start"])+ assert result.exit_code != 0+ assert "conflict" in result.output+ terminate.assert_called_once_with(4321, grace_seconds=10.0)+ clear.assert_called_once_with(4321)+def test_windows_spawn_is_detached_from_the_console(self) -> None:# These constants are only exported by ``subprocess`` on Windows.# Supply their documented values so the simulated Windows branch is@@ -207,3 +292,81 @@ class TestDaemonDetachment:kwargs = self._spawn_kwargs("linux")assert kwargs.get("start_new_session") is Trueassert "creationflags" not in kwargs or kwargs["creationflags"] == 0+++def test_subprocess_registers_actual_python_pid(tmp_path: Path, monkeypatch) -> None:+ """A real child registers its PID whether Python is direct or uses a launcher."""+ from openjarvis.cli import daemon_cmd++ pid_file = tmp_path / "server.pid"+ state_file = tmp_path / "server.json"+ child_pid_file = tmp_path / "child.pid"+ ready_file = tmp_path / "child.ready"+ log_file = tmp_path / "child.log"+ token = secrets.token_hex(16)+ monkeypatch.setattr(daemon_cmd, "_PID_FILE", pid_file)+ monkeypatch.setattr(daemon_cmd, "_STATE_FILE", state_file)++ code = """+import os+import time+from pathlib import Path+from openjarvis.cli import daemon_cmd++daemon_cmd._PID_FILE = Path(os.environ["OJ_TEST_PID_FILE"])+daemon_cmd._STATE_FILE = Path(os.environ["OJ_TEST_STATE_FILE"])+pid_file = Path(os.environ["OJ_TEST_CHILD_PID_FILE"])+pid_tmp = pid_file.with_suffix(".tmp")+pid_tmp.write_text(str(os.getpid()))+pid_tmp.replace(pid_file)+try:+ daemon_cmd.record_server_state(os.getpid(), "127.0.0.1", 8899)+ Path(os.environ["OJ_TEST_READY_FILE"]).write_text("ready")+ time.sleep(30)+finally:+ daemon_cmd.clear_server_state(os.getpid())+"""+ env = {+ **os.environ,+ daemon_cmd._LAUNCH_TOKEN_ENV: token,+ "OJ_TEST_PID_FILE": str(pid_file),+ "OJ_TEST_STATE_FILE": str(state_file),+ "OJ_TEST_CHILD_PID_FILE": str(child_pid_file),+ "OJ_TEST_READY_FILE": str(ready_file),+ }+ with log_file.open("w") as log_fh:+ proc = subprocess.Popen(+ [sys.executable, "-c", code],+ stdout=log_fh,+ stderr=log_fh,+ env=env,+ )++ actual_pid = None+ try:+ daemon_cmd._write_pid(proc.pid, "127.0.0.1", 0, ready=False, launch_token=token)+ deadline = time.monotonic() + 15.0+ while time.monotonic() < deadline:+ if child_pid_file.exists():+ actual_pid = int(child_pid_file.read_text())+ if ready_file.exists():+ break+ time.sleep(0.05)++ assert ready_file.exists(), log_file.read_text()+ assert actual_pid is not None+ assert daemon_cmd._read_pid_file() == actual_pid+ assert daemon_cmd._read_state()["pid"] == actual_pid+ assert daemon_cmd._read_state()["port"] == 8899+ assert daemon_cmd._read_state().get("ready", True)+ finally:+ terminate_process(proc.pid, grace_seconds=2.0)+ if actual_pid is None and child_pid_file.exists():+ actual_pid = int(child_pid_file.read_text())+ if actual_pid is not None and actual_pid != proc.pid:+ terminate_process(actual_pid, grace_seconds=2.0)+ try:+ proc.wait(timeout=5)+ except subprocess.TimeoutExpired:+ proc.kill()+ proc.wait(timeout=5)diff --git a/tests/cli/test_daemon_state.py b/tests/cli/test_daemon_state.pyindex 9fdb0a83..3c0e5ddb 100644--- a/tests/cli/test_daemon_state.py+++ b/tests/cli/test_daemon_state.py@@ -120,6 +120,86 @@ def test_parent_cannot_replace_child_bound_port_with_requested_zero(state_dir):assert daemon_cmd._read_state().get("ready", True)+def test_direct_python_promotes_pending_pid(state_dir, monkeypatch):+ """A direct Python executable has no child process to discover."""+ monkeypatch.setenv(daemon_cmd._LAUNCH_TOKEN_ENV, "launch-a")+ daemon_cmd._write_pid(4321, "127.0.0.1", 0, ready=False, launch_token="launch-a")++ daemon_cmd.record_server_state(4321, "127.0.0.1", 8899)++ assert daemon_cmd._read_pid_file() == 4321+ assert daemon_cmd._read_state() == {+ "pid": 4321,+ "host": "127.0.0.1",+ "port": 8899,+ "launch_token": "launch-a",+ }+++def test_launcher_child_promotes_pending_pid_even_after_delay(state_dir, monkeypatch):+ """The child can register whenever it binds; no discovery deadline applies."""+ monkeypatch.setenv(daemon_cmd._LAUNCH_TOKEN_ENV, "launch-a")+ monkeypatch.setattr(daemon_cmd, "_pid_alive", lambda pid: True)+ daemon_cmd._write_pid(4321, "127.0.0.1", 0, ready=False, launch_token="launch-a")++ daemon_cmd.record_server_state(9876, "127.0.0.1", 8899)++ assert daemon_cmd._read_pid_file() == 9876+ assert daemon_cmd._read_state() == {+ "pid": 9876,+ "host": "127.0.0.1",+ "port": 8899,+ "launch_token": "launch-a",+ }+++def test_child_ready_before_launcher_pending_write(state_dir, monkeypatch):+ """The parent cannot overwrite the child's real PID or bound port."""+ monkeypatch.setenv(daemon_cmd._LAUNCH_TOKEN_ENV, "launch-a")+ monkeypatch.setattr(daemon_cmd, "_pid_alive", lambda pid: True)+ daemon_cmd.record_server_state(9876, "127.0.0.1", 8899)++ daemon_cmd._write_pid(4321, "127.0.0.1", 0, ready=False, launch_token="launch-a")++ assert daemon_cmd._read_pid_file() == 9876+ assert daemon_cmd._read_state()["port"] == 8899+ assert daemon_cmd._read_state().get("ready", True)+++def test_other_launch_cannot_claim_live_pending_pid(state_dir, monkeypatch):+ monkeypatch.setattr(daemon_cmd, "_pid_alive", lambda pid: True)+ daemon_cmd._write_pid(4321, "127.0.0.1", 0, ready=False, launch_token="launch-a")++ with pytest.raises(RuntimeError, match="Another server is already registered"):+ daemon_cmd._write_pid(9876, "127.0.0.1", 8899, launch_token="launch-b")++ assert daemon_cmd._read_pid_file() == 4321+ assert daemon_cmd._read_state()["ready"] is False+++def test_same_launch_cannot_replace_ready_server(state_dir, monkeypatch):+ monkeypatch.setattr(daemon_cmd, "_pid_alive", lambda pid: True)+ daemon_cmd._write_pid(4321, "127.0.0.1", 8899, launch_token="launch-a")++ with pytest.raises(RuntimeError, match="Another server is already registered"):+ daemon_cmd._write_pid(9876, "127.0.0.1", 8899, launch_token="launch-a")++ assert daemon_cmd._read_pid_file() == 4321+++def test_other_launcher_cannot_replace_ready_server(state_dir, monkeypatch):+ monkeypatch.setattr(daemon_cmd, "_pid_alive", lambda pid: True)+ daemon_cmd._write_pid(9876, "127.0.0.1", 8899, launch_token="launch-a")++ with pytest.raises(RuntimeError, match="Another server is already registered"):+ daemon_cmd._write_pid(+ 4321, "127.0.0.1", 0, ready=False, launch_token="launch-b"+ )++ assert daemon_cmd._read_pid_file() == 9876+ assert daemon_cmd._read_state()["port"] == 8899++def test_pending_start_does_not_claim_an_address_is_listening(state_dir, monkeypatch):daemon_cmd._write_pid(999, "127.0.0.1", 0, ready=False)monkeypatch.setattr(daemon_cmd, "_pid_alive", lambda pid: True)diff --git a/tests/core/test_utils.py b/tests/core/test_utils.pyindex b6866677..6ca95e4f 100644--- a/tests/core/test_utils.py+++ b/tests/core/test_utils.py@@ -142,6 +142,7 @@ class TestTerminateProcess:def test_terminate_none_no_crash(self) -> None:terminate_process(None)+ @pytest.mark.skipif(platform.system() == "Windows", reason="POSIX-only")def test_posix_escalates_to_sigkill(self) -> None:with (patch("openjarvis.core.utils.platform.system", return_value="Linux"),@@ -162,7 +163,7 @@ class TestTerminateProcess:):terminate_process(1234, grace_seconds=1)run.assert_called_once_with(- ["taskkill", "/PID", "1234"], capture_output=True, check=False+ ["taskkill", "/T", "/PID", "1234"], capture_output=True, check=False)def test_windows_escalates_to_forced_tree_kill(self) -> None:@@ -174,7 +175,7 @@ class TestTerminateProcess:terminate_process(1234, grace_seconds=0)assert run.call_args_list == [call(- ["taskkill", "/PID", "1234"],+ ["taskkill", "/T", "/PID", "1234"],capture_output=True,check=False,),