Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Bound Rust terminal event reloads

Magnus Müllerbc2e3d6e6a51
diff --git a/browser_use/rust/service.py b/browser_use/rust/service.py
index 670e87d02..14d3514ab 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -4666,22 +4666,36 @@ class Agent(Generic[Context, AgentStructuredOutput]):
return candidate
return None
+ def _events_timeout_seconds(self) -> int:
+ raw = os.getenv('BROWSER_USE_RUST_EVENTS_TIMEOUT_SECONDS')
+ if raw:
+ try:
+ return max(1, int(raw))
+ except ValueError:
+ self.logger.warning(f'Ignoring invalid BROWSER_USE_RUST_EVENTS_TIMEOUT_SECONDS={raw!r}')
+ return 15
+
async def _load_events(self) -> list[dict[str, Any]]:
if not self.terminal_session_id:
return []
binary = find_browser_use_terminal_binary()
- proc = await asyncio.create_subprocess_exec(
- binary,
- *self._state_dir_args(),
- 'events',
- self.terminal_session_id,
- stdout=asyncio.subprocess.PIPE,
- stderr=asyncio.subprocess.PIPE,
- env=self._run_env(),
+ returncode, stdout_text, stderr_text = await self._run_process(
+ [
+ binary,
+ *self._state_dir_args(),
+ 'events',
+ self.terminal_session_id,
+ ],
+ timeout_seconds=self._events_timeout_seconds(),
)
- stdout, _stderr = await proc.communicate()
+ if returncode:
+ self.logger.warning(
+ 'Rust terminal event load failed with code %s: %s',
+ returncode,
+ (stderr_text or stdout_text).strip(),
+ )
events = []
- for line in stdout.decode(errors='replace').splitlines():
+ for line in stdout_text.splitlines():
try:
parsed = json.loads(line)
except json.JSONDecodeError:
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 757a46482..71646c077 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -4968,6 +4968,29 @@ async def test_rust_agent_follow_up_allows_timeout_overrides(monkeypatch):
assert process_calls[1][1] == 45
+async def test_rust_agent_load_events_uses_bounded_terminal_timeout(monkeypatch):
+ from browser_use.rust import Agent
+
+ monkeypatch.setenv('BROWSER_USE_TERMINAL_BINARY', '/tmp/browser-use-terminal')
+ monkeypatch.setenv('BROWSER_USE_RUST_EVENTS_TIMEOUT_SECONDS', '7')
+ agent = Agent(task='Load terminal events.', llm=type('LLM', (), {'model': 'gpt-test'})())
+ agent.terminal_session_id = '12345678-1234-1234-1234-123456789abc'
+ seen = {}
+
+ async def fake_run_process(argv, timeout_seconds=None):
+ seen['argv'] = argv
+ seen['timeout_seconds'] = timeout_seconds
+ return 0, '{"event_type":"session.done","payload":{"result":"answer"}}\nnot-json\n', ''
+
+ agent._run_process = fake_run_process
+
+ events = await agent._load_events()
+
+ assert seen['argv'][-2:] == ['events', '12345678-1234-1234-1234-123456789abc']
+ assert seen['timeout_seconds'] == 7
+ assert events == [{'event_type': 'session.done', 'payload': {'result': 'answer'}}]
+
+
async def test_rust_agent_run_logs_browser_use_lifecycle_dispatch(monkeypatch):
from browser_use.rust import Agent