"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(cli): clean up development server children
xkam7ardf24ecf9556f
diff --git a/agent/cli/_legacy.py b/agent/cli/_legacy.pyindex a648f075d..7c5d1f792 100644--- a/agent/cli/_legacy.py+++ b/agent/cli/_legacy.py@@ -5332,9 +5332,8 @@ def cmd_dev()console.print("[dim]Press Ctrl+C to stop both servers.[/dim]\n")- backend = subprocess.Popen(backend_cmd, cwd=str(AGENT_DIR))- frontend = subprocess.Popen(frontend_cmd, cwd=str(frontend_dir))- children = [backend, frontend]+ children: List[subprocess.Popen] = []+ exit_code = EXIT_SUCCESSdef _terminate_all() -> None:for child in children:@@ -5344,28 +5343,41 @@ def cmd_dev(except OSError:pass- # Wire signal handlers. On Windows, SIGTERM does not exist and signal- # handlers must be installed from the main thread; KeyboardInterrupt is- # the cross-platform path for Ctrl+C.- if threading.current_thread() is threading.main_thread():- try:- signal.signal(signal.SIGINT, lambda *_: _terminate_all())- except (ValueError, OSError):- pass- try:- signal.signal(signal.SIGTERM, lambda *_: _terminate_all())- except (AttributeError, ValueError, OSError):- pass-try:+ backend = subprocess.Popen(backend_cmd, cwd=str(AGENT_DIR))+ children.append(backend)+ frontend = subprocess.Popen(frontend_cmd, cwd=str(frontend_dir))+ children.append(frontend)++ # Wire signal handlers only after both children are tracked. On+ # Windows, SIGTERM may not exist and handlers must be installed from+ # the main thread; KeyboardInterrupt remains the portable Ctrl+C path.+ if threading.current_thread() is threading.main_thread():+ try:+ signal.signal(signal.SIGINT, lambda *_: _terminate_all())+ except (ValueError, OSError):+ pass+ try:+ signal.signal(signal.SIGTERM, lambda *_: _terminate_all())+ except (AttributeError, ValueError, OSError):+ pass+# Wait for whichever process exits first; if it's the backend we# bring the frontend down too, and vice versa.while True:time.sleep(0.5)- if backend.poll() is not None or frontend.poll() is not None:+ return_codes = [backend.poll(), frontend.poll()]+ if any(code is not None for code in return_codes):+ exit_code = next(+ (code for code in return_codes if code not in (None, EXIT_SUCCESS)),+ EXIT_SUCCESS,+ )breakexcept KeyboardInterrupt:pass+ except OSError as exc:+ console.print(f"[red]Failed to start development server:[/red] {exc}")+ exit_code = EXIT_RUN_FAILEDfinally:_terminate_all()# Give the children a brief grace period, then force-kill.@@ -5379,8 +5391,12 @@ def cmd_dev(child.kill()except OSError:pass+ try:+ child.wait(timeout=1.0)+ except (OSError, subprocess.TimeoutExpired):+ pass- return EXIT_SUCCESS+ return exit_codedef main(argv: list[str] | None = None) -> int:diff --git a/agent/tests/test_cli_setup_dev.py b/agent/tests/test_cli_setup_dev.pyindex a4017cd14..2bc1b381a 100644--- a/agent/tests/test_cli_setup_dev.py+++ b/agent/tests/test_cli_setup_dev.py@@ -13,6 +13,7 @@ from pathlib import Pathfrom unittest.mock import MagicMock, patchimport cli+import pytestclass TestIsWindows:@@ -194,6 +195,77 @@ class TestCmdDev:port_idx = frontend_cmd.index("--port") + 1assert frontend_cmd[port_idx] == "6000"+ @pytest.mark.parametrize(+ ("backend_code", "frontend_code", "expected"),+ [(7, None, 7), (None, 9, 9)],+ )+ def test_returns_first_child_failure_and_stops_sibling(+ self,+ tmp_path: Path,+ backend_code: int | None,+ frontend_code: int | None,+ expected: int,+ ) -> None:+ frontend_dir = _make_frontend_with_vite(tmp_path)+ backend = MagicMock()+ backend.poll.return_value = backend_code+ frontend = MagicMock()+ frontend.poll.return_value = frontend_code++ with patch(+ "cli._legacy._resolve_node_and_npm",+ return_value=("/usr/bin/node", "/usr/bin/npm"),+ ):+ with patch("cli._legacy.subprocess.Popen", side_effect=[backend, frontend]):+ with patch.object(cli._legacy.time, "sleep", return_value=None):+ rc = cli._legacy.cmd_dev(frontend_dir=frontend_dir)++ assert rc == expected+ running_sibling = frontend if backend_code is not None else backend+ running_sibling.terminate.assert_called_once()++ def test_second_spawn_failure_cleans_up_backend(self, tmp_path: Path) -> None:+ frontend_dir = _make_frontend_with_vite(tmp_path)+ backend = MagicMock()+ backend.poll.return_value = None++ with patch(+ "cli._legacy._resolve_node_and_npm",+ return_value=("/usr/bin/node", "/usr/bin/npm"),+ ):+ with patch(+ "cli._legacy.subprocess.Popen",+ side_effect=[backend, OSError("frontend failed to start")],+ ):+ rc = cli._legacy.cmd_dev(frontend_dir=frontend_dir)++ assert rc == cli._legacy.EXIT_RUN_FAILED+ backend.terminate.assert_called_once()+ backend.wait.assert_called_once()++ def test_interrupt_cleans_up_both_children(self, tmp_path: Path) -> None:+ frontend_dir = _make_frontend_with_vite(tmp_path)+ backend = MagicMock()+ backend.poll.return_value = None+ frontend = MagicMock()+ frontend.poll.return_value = None++ with patch(+ "cli._legacy._resolve_node_and_npm",+ return_value=("/usr/bin/node", "/usr/bin/npm"),+ ):+ with patch("cli._legacy.subprocess.Popen", side_effect=[backend, frontend]):+ with patch.object(+ cli._legacy.time, "sleep", side_effect=KeyboardInterrupt+ ):+ rc = cli._legacy.cmd_dev(frontend_dir=frontend_dir)++ assert rc == cli._legacy.EXIT_SUCCESS+ backend.terminate.assert_called_once()+ frontend.terminate.assert_called_once()+ backend.wait.assert_called_once()+ frontend.wait.assert_called_once()+def _make_frontend_with_vite(tmp_path: Path) -> Path:"""Create a fake frontend directory with a placeholder Vite binary so