Skip to content

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

Finish reconnect cleanup before propagating cancellation

Magnus Muellere16388ba6e29
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.py
index f15fdea33..c45f1213e 100644
--- a/browser_use/browser/session.py
+++ b/browser_use/browser/session.py
@@ -2245,16 +2245,31 @@ class BrowserSession(BaseModel):
self._cdp_client_root = None
self.session_manager = None
self.agent_focus_target_id = None
- if client:
+
+ async def cleanup() -> None:
try:
- await client.stop()
- except Exception:
- self.logger.debug('Failed to close the replacement CDP client')
- if manager:
+ if client:
+ try:
+ await client.stop()
+ except Exception:
+ self.logger.debug('Failed to close the replacement CDP client')
+ finally:
+ if manager:
+ try:
+ await manager.clear()
+ except Exception:
+ self.logger.debug('Failed to clear the replacement session manager')
+
+ cleanup_task = asyncio.create_task(cleanup())
+ cancelled = False
+ while not cleanup_task.done():
try:
- await manager.clear()
- except Exception:
- self.logger.debug('Failed to clear the replacement session manager')
+ await asyncio.shield(cleanup_task)
+ except asyncio.CancelledError:
+ cancelled = True
+ cleanup_task.result()
+ if cancelled:
+ raise asyncio.CancelledError
raise
async def _auto_reconnect(self, max_attempts: int = 3) -> None:
diff --git a/tests/ci/test_browser_session.py b/tests/ci/test_browser_session.py
index b715afbcb..2a909e31b 100644
--- a/tests/ci/test_browser_session.py
+++ b/tests/ci/test_browser_session.py
@@ -45,14 +45,33 @@ async def test_ws_drop_during_reconnect_triggers_follow_up_attempt(monkeypatch)
await session.event_bus.stop(clear=True, timeout=5)
[email protected]('method', ['Target.setDiscoverTargets', 'Target.setAutoAttach'])
[email protected]('failure', ['cancel', 'error'])
+ ('method', 'failure'),
+ [(method, failure) for method in ['Target.setDiscoverTargets', 'Target.setAutoAttach'] for failure in ['cancel', 'error']]
+ + [('Target.setDiscoverTargets', 'stop_cancel')],
+)
async def test_failed_reconnect_closes_replacement_client(monkeypatch, method, failure):
from browser_use.browser._cdp_timeout import TimeoutWrappedCDPClient
+ from browser_use.browser.session_manager import SessionManager
session = BrowserSession(browser_profile=BrowserProfile(headless=True, user_data_dir=None, cdp_url='ws://127.0.0.1:9222'))
started = asyncio.Event()
clients = []
+ stop_started = asyncio.Event()
+ allow_stop = asyncio.Event()
+ manager_cleared = asyncio.Event()
+ original_stop = TimeoutWrappedCDPClient.stop
+ original_clear = SessionManager.clear
+
+ async def stop(client):
+ if failure == 'stop_cancel':
+ stop_started.set()
+ await allow_stop.wait()
+ await original_stop(client)
+
+ async def clear(manager):
+ await original_clear(manager)
+ manager_cleared.set()
async def start(client):
clients.append(client)
@@ -61,12 +80,14 @@ async def test_failed_reconnect_closes_replacement_client(monkeypatch, method, f
async def send_raw(client, method: str, params=None, session_id=None):
if method == blocked_method:
started.set()
- if failure == 'error':
+ if failure in {'error', 'stop_cancel'}:
raise ConnectionError('Initialization failed')
await asyncio.Event().wait()
return {'targetInfos': []} if method == 'Target.getTargets' else {}
blocked_method = method
+ monkeypatch.setattr(TimeoutWrappedCDPClient, 'stop', stop)
+ monkeypatch.setattr(SessionManager, 'clear', clear)
monkeypatch.setattr(TimeoutWrappedCDPClient, 'start', start)
monkeypatch.setattr(TimeoutWrappedCDPClient, 'send_raw', send_raw)
reconnect = asyncio.create_task(session.reconnect())
@@ -74,13 +95,19 @@ async def test_failed_reconnect_closes_replacement_client(monkeypatch, method, f
await asyncio.wait_for(started.wait(), timeout=5)
if failure == 'cancel':
reconnect.cancel()
- with pytest.raises(asyncio.CancelledError if failure == 'cancel' else ConnectionError):
+ if failure == 'stop_cancel':
+ await asyncio.wait_for(stop_started.wait(), timeout=5)
+ reconnect.cancel()
+ allow_stop.set()
+ with pytest.raises(asyncio.CancelledError if failure in {'cancel', 'stop_cancel'} else ConnectionError):
await reconnect
assert session._cdp_client_root is None
assert session.session_manager is None
assert session.agent_focus_target_id is None
assert clients[0]._message_handler_task is None
+ assert manager_cleared.is_set()
finally:
+ allow_stop.set()
if not reconnect.done():
reconnect.cancel()
await asyncio.gather(reconnect, return_exceptions=True)