Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix(SKY-13502): report a closed browser session as closed instead of high demand (#8043)
Aaron Perez3e4efaa2626d
diff --git a/skyvern/exceptions.py b/skyvern/exceptions.pyindex 85cf853f8..9b87a9360 100644--- a/skyvern/exceptions.py+++ b/skyvern/exceptions.py@@ -140,6 +140,13 @@ def _is_browser_connection_error(message: str) -> bool:return any(pattern in message for pattern in _BROWSER_CONNECTION_PATTERNS)+def _is_session_closed_error(message: str) -> bool:+ # The session router closes with (4410, "session closed") when the session was already closed;+ # match the reason text, not the bare code — 4410 is reused elsewhere with other reasons.+ # Message selection only: redaction routing must keep the broad _is_browser_connection_error net.+ return "session closed" in message.lower()++# A raw CDP connect failure (e.g. from playwright.chromium.connect_over_cdp) echoes the# endpoint URL, which can carry the remote-browser vendor host, a session-bearing path/query,# or credentials embedded as user:pass@host. The devtools socket is always ws/wss, so a ws/wss@@ -165,6 +172,12 @@ def get_user_facing_exception_message(exception: Exception) -> str:raw = str(exception)if _is_browser_connection_error(raw):+ if _is_session_closed_error(raw):+ return (+ "Failed to connect to the browser session because the session is already closed. "+ "Start a new browser session to continue. "+ "If this is unexpected, contact [email protected]."+ )return (f"Failed to connect to the browser session. "f"This is usually caused by high demand and is transient. {_BROWSER_CONNECTION_GUIDANCE}"diff --git a/tests/unit/test_exception_messages.py b/tests/unit/test_exception_messages.pyindex 36dd900b3..fac30f08e 100644--- a/tests/unit/test_exception_messages.py+++ b/tests/unit/test_exception_messages.py@@ -333,6 +333,7 @@ def test_browser_connection_error_connect_over_cdp_websocket() -> None:assert "Call log" not in messageassert "WebSocket" not in messageassert "Failed to connect to the browser session" in message+ assert "high demand" in messageassert "try re-running" in message@@ -344,6 +345,55 @@ def test_browser_connection_error_websocket_closed() -> None:assert "try re-running" in message+def test_session_closed_error_does_not_claim_high_demand() -> None:+ # SKY-13502: a session-router lifecycle close (4410 "session closed") means the session was+ # already closed before the connect — "high demand" asserts a cause that was never+ # established, and retry advice cannot help a closed session.+ raw_error = (+ "BrowserType.connect_over_cdp: Target page, context or browser has been closed "+ "Call log: - <ws connecting> wss://sessions.skyvern.com/pbs_000000000000000000 "+ "- <ws error> error WebSocket was closed before the connection code=4410 reason=session closed"+ )+ message = get_user_facing_exception_message(Exception(raw_error))+ assert "high demand" not in message+ assert "re-run" not in message+ assert "already closed" in message+ assert "sessions.skyvern.com" not in message+ assert "wss://" not in message+++def test_session_closed_requires_reason_text_not_bare_close_code() -> None:+ # Close code 4410 is reused for other outcomes (e.g. the live-view stream closes with+ # reason "no_working_page"), so the bare code must not trigger the session-closed story.+ raw_error = (+ "BrowserType.connect_over_cdp: WebSocket error: wss://sessions.skyvern.com/pbs_000000000000000000 "+ "- <ws error> error WebSocket was closed before the connection code=4410 reason=no_working_page"+ )+ message = get_user_facing_exception_message(Exception(raw_error))+ assert "already closed" not in message+ assert "Failed to connect to the browser session" in message+++def test_unknown_error_still_redacts_cdp_endpoints_for_session_closed_error() -> None:+ # SKY-13502 split _is_browser_connection_error's consumers: the narrow session-closed test+ # picks the message, but redaction routing must keep the broad net — a session-closed CDP+ # failure still echoes endpoint URLs carrying host, session tokens, or credentials.+ inner_exception = Exception(+ "browserType.connectOverCDP: WebSocket error: "+ "wss://user:[email protected]/session/tok-9f3a?apiKey=SEKRET "+ "fetching https://remote.example.internal/json/version?token=SEKRET failed "+ "WebSocket was closed before the connection code=4410 reason=session closed"+ )+ message = str(UnknownErrorWhileCreatingBrowserContext("dynamic-browser", inner_exception))++ assert "wss://" not in message+ assert "https://" not in message+ assert "SEKRET" not in message+ assert "tok-9f3a" not in message+ assert "remote.example.internal" not in message+ assert "[remote browser endpoint]" in message++def test_non_browser_error_not_intercepted() -> None:"""Regular errors should still pass through as-is."""message = get_user_facing_exception_message(ValueError("some other error"))diff --git a/tests/unit_tests/test_streaming_screencast.py b/tests/unit_tests/test_streaming_screencast.pyindex 6f220a2cb..c78ec613a 100644--- a/tests/unit_tests/test_streaming_screencast.py+++ b/tests/unit_tests/test_streaming_screencast.py@@ -124,4 +124,3 @@ async def test_wait_for_browser_state_returns_none_on_timeout(monkeypatch: pytesassert result is Noneassert resolve_mock.await_count == 3assert browser_state.get_working_page.await_count == 3- assert sleep_mock.await_count == 3