Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
Clean up failed Rust protocol handshake
Gregor Žunič66dec5bb3f06
diff --git a/browser_use/rust/service.py b/browser_use/rust/service.pyindex e176c9ce3..61752beea 100644--- a/browser_use/rust/service.py+++ b/browser_use/rust/service.py@@ -6260,16 +6260,24 @@ class Agent(Generic[Context, AgentStructuredOutput]):self._sdk_browser_id = Noneself.terminal_session_id = Noneself._sdk_client = RustSdkClient(self._sdk_server_argv(), self._run_env())- ping = await self._sdk_client.call('runtime.ping')- protocol_version = ping.get('sdk_protocol_version') if isinstance(ping, dict) else None- if protocol_version != 1:+ try:+ ping = await self._sdk_client.call('runtime.ping')+ protocol_version = ping.get('sdk_protocol_version') if isinstance(ping, dict) else None+ if protocol_version == 1:+ return self._sdk_client+ except Exception as exc:await self._sdk_client.close()self._sdk_client = Noneraise RustAgentError(- f'Unsupported browser-use-terminal SDK protocol {protocol_version!r}; expected 1. '+ 'Failed to negotiate browser-use-terminal SDK protocol via runtime.ping. ''Install a compatible browser-use-core package or set BROWSER_USE_TERMINAL_BINARY to a compatible binary.'- )- return self._sdk_client+ ) from exc+ await self._sdk_client.close()+ self._sdk_client = None+ raise RustAgentError(+ f'Unsupported browser-use-terminal SDK protocol {protocol_version!r}; expected 1. '+ 'Install a compatible browser-use-core package or set BROWSER_USE_TERMINAL_BINARY to a compatible binary.'+ )def _sdk_llm_payload(self) -> dict[str, Any]:provider = _llm_provider_name(self.llm) or 'browser-use'diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.pyindex e628df2e6..cfb3e3941 100644--- a/tests/ci/test_rust_agent.py+++ b/tests/ci/test_rust_agent.py@@ -3326,6 +3326,34 @@ print(json.dumps({"jsonrpc": "2.0", "id": request["id"], "result": {"ok": True}}assert 'Unsupported browser-use-terminal SDK protocol' in str(exc_info.value)+async def test_rust_agent_clears_sdk_client_when_protocol_ping_fails(monkeypatch):+ import browser_use.rust.service as rust_service+ from browser_use.rust import Agent++ class LLM:+ model = 'gpt-test'++ script = r"""+import json+import sys++request = json.loads(sys.stdin.readline())+print(json.dumps({+ "jsonrpc": "2.0",+ "id": request["id"],+ "error": {"code": -32000, "message": "ping failed"},+}), flush=True)+"""+ agent = Agent(task='answer', llm=LLM())+ monkeypatch.setattr(agent, '_sdk_server_argv', lambda: [sys.executable, '-c', script])++ with pytest.raises(rust_service.RustAgentError) as exc_info:+ await agent._ensure_sdk_client()++ assert 'Failed to negotiate browser-use-terminal SDK protocol via runtime.ping' in str(exc_info.value)+ assert agent._sdk_client is None++def test_rust_agent_bridges_llm_credentials_to_terminal_env(monkeypatch):from browser_use.rust import Agent