Skip to content

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

use retry decorator from bubus

Nick Sweeting48086d0cdd9d
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.py
index 872b302ed..d4a81164f 100644
--- a/browser_use/browser/session.py
+++ b/browser_use/browser/session.py
@@ -26,6 +26,7 @@ os.environ['PW_TEST_SCREENSHOT_NO_FONTS_READY'] = '1' # https://github.com/micr
import anyio
import psutil
+from bubus.helpers import retry
from playwright._impl._api_structures import ViewportSize
from pydantic import AliasChoices, BaseModel, ConfigDict, Field, InstanceOf, PrivateAttr, model_validator
from uuid_extensions import uuid7str
@@ -56,7 +57,6 @@ from browser_use.utils import (
is_new_tab_page,
match_url_with_domain_pattern,
merge_dicts,
- retry,
time_execution_async,
time_execution_sync,
)
diff --git a/browser_use/controller/service.py b/browser_use/controller/service.py
index dabb57ef8..bd4dfcfc9 100644
--- a/browser_use/controller/service.py
+++ b/browser_use/controller/service.py
@@ -11,6 +11,7 @@ try:
from lmnr import Laminar # type: ignore
except ImportError:
Laminar = None # type: ignore
+from bubus.helpers import retry
from pydantic import BaseModel
from browser_use.agent.views import ActionModel, ActionResult
@@ -505,11 +506,17 @@ Explain the content of the page and that the requested information is not availa
"""
page = await browser_session.get_current_page()
+ # Helper function to get window height with retry decorator
+ @retry(wait=1, retries=3, timeout=5)
+ async def get_window_height():
+ return await page.evaluate('() => window.innerHeight')
+
# Get window height with retries
- dy_result = await retry_async_function(
- lambda: page.evaluate('() => window.innerHeight'), 'Scroll failed due to an error.'
- )
- window_height = dy_result or 0
+ try:
+ window_height = await get_window_height()
+ except Exception as e:
+ raise RuntimeError(f'Scroll failed due to an error: {e}')
+ window_height = window_height or 0
# Determine scroll amount based on num_pages
scroll_amount = int(window_height * params.num_pages)
diff --git a/browser_use/logging_config.py b/browser_use/logging_config.py
index d0e8e73f6..043d6568b 100644
--- a/browser_use/logging_config.py
+++ b/browser_use/logging_config.py
@@ -136,6 +136,8 @@ def setup_logging(stream=None, log_level=None, force_setup=False):
'trafilatura.htmlprocessing',
'trafilatura',
'groq',
+ 'portalocker',
+ 'portalocker.utils',
]
for logger_name in third_party_loggers:
third_party = logging.getLogger(logger_name)
diff --git a/browser_use/utils.py b/browser_use/utils.py
index 59f3f3110..76e5e2f3f 100644
--- a/browser_use/utils.py
+++ b/browser_use/utils.py
@@ -3,27 +3,17 @@ import logging
import os
import platform
import signal
-import tempfile
-import threading
import time
from collections.abc import Callable, Coroutine
from fnmatch import fnmatch
from functools import cache, wraps
from pathlib import Path
from sys import stderr
-from typing import Any, Literal, ParamSpec, TypeVar
+from typing import Any, ParamSpec, TypeVar
from urllib.parse import urlparse
-import portalocker
from dotenv import load_dotenv
-try:
- import psutil
-
- PSUTIL_AVAILABLE = True
-except ImportError:
- PSUTIL_AVAILABLE = False
-
load_dotenv()
@@ -612,277 +602,3 @@ def _log_pretty_url(s: str, max_len: int | None = 22) -> str:
if max_len is not None and len(s) > max_len:
return s[:max_len] + '…'
return s
-
-
-# Global semaphore registry for retry decorator
-GLOBAL_RETRY_SEMAPHORES: dict[str, asyncio.Semaphore] = {}
-GLOBAL_RETRY_SEMAPHORE_LOCK = threading.Lock()
-
-# Multiprocess semaphore support
-MULTIPROCESS_SEMAPHORE_DIR = Path(tempfile.gettempdir()) / 'browser_use_semaphores'
-MULTIPROCESS_SEMAPHORE_DIR.mkdir(exist_ok=True)
-
-# Global multiprocess semaphore registry
-# Multiprocess semaphores are not cached due to internal state issues causing "Already locked" errors
-MULTIPROCESS_SEMAPHORE_LOCK = threading.Lock()
-
-# Global overload detection state
-_last_overload_check = 0.0
-_overload_check_interval = 5.0 # Check every 5 seconds
-_active_retry_operations = 0
-_active_operations_lock = threading.Lock()
-
-
-def _check_system_overload() -> tuple[bool, str]:
- """Check if system is overloaded and return (is_overloaded, reason)"""
- if not PSUTIL_AVAILABLE:
- return False, ''
-
- try:
- # Get system stats
- cpu_percent = psutil.cpu_percent(interval=0.1)
- memory = psutil.virtual_memory()
-
- # Check thresholds
- reasons = []
- is_overloaded = False
-
- if cpu_percent > 85:
- is_overloaded = True
- reasons.append(f'CPU: {cpu_percent:.1f}%')
-
- if memory.percent > 85:
- is_overloaded = True
- reasons.append(f'Memory: {memory.percent:.1f}%')
-
- # Check number of concurrent operations
- with _active_operations_lock:
- if _active_retry_operations > 30:
- is_overloaded = True
- reasons.append(f'Active operations: {_active_retry_operations}')
-
- return is_overloaded, ', '.join(reasons)
- except Exception:
- return False, ''
-
-
-def retry(
- wait: float = 3,
- retries: int = 3,
- timeout: float = 5,
- retry_on: tuple[type[Exception], ...] | None = None,
- backoff_factor: float = 1.0,
- semaphore_limit: int | None = None,
- semaphore_name: str | None = None,
- semaphore_lax: bool = True,
- semaphore_scope: Literal['global', 'class', 'self', 'multiprocess'] = 'global',
- semaphore_timeout: float | None = None,
-):
- """
- Retry decorator with semaphore support for async functions.
-
- Args:
- wait: Seconds to wait between retries
- retries: Number of retry attempts after initial failure
- timeout: Per-attempt timeout in seconds
- retry_on: Tuple of exception types to retry on (None = retry all exceptions)
- backoff_factor: Multiplier for wait time after each retry (1.0 = no backoff)
- semaphore_limit: Max concurrent executions (creates semaphore if needed)
- semaphore_name: Name for semaphore (defaults to function name)
- semaphore_lax: If True, continue without semaphore on acquisition failure
- semaphore_scope: Scope for semaphore sharing:
- - 'global': All calls share one semaphore (default)
- - 'class': All instances of a class share one semaphore
- - 'self': Each instance gets its own semaphore
- - 'multiprocess': All processes on the machine share one semaphore
- semaphore_timeout: Max time to wait for semaphore acquisition (None = timeout * (limit - 1))
-
- Example:
- @retry(wait=3, retries=3, timeout=5, semaphore_limit=3, semaphore_scope='self')
- async def some_function(self, ...):
- # Limited to 5s per attempt, retries up to 3 times on failure
- # Max 3 concurrent executions per instance
-
- Notes:
- - semaphore aquision happens once at start time, it's not retried
- - semaphore_timeout is only used if semaphore_limit is set.
- - if semaphore_timeout is set to 0, it will wait forever for a semaphore slot to become available.
- - if semaphore_timeout is set to None, it will wait for the default (timeout * (semaphore_limit - 1)) +0.01s
- - retries are 0-indexed, so retries=1 means the function will be called 2 times total (1 initial + 1 retry)
- """
-
- def decorator(func: Callable[P, Coroutine[Any, Any, T]]) -> Callable[P, Coroutine[Any, Any, T]]:
- @wraps(func)
- async def wrapper(*args: P.args, **kwargs: P.kwargs) -> T: # type: ignore[return]
- # Get or create semaphore if needed
- semaphore: Any = None
- semaphore_acquired = False
- multiprocess_lock: Any = None
-
- if semaphore_limit is not None:
- # Determine semaphore key based on scope
- base_name = semaphore_name or func.__name__
-
- if semaphore_scope == 'multiprocess':
- # Use multiprocess semaphore
- sem_key = base_name
- with MULTIPROCESS_SEMAPHORE_LOCK:
- # Don't cache multiprocess semaphores - they have internal state issues
- # Create a new instance each time to avoid "Already locked" errors
- semaphore = portalocker.utils.NamedBoundedSemaphore(
- maximum=semaphore_limit,
- name=sem_key,
- directory=str(MULTIPROCESS_SEMAPHORE_DIR),
- )
- else:
- # Use in-process semaphore
- if semaphore_scope == 'global':
- sem_key = base_name
- elif semaphore_scope == 'class' and args and hasattr(args[0], '__class__'):
- # Use class name for class-level semaphore
- class_name = args[0].__class__.__name__
- sem_key = f'{class_name}.{base_name}'
- elif semaphore_scope == 'self' and args:
- # Use instance id for instance-level semaphore
- instance_id = id(args[0])
- sem_key = f'{instance_id}.{base_name}'
- else:
- # Fallback to global if we can't determine scope
- sem_key = base_name
-
- with GLOBAL_RETRY_SEMAPHORE_LOCK:
- if sem_key not in GLOBAL_RETRY_SEMAPHORES:
- GLOBAL_RETRY_SEMAPHORES[sem_key] = asyncio.Semaphore(semaphore_limit)
- semaphore = GLOBAL_RETRY_SEMAPHORES[sem_key]
-
- # Try to acquire semaphore
- sem_start = time.time()
- # Calculate semaphore timeout
- if semaphore_timeout is None:
- # Default: wait time is if all other slots are occupied with max timeout operations
- # Ensure minimum of timeout value when limit=1
- sem_timeout = max(timeout, timeout * (semaphore_limit - 1))
- else:
- # Use provided timeout, but ensure minimum of 0.01 if 0 was passed
- sem_timeout = max(0.01, semaphore_timeout) if semaphore_timeout == 0 else semaphore_timeout
-
- if semaphore_scope == 'multiprocess':
- # Multiprocess semaphore uses context manager with timeout
- try:
- # Use a temporary thread to run the blocking operation
- # portalocker returns a Lock object or None
- multiprocess_lock = await asyncio.to_thread(
- lambda: semaphore.acquire(timeout=sem_timeout, check_interval=0.05, fail_when_locked=False)
- )
- if multiprocess_lock:
- semaphore_acquired = True
- else:
- raise TimeoutError(f'Could not acquire semaphore within {sem_timeout}s')
- except Exception as e:
- sem_wait_time = time.time() - sem_start
- if 'Could not acquire' in str(e) or isinstance(e, TimeoutError):
- if not semaphore_lax:
- raise TimeoutError(
- f'Failed to acquire multiprocess semaphore "{sem_key}" within {sem_timeout}s '
- f'(limit={semaphore_limit}, timeout={timeout}s per operation)'
- )
- logger.warning(
- f'Failed to acquire multiprocess semaphore "{sem_key}" after {sem_wait_time:.1f}s, '
- f'proceeding without concurrency limit'
- )
- else:
- raise
- else:
- # Regular asyncio semaphore
- try:
- async with asyncio.timeout(sem_timeout):
- await semaphore.acquire()
- semaphore_acquired = True
- except TimeoutError:
- sem_wait_time = time.time() - sem_start
- if not semaphore_lax:
- raise TimeoutError(
- f'Failed to acquire semaphore "{sem_key}" within {sem_timeout}s '
- f'(limit={semaphore_limit}, timeout={timeout}s per operation)'
- )
- logger.warning(
- f'Failed to acquire semaphore "{sem_key}" after {sem_wait_time:.1f}s, '
- f'proceeding without concurrency limit'
- )
-
- # Track active operations
- global _last_overload_check, _active_retry_operations
- with _active_operations_lock:
- _active_retry_operations += 1
-
- # Check for system overload (rate limited)
- current_time = time.time()
- if current_time - _last_overload_check > _overload_check_interval:
- _last_overload_check = current_time
- is_overloaded, reason = _check_system_overload()
- if is_overloaded:
- logger.warning(
- f'⚠️ System overload detected: {reason}. Consider reducing concurrent operations to prevent hanging.'
- )
-
- # Execute function with retries
- start_time = time.time()
- last_exception = None
-
- try:
- for attempt in range(retries + 1):
- try:
- # Execute with per-attempt timeout
- async with asyncio.timeout(timeout):
- return await func(*args, **kwargs)
-
- except Exception as e:
- # Check if we should retry this exception
- if retry_on is not None and not isinstance(e, retry_on):
- raise
-
- last_exception = e
-
- if attempt < retries:
- # Calculate wait time with backoff
- current_wait = wait * (backoff_factor**attempt)
-
- # Only log warning on the final retry attempt (second-to-last overall attempt)
- if attempt == retries - 1:
- logger.warning(
- f'{func.__name__} failed (attempt {attempt + 1}/{retries + 1}): '
- f'{type(e).__name__}: {e}. Waiting {current_wait:.1f}s before retry...'
- )
- # else:
- # # For earlier attempts, skip logging to reduce noise
- # logger.debug(
- # f'{func.__name__} failed (attempt {attempt + 1}/{retries + 1}): '
- # f'{type(e).__name__}: {e}. Waiting {current_wait:.1f}s before retry...'
- # )
- await asyncio.sleep(current_wait)
- else:
- # Final failure
- total_time = time.time() - start_time
- sem_wait = time.time() - sem_start - total_time if semaphore_limit else 0
-
- logger.error(
- f'{func.__name__} failed after {retries + 1} attempts over {total_time:.1f}s. '
- f'Semaphore wait: {sem_wait:.1f}s. Final error: {type(e).__name__}: {e}'
- )
- raise
-
- finally:
- # Decrement active operations counter
- with _active_operations_lock:
- _active_retry_operations = max(0, _active_retry_operations - 1)
-
- if semaphore_acquired and semaphore:
- if semaphore_scope == 'multiprocess' and multiprocess_lock:
- # Release the lock object for portalocker
- await asyncio.to_thread(lambda: multiprocess_lock.release())
- elif semaphore:
- semaphore.release()
-
- return wrapper
-
- return decorator
diff --git a/pyproject.toml b/pyproject.toml
index bd57db61f..b2621b1e2 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -13,7 +13,7 @@ classifiers = [
dependencies = [
"aiofiles>=24.1.0",
"anyio>=4.9.0",
- "bubus>=1.4.2",
+ "bubus>=1.4.3",
"google-api-core>=2.25.0",
"httpx>=0.28.1",
"markdownify==1.1.0",
diff --git a/tests/ci/test_semaphores.py b/tests/ci/test_semaphores.py
index 5170e2f01..4f28bea47 100644
--- a/tests/ci/test_semaphores.py
+++ b/tests/ci/test_semaphores.py
@@ -14,7 +14,7 @@ import pytest
# Add the browser-use directory to the path so we can import from it
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
-from browser_use.utils import retry
+from bubus.helpers import retry
def worker_acquire_semaphore(