Skip to content

Commit

Permalink
Resolve problems on Windows running the sub processes when in editabl…
Browse files Browse the repository at this point in the history
…e 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
  • Loading branch information
emlowe authored Aug 1, 2024
1 parent 7e057ca commit 7b777e7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
17 changes: 16 additions & 1 deletion chia/_tests/cmds/test_daemon.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import sys
from pathlib import Path
from typing import Any, Dict, Optional

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion chia/cmds/start_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import asyncio
import os
import shutil
import subprocess
import sys
from concurrent.futures import ThreadPoolExecutor
Expand All @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion chia/daemon/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import json
import logging
import os
import shutil
import signal
import ssl
import subprocess
Expand Down Expand Up @@ -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]:
Expand Down

0 comments on commit 7b777e7

Please sign in to comment.