Skip to content

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

Implement timeout protections for DOM processing and screenshot operations

Magnus Mülleree7d6da25077
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.py
index ca79caada..3bf0c2bde 100644
--- a/browser_use/browser/session.py
+++ b/browser_use/browser/session.py
@@ -49,7 +49,7 @@ from browser_use.browser.views import (
)
from browser_use.dom.clickable_element_processor.service import ClickableElementProcessor
from browser_use.dom.service import DomService
-from browser_use.dom.views import DOMElementNode, SelectorMap
+from browser_use.dom.views import DOMElementNode, DOMState, SelectorMap
from browser_use.utils import match_url_with_domain_pattern, merge_dicts, retry, time_execution_async, time_execution_sync
_GLOB_WARNING_SHOWN = False # used inside _is_url_allowed to avoid spamming the logs with the same warning multiple times
@@ -699,8 +699,8 @@ class BrowserSession(BaseModel):
@retry(
wait=1, # wait 1s between each attempt to take a screenshot (faster retry)
- retries=3, # try up to 3 times to take the screenshot (more attempts)
- timeout=20, # shorter timeout to detect deadlocks faster (was 35s)
+ retries=2, # try up to 3 times to take the screenshot (more attempts)
+ timeout=10, # shorter timeout to detect deadlocks faster (was 35s)
semaphore_name='screenshot_global',
semaphore_limit=3, # only 3 concurrent screenshots at a time total on the entire machine (ideally)
semaphore_scope='global', # because it's a hardware VRAM bottleneck, chrome crashes if too many concurrent screenshots are rendered via CDP
@@ -763,12 +763,12 @@ class BrowserSession(BaseModel):
page.screenshot(
full_page=False,
# scale='css',
- timeout=15000, # Shorter playwright timeout (15s instead of 30s)
+ timeout=5000, # Shorter playwright timeout (15s instead of 30s)
# clip parameter removed - no clipping needed for deadlock protection
animations='allow',
caret='initial',
),
- timeout=18.0, # Extra asyncio timeout as deadlock protection
+ timeout=6.0, # Extra asyncio timeout as deadlock protection
)
except TimeoutError:
self.logger.warning('🚨 Screenshot deadlock detected (asyncio timeout), restarting browser...')
@@ -2700,11 +2700,28 @@ class BrowserSession(BaseModel):
try:
await self.remove_highlights()
dom_service = DomService(page, logger=self.logger)
- content = await dom_service.get_clickable_elements(
- focus_element=focus_element,
- viewport_expansion=self.browser_profile.viewport_expansion,
- highlight_elements=self.browser_profile.highlight_elements,
- )
+ # Add timeout protection for DOM processing to prevent indefinite hangs on complex pages
+ try:
+ content = await asyncio.wait_for(
+ dom_service.get_clickable_elements(
+ focus_element=focus_element,
+ viewport_expansion=self.browser_profile.viewport_expansion,
+ highlight_elements=self.browser_profile.highlight_elements,
+ ),
+ timeout=30.0, # 30 second timeout for DOM processing
+ )
+ except TimeoutError:
+ self.logger.warning(f'DOM processing timed out after 30 seconds for {page.url} - page may be too complex')
+ # empty content
+ element_tree = DOMElementNode(
+ tag_name='',
+ parent=None,
+ is_visible=False,
+ xpath='',
+ attributes={},
+ children=[],
+ )
+ content = DOMState(element_tree=element_tree, selector_map={})
tabs_info = await self.get_tabs_info()
@@ -2730,7 +2747,16 @@ class BrowserSession(BaseModel):
# )
try:
- screenshot_b64 = await self.take_screenshot()
+ # Add timeout protection for screenshot to prevent long hangs on complex pages
+ screenshot_b64 = await asyncio.wait_for(
+ self.take_screenshot(),
+ timeout=10.0, # 15 second timeout for screenshot operations
+ )
+ except TimeoutError:
+ self.logger.warning(
+ f'Screenshot timed out after 15 seconds for {page.url} - page may be too complex or slow to render'
+ )
+ screenshot_b64 = None
except Exception as e:
self.logger.warning(f'Failed to capture screenshot: {type(e).__name__}: {e}')
screenshot_b64 = None
@@ -2776,7 +2802,7 @@ class BrowserSession(BaseModel):
page = await self.get_current_page()
try:
- await page.wait_for_load_state(timeout=5000)
+ await page.wait_for_load_state(timeout=1000)
except Exception:
pass