Skip to content

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

Close Rust agent browser sessions

Magnus Müller1c8dc7dc1080
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index 452ee6caa..3f87abe4d 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -805,10 +805,15 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai
- Warnings include placeholder/domain metadata but do not log raw secret values.
- Proof: `test_rust_agent_warns_about_sensitive_data_domain_constraints`.
+159. Rust Agent close lifecycle parity
+ - `Agent.close()` now mirrors Browser Use cleanup by calling `browser_session.kill()` when a browser session is present and `browser_profile.keep_alive` is not enabled.
+ - Keep-alive sessions are preserved, and lightweight custom sessions without a Browser Use browser process remain safe.
+ - Proof: `test_rust_agent_close_kills_non_keep_alive_browser_session`.
+
## Current Verification
- `python3 -m py_compile browser_use/agent/service.py browser_use/rust/service.py browser_use/rust/__init__.py browser_use/__init__.py tests/ci/test_rust_agent.py examples/rust_agent/basic.py examples/rust_agent/real_v8_smoke.py`
-- `uv run pytest -q tests/ci/test_rust_agent.py` (130 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (131 tests)
- `cargo build -q -p browser-use-cli` on terminal branch `magnus/browser-use-rust-main-integration`
- `cargo test -q -p browser-use-cli run_codex_session_command_accepts_task_id_and_model -- --nocapture`
- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-agent selected_remote_cdp_mode_allows_remote_cdp_connect -- --nocapture`
diff --git a/browser_use/rust/service.py b/browser_use/rust/service.py
index 09dcee6f5..798d445dd 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -3606,11 +3606,18 @@ class Agent(Generic[Context, AgentStructuredOutput]):
self._external_pause_event.set()
async def close(self):
- """Browser Use-compatible close hook.
-
- The Rust terminal owns browser lifecycle for managed modes, and remote CDP
- browsers are owned by the caller, so the Python wrapper has nothing to close.
- """
+ """Close Browser Use session resources when the caller did not request keep-alive."""
+ if self.browser_session is not None:
+ profile = getattr(self.browser_session, 'browser_profile', None) or self.browser_profile
+ if not getattr(profile, 'keep_alive', False):
+ kill = getattr(self.browser_session, 'kill', None)
+ if callable(kill):
+ result = kill()
+ if inspect.isawaitable(result):
+ await result
+ import gc
+
+ gc.collect()
return None
async def log_completion(self) -> None:
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index d4a8943fa..aaf8567e8 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -3420,6 +3420,40 @@ def test_rust_agent_lifecycle_state_and_save_history(tmp_path):
assert 'saved answer' in history_file.read_text(encoding='utf-8')
+async def test_rust_agent_close_kills_non_keep_alive_browser_session():
+ from browser_use.rust import Agent
+
+ class BrowserProfile:
+ keep_alive = False
+
+ class BrowserSession:
+ browser_profile = BrowserProfile()
+
+ def __init__(self):
+ self.kill_calls = 0
+
+ async def kill(self):
+ self.kill_calls += 1
+
+ session = BrowserSession()
+ agent = Agent(task='close session', browser_session=session, directly_open_url=False)
+
+ await agent.close()
+
+ assert session.kill_calls == 1
+
+ class KeepAliveProfile:
+ keep_alive = True
+
+ keep_alive_session = BrowserSession()
+ keep_alive_session.browser_profile = KeepAliveProfile()
+ keep_alive_agent = Agent(task='keep session', browser_session=keep_alive_session, directly_open_url=False)
+
+ await keep_alive_agent.close()
+
+ assert keep_alive_session.kill_calls == 0
+
+
async def test_rust_agent_check_stop_or_pause_matches_browser_use_lifecycle():
from browser_use.rust import Agent