From 7b777e7db7c7de0e53f30ba36198f56c9dc1a584 Mon Sep 17 00:00:00 2001 From: Earle Lowe <30607889+emlowe@users.noreply.github.com> Date: Thu, 1 Aug 2024 16:40:44 -0700 Subject: [PATCH] Resolve problems on Windows running the sub processes when in editable mode (#18379) * use shutil.which() to find chia executable in a cross platform way * mypy fixes * add simple test for launch_start_daemon * test adjustments --- chia/_tests/cmds/test_daemon.py | 17 ++++++++++++++++- chia/cmds/start_funcs.py | 8 +++++++- chia/daemon/server.py | 4 +++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/chia/_tests/cmds/test_daemon.py b/chia/_tests/cmds/test_daemon.py index a7d50ffa91ec..8c92a004dc6b 100644 --- a/chia/_tests/cmds/test_daemon.py +++ b/chia/_tests/cmds/test_daemon.py @@ -1,5 +1,6 @@ from __future__ import annotations +import sys from pathlib import Path from typing import Any, Dict, Optional @@ -9,7 +10,7 @@ from pytest_mock import MockerFixture from chia.cmds.chia import cli -from chia.cmds.start_funcs import create_start_daemon_connection +from chia.cmds.start_funcs import create_start_daemon_connection, launch_start_daemon @pytest.mark.anyio @@ -52,6 +53,20 @@ def get_current_passphrase() -> Optional[str]: assert not captured.out.endswith("Skipping to unlock keyring\n") +@pytest.mark.anyio +def test_launch_start_daemon(tmp_path: Path) -> None: + sys.argv[0] = "not-exist" + with pytest.raises(FileNotFoundError): + launch_start_daemon(tmp_path) + + helper: Path = Path(sys.executable) + sys.argv[0] = str(helper.parent) + "/chia" + process = launch_start_daemon(tmp_path) + assert process is not None + process.kill() + process.wait() + + def test_start_daemon(tmp_path: Path, empty_keyring: Any, mocker: MockerFixture) -> None: class DummyDaemon: @staticmethod diff --git a/chia/cmds/start_funcs.py b/chia/cmds/start_funcs.py index 0857f7e30719..cdc58ad8c49c 100644 --- a/chia/cmds/start_funcs.py +++ b/chia/cmds/start_funcs.py @@ -2,6 +2,7 @@ import asyncio import os +import shutil import subprocess import sys from concurrent.futures import ThreadPoolExecutor @@ -21,8 +22,13 @@ def launch_start_daemon(root_path: Path) -> subprocess.Popen: if sys.platform == "win32": creationflags = subprocess.CREATE_NEW_PROCESS_GROUP | subprocess.CREATE_NO_WINDOW + path_helper: Path = Path(sys.argv[0]) + cmd_to_execute = shutil.which(cmd=path_helper.name, path=path_helper.parent) + if cmd_to_execute is None: + cmd_to_execute = sys.argv[0] + process = subprocess.Popen( - [sys.argv[0], "run_daemon", "--wait-for-unlock"], + [cmd_to_execute, "run_daemon", "--wait-for-unlock"], encoding="utf-8", stdout=subprocess.PIPE, creationflags=creationflags, diff --git a/chia/daemon/server.py b/chia/daemon/server.py index b145a7232ac4..d1c0be1866cd 100644 --- a/chia/daemon/server.py +++ b/chia/daemon/server.py @@ -5,6 +5,7 @@ import json import logging import os +import shutil import signal import ssl import subprocess @@ -112,7 +113,8 @@ def executable_for_service(service_name: str) -> str: application_path = os.path.dirname(__file__) def executable_for_service(service_name: str) -> str: - return service_name + cmd_to_exec = shutil.which(service_name) + return cmd_to_exec if cmd_to_exec is not None else service_name async def ping() -> Dict[str, Any]: