Skip to content

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

fix: fixed cubic issues + handle onTabClosed event emission correctly

reformedotb2b5a260db5e
diff --git a/browser_use/browser/session_manager.py b/browser_use/browser/session_manager.py
index ab74edbc4..0300cac8d 100644
--- a/browser_use/browser/session_manager.py
+++ b/browser_use/browser/session_manager.py
@@ -223,6 +223,8 @@ class SessionManager:
return
agent_focus_lost = False
+ target_fully_removed = False
+ target_type = None
async with self._lock:
# Remove this session from target's session set
@@ -240,6 +242,8 @@ class SessionManager:
if remaining_sessions == 0:
self.logger.info(f'[SessionManager] No sessions remain for target {target_id[:8]}..., removing from pool')
+ target_fully_removed = True
+
# Check if agent_focus points to this target
agent_focus_lost = (
self.browser_session.agent_focus and self.browser_session.agent_focus.target_id == target_id
@@ -255,30 +259,35 @@ class SessionManager:
# Clean up tracking
del self._target_sessions[target_id]
+ else:
+ # Target not tracked - already removed or never attached
+ self.logger.debug(
+ f'[SessionManager] Session detached from untracked target: target={target_id[:8]}... '
+ f'session={session_id[:8]}... (target was already removed or attach event was missed)'
+ )
- # Clean up target type cache
- if target_id in self._target_types:
- cached_target_type = self._target_types.pop(target_id)
- else:
- cached_target_type = None
+ # Get target type before cleaning up cache (needed for TabClosedEvent dispatch)
+ target_type = self._target_types.get(target_id)
+
+ # Clean up target type cache if target fully removed
+ if target_id not in self._target_sessions and target_id in self._target_types:
+ del self._target_types[target_id]
# Remove from reverse mapping
if session_id in self._session_to_target:
del self._session_to_target[session_id]
- # Dispatch TabClosedEvent only for page/tab targets (not iframes/workers)
- # Use cached target type to avoid extra CDP call
- target_type = self._target_types.get(target_id) if target_id in self._target_types else cached_target_type
-
- if target_type in ('page', 'tab'):
- from browser_use.browser.events import TabClosedEvent
+ # Dispatch TabClosedEvent only for page/tab targets that are fully removed (not iframes/workers or partial detaches)
+ if target_fully_removed:
+ if target_type in ('page', 'tab'):
+ from browser_use.browser.events import TabClosedEvent
- self.browser_session.event_bus.dispatch(TabClosedEvent(target_id=target_id))
- self.logger.debug(f'[SessionManager] Dispatched TabClosedEvent for page target {target_id[:8]}...')
- elif target_type:
- self.logger.debug(
- f'[SessionManager] Target {target_id[:8]}... detached (type={target_type}) - not dispatching TabClosedEvent'
- )
+ self.browser_session.event_bus.dispatch(TabClosedEvent(target_id=target_id))
+ self.logger.debug(f'[SessionManager] Dispatched TabClosedEvent for page target {target_id[:8]}...')
+ elif target_type:
+ self.logger.debug(
+ f'[SessionManager] Target {target_id[:8]}... fully removed (type={target_type}) - not dispatching TabClosedEvent'
+ )
# Auto-recover agent_focus outside the lock to avoid blocking other operations
if agent_focus_lost:
diff --git a/browser_use/browser/watchdog_base.py b/browser_use/browser/watchdog_base.py
index 603f64aec..ddb9ad586 100644
--- a/browser_use/browser/watchdog_base.py
+++ b/browser_use/browser/watchdog_base.py
@@ -158,8 +158,8 @@ class BaseWatchdog(BaseModel):
f'🚌 {watchdog_and_handler_str} ❌ CDP connected but failed to re-create CDP session after error "{type(original_error).__name__}: {original_error}" in {actual_handler.__name__}({event.event_type}#{event.event_id[-4:]}): due to {type(sub_error).__name__}: {sub_error}\n'
)
- # Always re-raise the original error
- raise original_error
+ # Always re-raise the original error with its traceback preserved
+ raise
return unique_handler
@@ -257,4 +257,4 @@ class BaseWatchdog(BaseModel):
except Exception as e:
from browser_use.utils import logger
- logger.error(f'⚠️ Error during BrowserSession {self.__class__.__name__} gargabe collection __del__(): {type(e)}: {e}')
+ logger.error(f'⚠️ Error during BrowserSession {self.__class__.__name__} garbage collection __del__(): {type(e)}: {e}')