diff --git a/cli.py b/cli.py index d429e14fc9eb7..56e19ef423ad3 100644 --- a/cli.py +++ b/cli.py @@ -10874,7 +10874,7 @@ def _confirm_and_reload_mcp(self, cmd_original: str = "") -> None: if not confirm_required: with self._busy_command(self._slow_command_status(cmd_original)): - self._reload_mcp() + self._run_reload_mcp_with_timeout() return # Render warning + prompt. Use the same prompt_toolkit-native composer @@ -10914,7 +10914,20 @@ def _confirm_and_reload_mcp(self, cmd_original: str = "") -> None: print("⚠️ Couldn't persist opt-out — reloading once.") with self._busy_command(self._slow_command_status(cmd_original)): - self._reload_mcp() + self._run_reload_mcp_with_timeout() + + def _run_reload_mcp_with_timeout(self, timeout_seconds: float = 30.0) -> bool: + """Run MCP reload on a worker thread so slash-command input stays recoverable.""" + reload_thread = threading.Thread(target=self._reload_mcp, daemon=True) + reload_thread.start() + reload_thread.join(timeout=timeout_seconds) + if reload_thread.is_alive(): + print( + f" ⚠️ MCP reload timed out ({timeout_seconds:g}s). " + "Some servers may still be reconnecting." + ) + return False + return True def _reload_mcp(self): """Reload MCP servers: disconnect all, re-read config.yaml, reconnect. diff --git a/tests/cli/test_cli_loading_indicator.py b/tests/cli/test_cli_loading_indicator.py index dd7bdb68d1344..0718ab78308c7 100644 --- a/tests/cli/test_cli_loading_indicator.py +++ b/tests/cli/test_cli_loading_indicator.py @@ -1,5 +1,7 @@ """Regression tests for loading feedback on slow slash commands.""" +import threading +import time from unittest.mock import patch from cli import HermesCLI @@ -47,6 +49,7 @@ def test_reload_mcp_sets_busy_state_and_prints_status(self, capsys): def fake_reload(): seen["running"] = cli_obj._command_running seen["status"] = cli_obj._command_status + seen["thread_id"] = threading.get_ident() print("reload done") # /reload-mcp now wraps the actual reload in a prompt-cache-invalidation @@ -66,7 +69,29 @@ def fake_reload(): assert seen == { "running": True, "status": "Reloading MCP servers...", + "thread_id": seen["thread_id"], } + assert seen["thread_id"] != threading.get_ident() assert cli_obj._command_running is False assert cli_obj._command_status == "" assert invalidate_mock.call_count == 2 + + def test_reload_mcp_timeout_warns_without_blocking_forever(self, capsys): + cli_obj = self._make_cli() + stop_event = threading.Event() + + def fake_reload(): + stop_event.wait(0.1) + + with patch.object(cli_obj, "_reload_mcp", side_effect=fake_reload), \ + patch.object(cli_obj, "_invalidate"): + start = time.monotonic() + result = cli_obj._run_reload_mcp_with_timeout(timeout_seconds=0.01) + elapsed = time.monotonic() - start + + stop_event.set() + + output = capsys.readouterr().out + assert result is False + assert elapsed < 0.05 + assert "MCP reload timed out (0.01s)." in output