Skip to content

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

fixed cdp error timeout detection in browser closed

Saurav Panda126a420dcead
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index bfa67d275..7916fdb8b 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -1249,8 +1249,23 @@ class Agent(Generic[Context, AgentStructuredOutput]):
return None
def _is_browser_closed_error(self, error: Exception) -> bool:
- """Check if the browser has been closed or disconnected."""
- return self.browser_session._cdp_client_root is None
+ """Check if the browser has been closed or disconnected.
+
+ Only returns True when the error itself is a CDP/WebSocket connection failure
+ AND the CDP client is gone. Avoids false positives on unrelated errors
+ (element not found, timeouts, parse errors) that happen to coincide with
+ a transient None state during reconnects or resets.
+ """
+ error_str = str(error).lower()
+ is_connection_error = (
+ isinstance(error, ConnectionError)
+ or 'websocket connection closed' in error_str
+ or 'connection closed' in error_str
+ or 'browser has been closed' in error_str
+ or 'browser closed' in error_str
+ or 'no browser' in error_str
+ )
+ return is_connection_error and self.browser_session._cdp_client_root is None
async def _finalize(self, browser_state_summary: BrowserStateSummary | None) -> None:
"""Finalize the step with history, logging, and events"""