mirror of
https://github.com/browser-use/browser-use
synced 2026-04-22 17:45:09 +02:00
fix(tools): enforce per-action timeout to prevent hung event handlers
Individual CDP calls like Page.navigate() have their own 20s timeouts, but the surrounding event-bus plumbing (await event, event_result()) does not. When a cloud browser's CDP WebSocket goes silent mid-session, agent handlers hang indefinitely — agents never emit a step, any outer watchdog eventually fires, and the run returns with zero history. Observed in practice: a 170k-task collector run produced 1,090 empty-history traces (21% of output). 100% hit the 240s outer watchdog; median 582s, max 2214s. Cloud HTTP layer was clean (all 200/201) — hang was entirely in CDP. Wrap registry.execute_action in asyncio.wait_for with a configurable per- action cap (default 90s, BROWSER_USE_ACTION_TIMEOUT_S env var or tools.act(action_timeout=...)). On timeout, the action returns ActionResult(error=...) so the agent can record the step and recover. New tests/ci/test_action_timeout.py covers both hung and fast handlers. Existing tools.act tests (test_multi_act_guards, test_action_blank_page) still pass.
This commit is contained in:
90
tests/ci/test_action_timeout.py
Normal file
90
tests/ci/test_action_timeout.py
Normal file
@@ -0,0 +1,90 @@
|
||||
"""Per-action timeout regression test.
|
||||
|
||||
When a CDP WebSocket goes silent (common failure mode with remote / cloud browsers),
|
||||
action handlers can await event-bus dispatches that never resolve — individual CDP
|
||||
calls like Page.navigate() have their own timeouts, but the surrounding event
|
||||
plumbing does not. Without a per-action cap, `tools.act()` hangs indefinitely and
|
||||
agents never emit a step, producing empty history traces.
|
||||
|
||||
This test replaces `registry.execute_action` with a coroutine that sleeps longer
|
||||
than the per-action cap, then asserts that `tools.act()` returns within the cap
|
||||
with an ActionResult(error=...) instead of hanging.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import time
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from browser_use.agent.views import ActionModel, ActionResult
|
||||
from browser_use.tools.service import Tools
|
||||
|
||||
|
||||
class _StubActionModel(ActionModel):
|
||||
"""ActionModel with two arbitrary named slots for tools.act() plumbing tests.
|
||||
|
||||
Tests target tools.act() behaviour (timeout wrapping, error handling), not any
|
||||
registered action — so we declare fixed slots here and stub out execute_action.
|
||||
"""
|
||||
|
||||
hung_action: dict[str, Any] | None = None
|
||||
fast_action: dict[str, Any] | None = None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_act_enforces_per_action_timeout_on_hung_handler():
|
||||
"""tools.act() must return within action_timeout even if the handler hangs."""
|
||||
tools = Tools()
|
||||
|
||||
# Replace the action executor with one that hangs far past the timeout.
|
||||
sleep_seconds = 30.0
|
||||
call_count = {'n': 0}
|
||||
|
||||
async def _hanging_execute_action(**_kwargs):
|
||||
call_count['n'] += 1
|
||||
await asyncio.sleep(sleep_seconds)
|
||||
return ActionResult(extracted_content='should never be reached')
|
||||
|
||||
tools.registry.execute_action = _hanging_execute_action # type: ignore[assignment]
|
||||
|
||||
# Build an ActionModel with a single slot — act() iterates model_dump(exclude_unset=True).
|
||||
action = _StubActionModel(hung_action={'url': 'https://example.com'})
|
||||
|
||||
# Use a tight timeout so the test runs in under a second.
|
||||
action_timeout = 0.5
|
||||
start = time.monotonic()
|
||||
result = await tools.act(action=action, browser_session=None, action_timeout=action_timeout) # type: ignore[arg-type]
|
||||
elapsed = time.monotonic() - start
|
||||
|
||||
# Handler got invoked exactly once.
|
||||
assert call_count['n'] == 1
|
||||
|
||||
# Returned well before the sleep would have finished.
|
||||
assert elapsed < sleep_seconds / 2, f'act() did not honor timeout; took {elapsed:.2f}s'
|
||||
# And returned close to the timeout itself (with a reasonable grace margin).
|
||||
assert elapsed < action_timeout + 2.0, f'act() overshot timeout; took {elapsed:.2f}s'
|
||||
|
||||
# Returned a proper ActionResult describing the timeout.
|
||||
assert isinstance(result, ActionResult)
|
||||
assert result.error is not None
|
||||
assert 'timed out' in result.error.lower()
|
||||
assert 'hung_action' in result.error
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_act_passes_through_fast_handler():
|
||||
"""When the handler finishes fast, act() returns its result unchanged."""
|
||||
tools = Tools()
|
||||
|
||||
async def _fast_execute_action(**_kwargs):
|
||||
return ActionResult(extracted_content='done')
|
||||
|
||||
tools.registry.execute_action = _fast_execute_action # type: ignore[assignment]
|
||||
|
||||
action = _StubActionModel(fast_action={'x': 1})
|
||||
result = await tools.act(action=action, browser_session=None, action_timeout=5.0) # type: ignore[arg-type]
|
||||
|
||||
assert isinstance(result, ActionResult)
|
||||
assert result.error is None
|
||||
assert result.extracted_content == 'done'
|
||||
Reference in New Issue
Block a user