Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
Implement session integrity check in DefaultActionWatchdog and enhance error handling in BaseWatchdog to manage timeouts gracefully
Magnus Müller4ebfadbb7a8b
diff --git a/browser_use/browser/default_action_watchdog.py b/browser_use/browser/default_action_watchdog.pyindex 5c55451ea..b86fa0e1e 100644--- a/browser_use/browser/default_action_watchdog.py+++ b/browser_use/browser/default_action_watchdog.py@@ -34,6 +34,19 @@ class DefaultActionWatchdog(BaseWatchdog):async def on_ClickElementEvent(self, event: ClickElementEvent) -> dict[str, str] | None:"""Handle click request with CDP."""try:+ # Check if session is alive before attempting any operations+ if not self.browser_session.agent_focus or not self.browser_session.agent_focus.target_id:+ error_msg = 'Cannot execute click: browser session is corrupted (target_id=None). Session may have crashed.'+ self.logger.error(f'⚠️ {error_msg}')+ self.event_bus.dispatch(+ BrowserErrorEvent(+ error_type='SessionCorrupted',+ message=error_msg,+ details={'action': 'click', 'target_id': None},+ )+ )+ return {'success': 'false', 'error': error_msg}+# Use the provided nodeelement_node = event.nodeindex_for_logging = element_node.element_index or 'unknown'diff --git a/browser_use/browser/watchdog_base.py b/browser_use/browser/watchdog_base.pyindex 902c0bb31..6120c7c06 100644--- a/browser_use/browser/watchdog_base.py+++ b/browser_use/browser/watchdog_base.py@@ -93,14 +93,18 @@ class BaseWatchdog(BaseModel):)try:result = await actual_handler(event)- self.logger.debug(- f'[{self.__class__.__name__}] {actual_handler.__name__} completed successfully'- )+ self.logger.debug(f'[{self.__class__.__name__}] {actual_handler.__name__} completed successfully')return result- except Exception as e:- self.logger.error(- f'[{self.__class__.__name__}] {actual_handler.__name__} failed: {e}'+ except TimeoutError as e:+ # Handle timeouts gracefully - don't crash the agent+ self.logger.warning(+ f'⏰ [{self.__class__.__name__}] {actual_handler.__name__} timed out - continuing gracefully. '+ f'Event: {event.__class__.__name__}. Error: {e}')+ # Don't re-raise TimeoutError - let the agent continue+ return None+ except Exception as e:+ self.logger.error(f'[{self.__class__.__name__}] {actual_handler.__name__} failed: {e}')raisereturn unique_handler@@ -111,14 +115,14 @@ class BaseWatchdog(BaseModel):# Check if this handler is already registered - throw error if duplicateexisting_handlers = self.event_bus.handlers.get(event_class.__name__, [])handler_names = [getattr(h, '__name__', str(h)) for h in existing_handlers]-+if unique_handler.__name__ in handler_names:raise RuntimeError(f'[{self.__class__.__name__}] Duplicate handler registration attempted! 'f'Handler {unique_handler.__name__} is already registered for {event_name}. 'f'This likely means attach_to_session() was called multiple times.')-+self.event_bus.on(event_class, unique_handler)registered_events.add(event_class)# logger.debug(