Skip to content

Automate browser based workflows with AI

SKY-13105 Bound the page read that could hang an entire copilot turn (#7796)

Andrew Neilsonbe051ad10c74
diff --git a/skyvern/forge/sdk/copilot/tools/_shared.py b/skyvern/forge/sdk/copilot/tools/_shared.py
index 45ec4bc4e..f06970484 100644
--- a/skyvern/forge/sdk/copilot/tools/_shared.py
+++ b/skyvern/forge/sdk/copilot/tools/_shared.py
@@ -452,16 +452,33 @@ async def _fallback_page_info(ctx: AgentContext, session_id_override: str | None
session_id = session_id_override or ctx.browser_session_id
if not session_id:
return "", ""
- try:
+
+ # page.url is a synchronous property, so it is already in hand when the title stalls, and most
+ # callers here destructure the title away and want only the url.
+ url = ""
+
+ async def _read() -> str:
+ nonlocal url
browser_state = await resolve_browser_state_for_context(ctx, session_id=session_id)
if not browser_state:
- return "", ""
+ return ""
page = await browser_state.get_or_create_page()
- if page:
- return page.url, await page.title()
+ if not page:
+ return ""
+ url = page.url
+ return await page.title()
+
+ # page.title() waits on the renderer, so a wedged or busy page hangs here forever rather than
+ # raising — and every caller reaches this path, since a tool result's browser_context carries
+ # no url. Without the bound, one unreachable page deadlocks the whole turn.
+ try:
+ title = await asyncio.wait_for(_read(), timeout=_DISCOVERY_PER_CALL_TIMEOUT_SECONDS)
+ except asyncio.TimeoutError:
+ LOG.info("copilot page title read timed out", session_id=session_id, page_url=url)
+ return url, ""
except Exception:
- pass
- return "", ""
+ return url, ""
+ return url, title
def _composition_evidence_page_url(evidence: dict[str, Any] | None) -> str | None:
diff --git a/tests/unit/test_copilot_watchdog.py b/tests/unit/test_copilot_watchdog.py
index b69da63d5..ddeb300eb 100644
--- a/tests/unit/test_copilot_watchdog.py
+++ b/tests/unit/test_copilot_watchdog.py
@@ -26,6 +26,7 @@ Those three are where the SKY-9163 correctness properties live:
from __future__ import annotations
+import asyncio
from datetime import datetime, timezone
from types import SimpleNamespace
from typing import Any
@@ -274,6 +275,36 @@ async def test_fallback_page_info_uses_persistent_session_state_without_sdk_reco
)
+async def test_fallback_page_info_bounds_a_title_that_never_resolves_and_keeps_the_url(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """A wedged renderer hangs `title()` rather than raising it. The bound has to return, and it
+ has to keep the url — `page.url` is synchronous, so it is already in hand when the title
+ stalls, and most callers of this helper want only the url."""
+ from skyvern.forge import app as forge_app
+ from skyvern.forge.sdk.copilot.tools import _shared
+
+ async def _never_resolves() -> str:
+ await asyncio.Event().wait()
+ return "unreachable"
+
+ page = SimpleNamespace(url="https://example.test/wedged", title=_never_resolves)
+ browser_state = SimpleNamespace(get_or_create_page=AsyncMock(return_value=page))
+ session_manager = SimpleNamespace(get_browser_state=AsyncMock(return_value=browser_state))
+ monkeypatch.setattr(forge_app, "PERSISTENT_SESSIONS_MANAGER", session_manager)
+ monkeypatch.setattr(_shared, "_DISCOVERY_PER_CALL_TIMEOUT_SECONDS", 0.05)
+
+ ctx = SimpleNamespace(
+ organization_id="o_test", browser_session_id="pbs_copilot", turn_origin=TurnOrigin.interactive
+ )
+
+ current_url, page_title = await asyncio.wait_for(_fallback_page_info(ctx), timeout=5)
+
+ assert current_url == "https://example.test/wedged"
+ assert page_title == ""
+
+
@pytest.mark.asyncio
async def test_stagnation_error_message_does_not_invite_retry() -> None:
"""The exact SKY-9163 bug: the old copy said "likely stuck repeating