mirror of
https://github.com/browser-use/browser-use
synced 2026-04-22 17:45:09 +02:00
address cubic review: validate constructor timeout + exercise real send_raw
Two P2 comments from cubic on 9a09c4d7:
1. TimeoutWrappedCDPClient.__init__ trusted its cdp_request_timeout_s arg
blindly. nan / inf / <=0 would either make every CDP call time out
immediately (nan) or disable the guard (inf / <=0) — same defensive
gap we already fixed for the env-var path. Extracted _coerce_valid_
timeout() that mirrors _parse_env_cdp_timeout's validation; constructor
now routes through it, so both entry points are equally safe.
2. test_send_raw_times_out_on_silent_server used an inline copy of the
wrapper logic rather than the real TimeoutWrappedCDPClient.send_raw.
A regression in the production method — e.g. accidentally removing
the asyncio.wait_for — would not fail the test. Rewrote to:
- Construct via __new__ (skip CDPClient.__init__'s WebSocket setup)
- unittest.mock.patch the parent CDPClient.send_raw with a hanging
coroutine
- Call the real TimeoutWrappedCDPClient.send_raw, which does
super().send_raw(...) → our patched stub
- Assert it raises TimeoutError within the cap
Also added test_send_raw_passes_through_when_fast (fast-path regression
guard) and test_constructor_rejects_invalid_timeout (validation for
fix #1). All 14 tests in the timeout suite pass locally.
This commit is contained in:
@@ -68,6 +68,26 @@ def _parse_env_cdp_timeout(raw: str | None) -> float:
|
||||
DEFAULT_CDP_REQUEST_TIMEOUT_S: float = _parse_env_cdp_timeout(os.getenv('BROWSER_USE_CDP_TIMEOUT_S'))
|
||||
|
||||
|
||||
def _coerce_valid_timeout(value: float | None) -> float:
|
||||
"""Normalize a user-supplied timeout to a finite positive value.
|
||||
|
||||
None / nan / inf / non-positive values all fall back to the env-derived
|
||||
default with a warning. This mirrors _parse_env_cdp_timeout so callers that
|
||||
pass cdp_request_timeout_s directly get the same defensive behaviour as
|
||||
callers that set the env var.
|
||||
"""
|
||||
if value is None:
|
||||
return DEFAULT_CDP_REQUEST_TIMEOUT_S
|
||||
if not math.isfinite(value) or value <= 0:
|
||||
logger.warning(
|
||||
'cdp_request_timeout_s=%r is not a finite positive number; falling back to %.0fs',
|
||||
value,
|
||||
DEFAULT_CDP_REQUEST_TIMEOUT_S,
|
||||
)
|
||||
return DEFAULT_CDP_REQUEST_TIMEOUT_S
|
||||
return float(value)
|
||||
|
||||
|
||||
class TimeoutWrappedCDPClient(CDPClient):
|
||||
"""CDPClient subclass that enforces a per-request timeout on send_raw.
|
||||
|
||||
@@ -83,9 +103,7 @@ class TimeoutWrappedCDPClient(CDPClient):
|
||||
**kwargs: Any,
|
||||
) -> None:
|
||||
super().__init__(*args, **kwargs)
|
||||
self._cdp_request_timeout_s: float = (
|
||||
cdp_request_timeout_s if cdp_request_timeout_s is not None else DEFAULT_CDP_REQUEST_TIMEOUT_S
|
||||
)
|
||||
self._cdp_request_timeout_s: float = _coerce_valid_timeout(cdp_request_timeout_s)
|
||||
|
||||
async def send_raw(
|
||||
self,
|
||||
|
||||
Reference in New Issue
Block a user