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
review: reject non-finite timeouts + restore module after reload tests
Saurav Pandad2985dcab942
diff --git a/browser_use/tools/service.py b/browser_use/tools/service.pyindex 1f0d41aaa..74e89902e 100644--- a/browser_use/tools/service.py+++ b/browser_use/tools/service.py@@ -1,6 +1,7 @@import asyncioimport jsonimport logging+import mathimport osfrom typing import Generic, TypeVar@@ -87,18 +88,37 @@ T = TypeVar('T', bound=BaseModel)# 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 _parse_env_action_timeout(raw: str | None) -> float:+ """Parse BROWSER_USE_ACTION_TIMEOUT_S defensively.++ Accepts only finite positive values. Empty, non-numeric, inf, nan, or+ non-positive values fall back to the hardcoded default with a warning+ — these would otherwise make every action time out immediately (nan)+ or disable the hang guard entirely (inf / negative / zero).+ """+ if raw is None or raw == '':+ return _ACTION_TIMEOUT_FALLBACK_S+ try:+ parsed = float(raw)+ except ValueError:+ logging.getLogger(__name__).warning(+ 'Invalid BROWSER_USE_ACTION_TIMEOUT_S=%r; falling back to %.0fs',+ raw,+ _ACTION_TIMEOUT_FALLBACK_S,+ )+ return _ACTION_TIMEOUT_FALLBACK_S+ if not math.isfinite(parsed) or parsed <= 0:+ logging.getLogger(__name__).warning(+ 'BROWSER_USE_ACTION_TIMEOUT_S=%r is not a finite positive number; falling back to %.0fs',+ raw,+ _ACTION_TIMEOUT_FALLBACK_S,+ )+ return _ACTION_TIMEOUT_FALLBACK_S+ return parsed+++_DEFAULT_ACTION_TIMEOUT_S = _parse_env_action_timeout(os.getenv('BROWSER_USE_ACTION_TIMEOUT_S'))def _detect_sensitive_key_name(text: str, sensitive_data: dict[str, str | dict[str, str]] | None) -> str | None:diff --git a/tests/ci/test_action_timeout.py b/tests/ci/test_action_timeout.pyindex ca84cb87d..29f90f34e 100644--- a/tests/ci/test_action_timeout.py+++ b/tests/ci/test_action_timeout.py@@ -102,26 +102,45 @@ def test_default_action_timeout_accommodates_extract_action():)-def test_malformed_env_timeout_does_not_break_import(monkeypatch):- """Empty / non-numeric BROWSER_USE_ACTION_TIMEOUT_S must not crash import.+def _restore_service_module():+ """Reload browser_use.tools.service without any env override on teardown.- Env-templating tools sometimes produce empty strings; that turning into a- ValueError at module import would take out every tool call process-wide.+ Tests in this file intentionally reload the module with BROWSER_USE_ACTION_TIMEOUT_S+ set to various values; without this fixture, the last reload's default leaks into+ every later test in the same worker."""import importlib+ import osimport browser_use.tools.service as svc_module- for bad_value in ('', 'not-a-number', 'abc'):+ yield svc_module+ os.environ.pop('BROWSER_USE_ACTION_TIMEOUT_S', None)+ importlib.reload(svc_module)+++def test_malformed_env_timeout_does_not_break_import(monkeypatch, _restore_service_module):+ """Bad BROWSER_USE_ACTION_TIMEOUT_S values must fall back, not crash or misbehave.++ Covers three failure modes:+ - Non-numeric / empty (ValueError from float()): would crash module import.+ - NaN: parses fine but makes asyncio.wait_for time out immediately for every action.+ - Infinity / negative / zero: parses fine but effectively disables the hang guard.+ """+ import importlib++ svc_module = _restore_service_module++ bad_values = ('', 'not-a-number', 'abc', 'nan', 'NaN', 'inf', '-inf', '0', '-5')+ for bad_value in bad_values: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.+ # Valid finite positive values still take effect.monkeypatch.setenv('BROWSER_USE_ACTION_TIMEOUT_S', '45')reloaded = importlib.reload(svc_module)assert reloaded._DEFAULT_ACTION_TIMEOUT_S == 45.0