Skip to content

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

address PR review: raise default cap + tolerate bad env values

Saurav Panda1488a39b7f53
diff --git a/browser_use/tools/service.py b/browser_use/tools/service.py
index 83181c5f7..1f0d41aaa 100644
--- a/browser_use/tools/service.py
+++ b/browser_use/tools/service.py
@@ -80,8 +80,25 @@ T = TypeVar('T', bound=BaseModel)
# watchdog handler blocks on a dead CDP WebSocket, the action can hang past
# any agent-level watchdog. This cap ensures every action returns within a
# bounded window with an ActionResult(error=...) instead of hanging silently.
-# Override per-call via BROWSER_USE_ACTION_TIMEOUT_S env var or tools.act(action_timeout=...).
-_DEFAULT_ACTION_TIMEOUT_S = float(os.getenv('BROWSER_USE_ACTION_TIMEOUT_S', '90'))
+#
+# The default (180s) sits above the longest built-in inner timeout — the extract
+# action's page_extraction_llm.ainvoke at 120s — plus comfortable grace, so
+# slow-but-valid LLM-backed actions aren't truncated. Override per-call via
+# BROWSER_USE_ACTION_TIMEOUT_S env var or tools.act(action_timeout=...).
+_ACTION_TIMEOUT_FALLBACK_S = 180.0
+
+_env_timeout = os.getenv('BROWSER_USE_ACTION_TIMEOUT_S')
+try:
+ _DEFAULT_ACTION_TIMEOUT_S = float(_env_timeout) if _env_timeout else _ACTION_TIMEOUT_FALLBACK_S
+except ValueError:
+ # A bad env value should not prevent the module from importing (would break
+ # every tool call). Warn and fall back to the default.
+ logging.getLogger(__name__).warning(
+ 'Invalid BROWSER_USE_ACTION_TIMEOUT_S=%r; falling back to %.0fs',
+ _env_timeout,
+ _ACTION_TIMEOUT_FALLBACK_S,
+ )
+ _DEFAULT_ACTION_TIMEOUT_S = _ACTION_TIMEOUT_FALLBACK_S
def _detect_sensitive_key_name(text: str, sensitive_data: dict[str, str | dict[str, str]] | None) -> str | None:
@@ -2058,7 +2075,8 @@ Validated Code (after quote fixing):
action_timeout: per-action wall-clock cap (seconds). Prevents actions from hanging
indefinitely when a CDP WebSocket goes silent — a common failure mode with remote
browsers where internal CDP calls (tab switches, lifecycle waits) have no timeouts.
- Defaults to BROWSER_USE_ACTION_TIMEOUT_S env var or 90s.
+ Defaults to BROWSER_USE_ACTION_TIMEOUT_S env var or 180s (above the 120s
+ page_extraction_llm cap used by the `extract` action).
"""
timeout_s = action_timeout if action_timeout is not None else _DEFAULT_ACTION_TIMEOUT_S
diff --git a/tests/ci/test_action_timeout.py b/tests/ci/test_action_timeout.py
index 084b73d75..ca84cb87d 100644
--- a/tests/ci/test_action_timeout.py
+++ b/tests/ci/test_action_timeout.py
@@ -88,3 +88,40 @@ async def test_act_passes_through_fast_handler():
assert isinstance(result, ActionResult)
assert result.error is None
assert result.extracted_content == 'done'
+
+
+def test_default_action_timeout_accommodates_extract_action():
+ """The module-level default must sit above extract's 120s LLM inner cap."""
+ from browser_use.tools.service import _DEFAULT_ACTION_TIMEOUT_S
+
+ # extract action uses page_extraction_llm.ainvoke(..., timeout=120.0); the
+ # outer per-action cap must not truncate it.
+ assert _DEFAULT_ACTION_TIMEOUT_S >= 150.0, (
+ f'Default action cap ({_DEFAULT_ACTION_TIMEOUT_S}s) is below the 120s '
+ f'extract timeout + grace — slow but valid extractions would be killed.'
+ )
+
+
+def test_malformed_env_timeout_does_not_break_import(monkeypatch):
+ """Empty / non-numeric BROWSER_USE_ACTION_TIMEOUT_S must not crash import.
+
+ Env-templating tools sometimes produce empty strings; that turning into a
+ ValueError at module import would take out every tool call process-wide.
+ """
+ import importlib
+
+ import browser_use.tools.service as svc_module
+
+ for bad_value in ('', 'not-a-number', 'abc'):
+ monkeypatch.setenv('BROWSER_USE_ACTION_TIMEOUT_S', bad_value)
+ # Re-import cleanly; this would have raised ValueError before the fix.
+ reloaded = importlib.reload(svc_module)
+ # Fell back to the hardcoded default (180s) without raising.
+ assert reloaded._DEFAULT_ACTION_TIMEOUT_S == 180.0, (
+ f'Expected fallback 180.0 for bad env {bad_value!r}, got {reloaded._DEFAULT_ACTION_TIMEOUT_S}'
+ )
+
+ # Numeric values still work.
+ monkeypatch.setenv('BROWSER_USE_ACTION_TIMEOUT_S', '45')
+ reloaded = importlib.reload(svc_module)
+ assert reloaded._DEFAULT_ACTION_TIMEOUT_S == 45.0