Skip to content

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

Add timeouts to browser connects and waits

LarsenCundric505d5df40617
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.py
index 43cbd7fd2..92a613ac1 100644
--- a/browser_use/browser/session.py
+++ b/browser_use/browser/session.py
@@ -1403,12 +1403,15 @@ class BrowserSession(BaseModel):
f'(agent_focus stays on {current_focus}...)'
)
- # Resume if waiting for debugger
+ # Resume if waiting for debugger (non-essential, don't let it block connect)
if focus:
try:
- await session.cdp_client.send.Runtime.runIfWaitingForDebugger(session_id=session.session_id)
+ await asyncio.wait_for(
+ session.cdp_client.send.Runtime.runIfWaitingForDebugger(session_id=session.session_id),
+ timeout=3.0,
+ )
except Exception:
- pass # May fail if not waiting
+ pass # May fail if not waiting, or timeout — either is fine
return session
@@ -1669,7 +1672,9 @@ class BrowserSession(BaseModel):
)
# Run a tiny HTTP client to query for the WebSocket URL from the /json/version endpoint
- async with httpx.AsyncClient() as client:
+ # Default httpx timeout is 5s which can race the global wait_for(connect(), 15s).
+ # Use 30s as a safety net for direct connect() callers; the wait_for is the real deadline.
+ async with httpx.AsyncClient(timeout=httpx.Timeout(30.0)) as client:
headers = self.browser_profile.headers or {}
version_info = await client.get(url, headers=headers)
self.logger.debug(f'Raw version info: {str(version_info)}')