Skip to content

Automate browser based workflows with AI

Fix extension bridge reconnect wedge and CDP child-target hang (SKY-13562) (#8075)

Suchintan6e237f261049
diff --git a/skyvern/browser_extension/cdp_adapter.py b/skyvern/browser_extension/cdp_adapter.py
index 5ab18a801..1b8ab773b 100644
--- a/skyvern/browser_extension/cdp_adapter.py
+++ b/skyvern/browser_extension/cdp_adapter.py
@@ -4,6 +4,7 @@ import asyncio
import json
import secrets
from collections.abc import Coroutine
+from functools import partial
from typing import TYPE_CHECKING
import structlog
@@ -40,6 +41,20 @@ _CHILD_AUTO_ATTACH_PARAMS = {
"flatten": True,
"autoAttach": True,
"waitForDebuggerOnStart": False,
+ "filter": [{"type": "iframe", "exclude": False}],
+}
+_UNSUPPORTED_CHILD_TARGET_TYPES = {"service_worker", "shared_worker", "worker"}
+_CHILD_AUTO_ATTACH_TIMEOUT_SECONDS = 3.0
+_CHILD_DETACH_TIMEOUT_SECONDS = 2.0
+_ROOT_TARGET_GATE_METHODS = {
+ "Browser.close",
+ "Target.activateTarget",
+ "Target.attachToTarget",
+ "Target.closeTarget",
+ "Target.createTarget",
+ "Target.getTargetInfo",
+ "Target.getTargets",
+ "Target.setDiscoverTargets",
}
@@ -54,6 +69,7 @@ class ExtensionCdpAdapter:
self._client_ws: web.WebSocketResponse | None = None
self._client_guard = asyncio.Lock()
self._send_lock = asyncio.Lock()
+ self._root_target_setup_lock = asyncio.Lock()
self._auto_attach = False
self._discover_targets = False
self._attached_tabs: set[int] = set()
@@ -62,6 +78,10 @@ class ExtensionCdpAdapter:
self._scope_generations: dict[int, int] = {}
self._scope_tombstones: set[int] = set()
self._background_tasks: set[asyncio.Task[None]] = set()
+ self._client_tasks: dict[web.WebSocketResponse, set[asyncio.Task[None]]] = {}
+ self._closing_client_websockets: set[web.WebSocketResponse] = set()
+ self._pending_child_sessions: set[str] = set()
+ self._pending_child_events: dict[str, list[dict]] = {}
async def start(self) -> None:
if self._runner is not None:
@@ -171,15 +191,16 @@ class ExtensionCdpAdapter:
try:
async for message in ws:
if message.type == WSMsgType.TEXT:
- await self._handle_client_text(ws, message.data)
+ self._spawn_client_task(ws, self._handle_client_text(ws, message.data))
elif message.type == WSMsgType.ERROR:
break
elif message.type == WSMsgType.BINARY:
await ws.close(code=1003, message=b"text frames required")
break
finally:
+ await self._cancel_client_tasks(ws)
async with self._client_guard:
- if self._client_ws is ws:
+ if self._client_ws is ws and ws not in self._closing_client_websockets:
self._client_ws = None
self._reset_connection_state()
return ws
@@ -233,7 +254,10 @@ class ExtensionCdpAdapter:
return
if not is_cdp_method_allowed(method, params):
raise ExtensionRequestError("CDP_METHOD_NOT_ALLOWED", "The requested CDP method is not allowed.")
- args = {"tabId": tab_id, "method": method, "params": params}
+ relay_params = params
+ if method == "Target.setAutoAttach" and params.get("autoAttach") is True:
+ relay_params = {**params, "filter": [{"type": "iframe", "exclude": False}]}
+ args = {"tabId": tab_id, "method": method, "params": relay_params}
if chrome_session_id is not None:
args["sessionId"] = chrome_session_id
relay_result = await self._relay.request("debugger.send", args)
@@ -250,18 +274,24 @@ class ExtensionCdpAdapter:
params: dict,
response_session_id: str | None = None,
) -> None:
+ if method == "Target.setAutoAttach":
+ async with self._root_target_setup_lock:
+ await self._set_auto_attach(ws, request_id, params, response_session_id)
+ return
+ if method in _ROOT_TARGET_GATE_METHODS:
+ # Auto-attach replaces temporary tab-<id> targets with main-frame ids.
+ # Wait for that setup without serializing these commands afterward.
+ async with self._root_target_setup_lock:
+ pass
+
if method == "Browser.getVersion":
await self._reply(ws, request_id, dict(_VERSION_RESULT), response_session_id)
elif method == "Browser.setDownloadBehavior":
await self._reply(ws, request_id, {}, response_session_id)
elif method == "Browser.close":
await self._reply(ws, request_id, {}, response_session_id)
- await self._detach_all_tabs()
- async with self._client_guard:
- if self._client_ws is ws:
- self._client_ws = None
- self._reset_connection_state()
- await ws.close()
+ self._closing_client_websockets.add(ws)
+ self._spawn(self._shutdown_client(ws))
elif method == "Browser.getWindowForTarget":
await self._reply(
ws,
@@ -273,8 +303,6 @@ class ExtensionCdpAdapter:
await self._reply(ws, request_id, {}, response_session_id)
elif method == "Browser.getWindowBounds":
await self._reply(ws, request_id, {"bounds": dict(_WINDOW_BOUNDS)}, response_session_id)
- elif method == "Target.setAutoAttach":
- await self._set_auto_attach(ws, request_id, params, response_session_id)
elif method == "Target.setDiscoverTargets":
await self._set_discover_targets(ws, request_id, params, response_session_id)
elif method == "Target.getTargets":
@@ -531,15 +559,23 @@ class ExtensionCdpAdapter:
self._forget_tab(tab_id)
await self._reply(ws, request_id, {}, response_session_id)
- async def _handle_debugger_event(self, payload: dict) -> None:
+ async def _handle_debugger_event(self, payload: dict, replaying_pending_event: bool = False) -> None:
tab_id = payload.get("tabId")
method = payload.get("method")
event_params = payload.get("params")
if type(tab_id) is not int or not isinstance(method, str) or not isinstance(event_params, dict):
return
+ payload_session_id = payload.get("sessionId")
+ if (
+ isinstance(payload_session_id, str)
+ and payload_session_id in self._pending_child_sessions
+ and not replaying_pending_event
+ ):
+ self._pending_child_events.setdefault(payload_session_id, []).append(payload)
+ return
try:
- if isinstance(payload.get("sessionId"), str):
- outer_session_ids = [payload["sessionId"]]
+ if isinstance(payload_session_id, str):
+ outer_session_ids = [payload_session_id]
self._registry.resolve_session(outer_session_ids[0])
else:
outer_session_ids = self._registry.root_session_ids(tab_id)
@@ -557,16 +593,38 @@ class ExtensionCdpAdapter:
# never surfaces that, and forwarding it duplicates the tab target.
if target_info.get("type") == "page":
return
- self._registry.register_child_session(tab_id, child_session_id, target_info)
- await self._emit_to_sessions(method, event_params, outer_session_ids)
+ if target_info.get("type") in _UNSUPPORTED_CHILD_TARGET_TYPES:
+ if child_session_id in self._pending_child_sessions:
+ return
+ self._pending_child_sessions.add(child_session_id)
+ self._spawn(self._discard_unsupported_child(tab_id, child_session_id, target_info))
+ await asyncio.sleep(0)
+ return
if self._auto_attach:
- self._spawn(self._set_child_auto_attach(tab_id, child_session_id))
+ if child_session_id in self._pending_child_sessions:
+ return
+ self._pending_child_sessions.add(child_session_id)
+ self._spawn(
+ self._initialize_child_target(
+ tab_id,
+ child_session_id,
+ target_info,
+ event_params,
+ outer_session_ids,
+ )
+ )
await asyncio.sleep(0)
+ else:
+ self._registry.register_child_session(tab_id, child_session_id, target_info)
+ await self._emit_to_sessions(method, event_params, outer_session_ids)
return
elif method == "Target.detachedFromTarget":
child_session_id = event_params.get("sessionId")
if not isinstance(child_session_id, str):
return
+ if child_session_id in self._pending_child_sessions:
+ self._pending_child_sessions.discard(child_session_id)
+ self._spawn(self._discard_buffered_child_events(tab_id, child_session_id))
try:
self._registry.resolve_session(child_session_id)
except KeyError:
@@ -576,7 +634,14 @@ class ExtensionCdpAdapter:
return
await self._emit_to_sessions(method, event_params, outer_session_ids)
- async def _set_child_auto_attach(self, tab_id: int, child_session_id: str) -> None:
+ async def _initialize_child_target(
+ self,
+ tab_id: int,
+ child_session_id: str,
+ target_info: dict,
+ event_params: dict,
+ outer_session_ids: list[str],
+ ) -> None:
try:
await self._relay.request(
"debugger.send",
@@ -586,12 +651,116 @@ class ExtensionCdpAdapter:
"method": "Target.setAutoAttach",
"params": dict(_CHILD_AUTO_ATTACH_PARAMS),
},
+ timeout=_CHILD_AUTO_ATTACH_TIMEOUT_SECONDS,
)
- except ExtensionRequestError as exc:
+ except Exception as exc:
LOG.debug(
"browser_extension_child_auto_attach_failed",
method="Target.setAutoAttach",
- error_code=exc.code,
+ error_code=exc.code if isinstance(exc, ExtensionRequestError) else "INTERNAL",
+ target_type=target_info.get("type"),
+ )
+ try:
+ await self._discard_buffered_child_events(tab_id, child_session_id)
+ await self._resume_and_detach_unserviceable_child(tab_id, child_session_id)
+ finally:
+ self._pending_child_sessions.discard(child_session_id)
+ return
+
+ if child_session_id not in self._pending_child_sessions or not self._auto_attach:
+ await self._discard_buffered_child_events(tab_id, child_session_id)
+ self._pending_child_sessions.discard(child_session_id)
+ return
+ live_outer_session_ids = []
+ for outer_session_id in outer_session_ids:
+ try:
+ self._registry.resolve_session(outer_session_id)
+ except KeyError:
+ continue
+ live_outer_session_ids.append(outer_session_id)
+ if not live_outer_session_ids:
+ try:
+ await self._discard_buffered_child_events(tab_id, child_session_id)
+ await self._resume_and_detach_unserviceable_child(tab_id, child_session_id)
+ finally:
+ self._pending_child_sessions.discard(child_session_id)
+ return
+ self._registry.register_child_session(tab_id, child_session_id, target_info)
+ await self._emit_to_sessions("Target.attachedToTarget", event_params, live_outer_session_ids)
+ await self._replay_buffered_child_events(child_session_id)
+ self._pending_child_sessions.discard(child_session_id)
+
+ async def _replay_buffered_child_events(self, child_session_id: str) -> None:
+ events = self._pending_child_events.get(child_session_id, [])
+ event_index = 0
+ while event_index < len(events):
+ payload = events[event_index]
+ event_index += 1
+ await self._handle_debugger_event(payload, replaying_pending_event=True)
+ self._pending_child_events.pop(child_session_id, None)
+
+ async def _discard_buffered_child_events(self, tab_id: int, child_session_id: str) -> None:
+ events = self._pending_child_events.pop(child_session_id, [])
+ buffered_child_session_ids: set[str] = set()
+ for payload in events:
+ if payload.get("method") != "Target.attachedToTarget":
+ continue
+ event_params = payload.get("params")
+ if not isinstance(event_params, dict):
+ continue
+ buffered_child_session_id = event_params.get("sessionId")
+ if not isinstance(buffered_child_session_id, str):
+ continue
+ buffered_child_session_ids.add(buffered_child_session_id)
+ for buffered_child_session_id in buffered_child_session_ids:
+ await self._discard_buffered_child_events(tab_id, buffered_child_session_id)
+ self._pending_child_sessions.discard(buffered_child_session_id)
+ await self._resume_and_detach_unserviceable_child(tab_id, buffered_child_session_id)
+
+ async def _discard_unsupported_child(self, tab_id: int, child_session_id: str, target_info: dict) -> None:
+ LOG.debug(
+ "browser_extension_child_target_skipped",
+ target_type=target_info.get("type"),
+ )
+ try:
+ await self._discard_buffered_child_events(tab_id, child_session_id)
+ await self._resume_and_detach_unserviceable_child(tab_id, child_session_id)
+ finally:
+ self._pending_child_sessions.discard(child_session_id)
+
+ async def _resume_and_detach_unserviceable_child(self, tab_id: int, child_session_id: str) -> None:
+ try:
+ await self._relay.request(
+ "debugger.send",
+ {
+ "tabId": tab_id,
+ "sessionId": child_session_id,
+ "method": "Runtime.runIfWaitingForDebugger",
+ "params": {},
+ },
+ timeout=_CHILD_DETACH_TIMEOUT_SECONDS,
+ )
+ except Exception as exc:
+ LOG.debug(
+ "browser_extension_child_resume_failed",
+ method="Runtime.runIfWaitingForDebugger",
+ error_type=type(exc).__name__,
+ )
+ try:
+ await self._relay.request(
+ "debugger.send",
+ {
+ "tabId": tab_id,
+ "method": "Target.detachFromTarget",
+ "params": {"sessionId": child_session_id},
+ },
+ timeout=_CHILD_DETACH_TIMEOUT_SECONDS,
+ )
+ except Exception as exc:
+ LOG.debug(
+ "browser_extension_child_detach_failed",
+ method="Target.detachFromTarget",
+ error_type=type(exc).__name__,
)
async def _handle_hello_tabs(self, tabs: list[tuple[dict, int]]) -> None:
@@ -773,6 +942,15 @@ class ExtensionCdpAdapter:
"browser_extension_debugger_detach_failed", method="debugger.detach", error_type=type(exc).__name__
)
+ async def _shutdown_client(self, ws: web.WebSocketResponse) -> None:
+ await self._detach_all_tabs()
+ async with self._client_guard:
+ if self._client_ws is ws:
+ self._client_ws = None
+ self._closing_client_websockets.discard(ws)
+ self._reset_connection_state()
+ await ws.close()
+
def _forget_tab(self, tab_id: int) -> None:
self._attached_tabs.discard(tab_id)
self._opener_ids.pop(tab_id, None)
@@ -802,8 +980,10 @@ class ExtensionCdpAdapter:
return self._scope_generations.get(tab_id) == generation and tab_id not in self._scope_tombstones
def _reset_connection_state(self) -> None:
+ current_task = asyncio.current_task()
for task in self._background_tasks:
- task.cancel()
+ if task is not current_task:
+ task.cancel()
self._auto_attach = False
self._discover_targets = False
self._attached_tabs.clear()
@@ -811,6 +991,9 @@ class ExtensionCdpAdapter:
self._opener_ids.clear()
self._scope_generations.clear()
self._scope_tombstones.clear()
+ self._closing_client_websockets.clear()
+ self._pending_child_sessions.clear()
+ self._pending_child_events.clear()
self._registry.clear()
def _spawn(self, coroutine: Coroutine[object, object, None]) -> None:
@@ -818,6 +1001,30 @@ class ExtensionCdpAdapter:
self._background_tasks.add(task)
task.add_done_callback(self._background_task_done)
+ def _spawn_client_task(self, ws: web.WebSocketResponse, coroutine: Coroutine[object, object, None]) -> None:
+ task = asyncio.create_task(coroutine)
+ self._client_tasks.setdefault(ws, set()).add(task)
+ task.add_done_callback(partial(self._client_task_done, ws))
+
+ async def _cancel_client_tasks(self, ws: web.WebSocketResponse) -> None:
+ tasks = list(self._client_tasks.pop(ws, set()))
+ for task in tasks:
+ task.cancel()
+ if tasks:
+ await asyncio.gather(*tasks, return_exceptions=True)
+
+ def _client_task_done(self, ws: web.WebSocketResponse, task: asyncio.Task[None]) -> None:
+ tasks = self._client_tasks.get(ws)
+ if tasks is not None:
+ tasks.discard(task)
+ if not tasks:
+ self._client_tasks.pop(ws, None)
+ if task.cancelled():
+ return
+ error = task.exception()
+ if error is not None:
+ LOG.debug("browser_extension_client_task_failed", error_type=type(error).__name__)
+
def _background_task_done(self, task: asyncio.Task[None]) -> None:
self._background_tasks.discard(task)
if task.cancelled():
diff --git a/skyvern/browser_extension/extension/bridge_connection.js b/skyvern/browser_extension/extension/bridge_connection.js
index 2ea2e8086..7aaaaeef3 100644
--- a/skyvern/browser_extension/extension/bridge_connection.js
+++ b/skyvern/browser_extension/extension/bridge_connection.js
@@ -10,6 +10,7 @@ import {
const PING_INTERVAL_MS = 20_000;
const SILENCE_TIMEOUT_MS = 45_000;
+const CONNECT_TIMEOUT_MS = 10_000;
const RECONNECT_MAX_MS = 30_000;
const AUTH_EXT_CONTEXT = "skyvern-ext-v1|";
const AUTH_SERVER_CONTEXT = "skyvern-srv-v1|";
@@ -88,6 +89,7 @@ export class BridgeConnection {
this.lastMessageAt = 0;
this.reconnectDelayMs = 1_000;
this.reconnectTimer = null;
+ this.connectTimer = null;
this.pingTimer = null;
this.silenceTimer = null;
this.connectionGeneration = 0;
@@ -193,9 +195,19 @@ export class BridgeConnection {
this.serverNonce = null;
this.hmacKey = null;
this.notifyState();
+ this.connectTimer = setTimeout(() => {
+ if (generation !== this.connectionGeneration || this.socket !== socket) {
+ return;
+ }
+ this.connectTimer = null;
+ if (socket.readyState === WebSocket.CONNECTING) {
+ this.failConnection("Could not reach the local Skyvern bridge.");
+ }
+ }, CONNECT_TIMEOUT_MS);
socket.addEventListener("open", () => {
if (generation === this.connectionGeneration) {
+ this.clearConnectTimer();
this.lastMessageAt = Date.now();
this.startHealthTimers();
}
@@ -207,8 +219,7 @@ export class BridgeConnection {
});
socket.addEventListener("error", () => {
if (generation === this.connectionGeneration) {
- this.lastError = "Could not reach the local Skyvern bridge.";
- this.notifyState();
+ this.failConnection("Could not reach the local Skyvern bridge.");
}
});
socket.addEventListener("close", () => {
@@ -217,6 +228,7 @@ export class BridgeConnection {
}
this.socket = null;
this.authenticated = false;
+ this.clearConnectTimer();
this.stopHealthTimers();
this.notifyState();
this.scheduleReconnect();
@@ -435,26 +447,30 @@ export class BridgeConnection {
}
}
- closeSocket() {
+ clearConnectTimer() {
+ if (this.connectTimer !== null) {
+ clearTimeout(this.connectTimer);
+ this.connectTimer = null;
+ }
+ }
+
+ closeSocket(code = 1000, reason = "Disconnected") {
this.connectionGeneration += 1;
const socket = this.socket;
this.socket = null;
this.authenticated = false;
+ this.clearConnectTimer();
this.stopHealthTimers();
if (socket !== null && socket.readyState < WebSocket.CLOSING) {
- socket.close(1000, "Disconnected");
+ socket.close(code, reason);
}
this.notifyState();
}
failConnection(message, code = 4400) {
this.lastError = message;
- this.authenticated = false;
- const socket = this.socket;
- if (socket !== null && socket.readyState < WebSocket.CLOSING) {
- socket.close(code, message.slice(0, 120));
- }
- this.notifyState();
+ this.closeSocket(code, message.slice(0, 120));
+ this.scheduleReconnect();
}
notifyState() {
diff --git a/skyvern/browser_extension/extension/debugger_router.js b/skyvern/browser_extension/extension/debugger_router.js
index 8ce4340cd..8b0a35d2b 100644
--- a/skyvern/browser_extension/extension/debugger_router.js
+++ b/skyvern/browser_extension/extension/debugger_router.js
@@ -8,12 +8,20 @@ import {
requireTabId,
} from "./protocol.js";
+const CHILD_AUTO_ATTACH_TIMEOUT_MS = 2_000;
+const LEAF_TARGET_TYPES = new Set([
+ "service_worker",
+ "shared_worker",
+ "worker",
+]);
+
export class DebuggerRouter {
constructor({ tabScope, sendEvent, onAttachedChange }) {
this.tabScope = tabScope;
this.sendEvent = sendEvent;
this.onAttachedChange = onAttachedChange;
this.attachedTabs = new Set();
+ this.childTargets = new Map();
chrome.debugger.onEvent.addListener((source, method, params) => {
void this.handleDebuggerEvent(source, method, params);
@@ -71,6 +79,7 @@ export class DebuggerRouter {
);
}
this.attachedTabs.delete(tabId);
+ this.forgetChildTargets(tabId);
this.onAttachedChange();
return {};
});
@@ -79,7 +88,10 @@ export class DebuggerRouter {
async send(args) {
const values = requireArgs(args);
const tabId = requireTabId(values.tabId);
- return this.tabScope.runTabOperation(tabId, async () => {
+ let commandPromise;
+ let commandError;
+ let skippedLeafAutoAttach = false;
+ await this.tabScope.runTabOperation(tabId, async () => {
await this.ensureStillControllableLocked(tabId);
if (!this.attachedTabs.has(tabId)) {
throw new ProtocolError(
@@ -116,30 +128,87 @@ export class DebuggerRouter {
"CDP parameters must be an object.",
);
}
+ const childTarget = this.childTargets.get(values.sessionId);
+ if (
+ values.method === "Target.setAutoAttach" &&
+ childTarget?.tabId === tabId &&
+ LEAF_TARGET_TYPES.has(childTarget?.type)
+ ) {
+ skippedLeafAutoAttach = true;
+ return;
+ }
const target =
values.sessionId === undefined
? { tabId }
: { tabId, sessionId: values.sessionId };
- let result;
+ // Initiate under the per-tab chain, then release it before awaiting the response.
try {
- result = await chrome.debugger.sendCommand(
- target,
- values.method,
- values.params ?? {},
- );
- } catch {
- await this.ensureStillControllableLocked(tabId);
+ commandPromise =
+ values.method === "Target.setAutoAttach" &&
+ values.sessionId !== undefined
+ ? this.sendCommandWithTimeout(
+ target,
+ values.method,
+ values.params ?? {},
+ CHILD_AUTO_ATTACH_TIMEOUT_MS,
+ )
+ : chrome.debugger.sendCommand(
+ target,
+ values.method,
+ values.params ?? {},
+ );
+ } catch (error) {
+ commandError = error;
+ }
+ });
+ if (skippedLeafAutoAttach) {
+ return { result: {} };
+ }
+
+ let result;
+ try {
+ if (commandError !== undefined) {
+ throw commandError;
+ }
+ result = await commandPromise;
+ } catch {
+ await this.assertCanSend(tabId);
+ throw new ProtocolError(
+ ERROR_CODES.CDP_ERROR,
+ "Chrome rejected the CDP command.",
+ );
+ }
+ await this.assertCanSend(tabId);
+ return { result: result ?? {} };
+ }
+
+ async assertCanSend(tabId) {
+ return this.tabScope.runTabOperation(tabId, async () => {
+ await this.ensureStillControllableLocked(tabId);
+ if (!this.attachedTabs.has(tabId)) {
throw new ProtocolError(
- ERROR_CODES.CDP_ERROR,
- "Chrome rejected the CDP command.",
+ ERROR_CODES.DEBUGGER_DETACHED,
+ "The debugger is not attached to this tab.",
);
}
- await this.ensureStillControllableLocked(tabId);
- return { result: result ?? {} };
});
}
+ async sendCommandWithTimeout(target, method, params, timeoutMs) {
+ let timeoutId;
+ try {
+ return await Promise.race([
+ chrome.debugger.sendCommand(target, method, params),
+ new Promise((_, reject) => {
+ timeoutId = setTimeout(() => reject(new Error("timeout")), timeoutMs);
+ }),
+ ]);
+ } finally {
+ clearTimeout(timeoutId);
+ }
+ }
+
async detachIfAttached(tabId) {
return this.tabScope.runTabOperation(tabId, () =>
this.detachIfAttachedLocked(tabId),
@@ -154,10 +223,12 @@ export class DebuggerRouter {
await chrome.debugger.detach({ tabId });
} catch {
this.attachedTabs.delete(tabId);
+ this.forgetChildTargets(tabId);
this.onAttachedChange();
return;
}
this.attachedTabs.delete(tabId);
+ this.forgetChildTargets(tabId);
this.onAttachedChange();
}
@@ -197,6 +268,23 @@ export class DebuggerRouter {
if (typeof source.sessionId === "string") {
eventParams.sessionId = source.sessionId;
}
+ const childSessionId = params?.sessionId;
+ if (
+ method === "Target.attachedToTarget" &&
+ typeof childSessionId === "string" &&
+ params.targetInfo !== null &&
+ typeof params.targetInfo === "object"
+ ) {
+ this.childTargets.set(childSessionId, {
+ tabId: source.tabId,
+ type: params.targetInfo.type,
+ });
+ } else if (
+ method === "Target.detachedFromTarget" &&
+ typeof childSessionId === "string"
+ ) {
+ this.childTargets.delete(childSessionId);
+ }
this.sendEvent(EVENTS.DEBUGGER_EVENT, eventParams);
}
@@ -206,6 +294,7 @@ export class DebuggerRouter {
}
await this.tabScope.runTabOperation(source.tabId, async () => {
this.attachedTabs.delete(source.tabId);
+ this.forgetChildTargets(source.tabId);
this.onAttachedChange();
this.sendEvent(EVENTS.DEBUGGER_DETACHED, {
tabId: source.tabId,
@@ -214,4 +303,12 @@ export class DebuggerRouter {
await this.tabScope.handleDebuggerDetachLocked(source.tabId);
});
}
+
+ forgetChildTargets(tabId) {
+ for (const [sessionId, target] of this.childTargets) {
+ if (target.tabId === tabId) {
+ this.childTargets.delete(sessionId);
+ }
+ }
+ }
}
diff --git a/skyvern/browser_extension/extension/tab_scope.js b/skyvern/browser_extension/extension/tab_scope.js
index ac8677802..2723040d2 100644
--- a/skyvern/browser_extension/extension/tab_scope.js
+++ b/skyvern/browser_extension/extension/tab_scope.js
@@ -385,14 +385,14 @@ export class TabScope {
const scopedGroupId = this.scopedGroupIds.get(tabId);
this.scopedGroupIds.delete(tabId);
try {
- await this.persistScope();
+ if (detach && this.debuggerRouter !== null) {
+ await this.debuggerRouter.detachIfAttachedLocked(tabId);
+ }
} finally {
- await this.ungroupTabLocked(tabId, scopedGroupId);
try {
- if (detach && this.debuggerRouter !== null) {
- await this.debuggerRouter.detachIfAttachedLocked(tabId);
- }
+ await this.persistScope();
} finally {
+ await this.ungroupTabLocked(tabId, scopedGroupId);
this.sendEvent(EVENTS.SCOPE_TAB_REMOVED, { tabId, reason });
}
}
diff --git a/skyvern/browser_extension/relay.py b/skyvern/browser_extension/relay.py
index 5316e7fbe..2b49217d2 100644
--- a/skyvern/browser_extension/relay.py
+++ b/skyvern/browser_extension/relay.py
@@ -576,6 +576,7 @@ class ExtensionRelayServer:
self._site: web.TCPSite | None = None
self._websocket: web.WebSocketResponse | None = None
self._connection_lock = asyncio.Lock()
+ self._send_lock = asyncio.Lock()
self._connected_event = asyncio.Event()
self._request_ids = itertools.count(1)
self._pending: dict[str, asyncio.Future[dict]] = {}
@@ -649,7 +650,7 @@ class ExtensionRelayServer:
future: asyncio.Future[dict] = asyncio.get_running_loop().create_future()
self._pending[request_id] = future
try:
- await websocket.send_json(frame)
+ await self._send_json(websocket, frame)
except (ConnectionError, RuntimeError):
pending = self._pending.pop(request_id, None)
if pending is not None:
@@ -670,6 +671,7 @@ class ExtensionRelayServer:
raise
async def _handle_pair_page(self, _request: web.Request) -> web.Response:
+ LOG.info("browser_extension_pair_page_served")
return web.Response(
text=_PAIR_PAGE,
content_type="text/html",
@@ -705,19 +707,24 @@ class ExtensionRelayServer:
payload = await _read_json_object(request)
supplied_nonce = payload.get("nonce") if payload is not None else None
+ valid_payload = (
+ payload is not None
+ and type(payload.get("v")) is int
+ and payload["v"] == PROTOCOL_VERSION
+ and isinstance(supplied_nonce, str)
+ )
nonce_matches = secrets.compare_digest(
active_nonce or ("0" * 43),
supplied_nonce if isinstance(supplied_nonce, str) else "",
)
expired = created_at is None or time.monotonic() - created_at >= _PAIRING_NONCE_TTL_SECONDS
- if (
- payload is None
- or type(payload.get("v")) is not int
- or payload["v"] != PROTOCOL_VERSION
- or not nonce_matches
- or expired
- ):
+ if not valid_payload:
+ LOG.info("browser_extension_pair_claim", outcome="bad_payload")
+ return web.json_response({"error": "invalid_nonce"}, status=403, headers={"Cache-Control": "no-store"})
+ if not nonce_matches or expired:
+ LOG.info("browser_extension_pair_claim", outcome="expired_or_unknown_nonce")
return web.json_response({"error": "invalid_nonce"}, status=403, headers={"Cache-Control": "no-store"})
+ LOG.info("browser_extension_pair_claim", outcome="ok")
return web.json_response(
{"v": PROTOCOL_VERSION, "port": self.bound_port, "token": self._token},
headers={"Cache-Control": "no-store"},
@@ -726,58 +733,76 @@ class ExtensionRelayServer:
async def _handle_websocket(self, request: web.Request) -> web.WebSocketResponse:
websocket = web.WebSocketResponse(max_msg_size=_MAX_WS_MESSAGE_BYTES)
await websocket.prepare(request)
+ LOG.info("browser_extension_websocket_opened", remote_address=request.remote)
- origin = request.headers.get("Origin")
- if origin is not None and not _is_extension_origin(origin):
- await websocket.close(code=_AUTH_CLOSE_CODE, message=b"authentication failed")
- return websocket
-
- server_nonce, challenge = build_challenge()
- await websocket.send_str(challenge)
try:
- proof_frame = await websocket.receive(timeout=_AUTH_TIMEOUT_SECONDS)
+ origin = request.headers.get("Origin")
+ if origin is not None and not _is_extension_origin(origin):
+ await self._reject_authentication(websocket, "invalid_origin")
+ return websocket
+
+ server_nonce, challenge = build_challenge()
+ await websocket.send_str(challenge)
+ try:
+ proof_frame = await websocket.receive(timeout=_AUTH_TIMEOUT_SECONDS)
+ except TimeoutError:
+ await self._reject_authentication(websocket, "timeout")
+ return websocket
if proof_frame.type is not WSMsgType.TEXT:
- raise BrowserExtensionError("Authentication proof must be a text frame")
- proof = parse_extension_message(proof_frame.data)
+ await self._reject_authentication(websocket, "bad_payload")
+ return websocket
+ try:
+ proof = parse_extension_message(proof_frame.data)
+ except BrowserExtensionError:
+ await self._reject_authentication(websocket, _auth_parse_failure_reason(proof_frame.data))
+ return websocket
if proof.kind != "auth.proof" or proof.client_nonce is None or proof.proof is None:
- raise BrowserExtensionError("Authentication proof is missing")
+ await self._reject_authentication(websocket, "bad_payload")
+ return websocket
if not _is_valid_client_nonce(proof.client_nonce):
- raise BrowserExtensionError("Authentication nonce is invalid")
+ await self._reject_authentication(websocket, "bad_nonce")
+ return websocket
if not verify_ext_proof(self._token, server_nonce, proof.client_nonce, proof.proof):
- raise BrowserExtensionError("Authentication proof is invalid")
- except (BrowserExtensionError, TimeoutError):
- await websocket.close(code=_AUTH_CLOSE_CODE, message=b"authentication failed")
+ await self._reject_authentication(websocket, "bad_proof")
+ return websocket
+
+ await self._send_json(
+ websocket,
+ {
+ "v": 1,
+ "type": "auth.ok",
+ "serverProof": compute_server_proof(self._token, proof.client_nonce, server_nonce),
+ },
+ )
+ LOG.info("browser_extension_auth_succeeded")
+ await self._activate_connection(websocket)
+
+ loop = asyncio.get_running_loop()
+ last_inbound = loop.time()
+ keepalive_task = asyncio.create_task(self._run_keepalive(websocket, lambda: last_inbound))
+ try:
+ async for message in websocket:
+ last_inbound = loop.time()
+ if self._websocket is not websocket:
+ break
+ if message.type is WSMsgType.TEXT:
+ await self._handle_text_frame(websocket, message.data)
+ elif message.type in {WSMsgType.CLOSE, WSMsgType.CLOSED, WSMsgType.ERROR}:
+ break
+ else:
+ LOG.warning("browser extension sent a non-text frame")
+ finally:
+ keepalive_task.cancel()
+ with suppress(asyncio.CancelledError):
+ await keepalive_task
+ await self._handle_disconnect(websocket)
return websocket
-
- await websocket.send_json(
- {
- "v": 1,
- "type": "auth.ok",
- "serverProof": compute_server_proof(self._token, proof.client_nonce, server_nonce),
- }
- )
- await self._activate_connection(websocket)
-
- loop = asyncio.get_running_loop()
- last_inbound = loop.time()
- keepalive_task = asyncio.create_task(self._run_keepalive(websocket, lambda: last_inbound))
- try:
- async for message in websocket:
- last_inbound = loop.time()
- if self._websocket is not websocket:
- break
- if message.type is WSMsgType.TEXT:
- await self._handle_text_frame(websocket, message.data)
- elif message.type in {WSMsgType.CLOSE, WSMsgType.CLOSED, WSMsgType.ERROR}:
- break
- else:
- LOG.warning("browser extension sent a non-text frame")
finally:
- keepalive_task.cancel()
- with suppress(asyncio.CancelledError):
- await keepalive_task
- await self._handle_disconnect(websocket)
- return websocket
+ LOG.info("browser_extension_websocket_closed", close_code=websocket.close_code)
+
+ async def _reject_authentication(self, websocket: web.WebSocketResponse, reason: str) -> None:
+ LOG.info("browser_extension_auth_failed", reason=reason)
+ await websocket.close(code=_AUTH_CLOSE_CODE, message=b"authentication failed")
async def _activate_connection(self, websocket: web.WebSocketResponse) -> None:
async with self._connection_lock:
@@ -786,6 +811,7 @@ class ExtensionRelayServer:
self._connected_event.clear()
if previous is not None and previous is not websocket:
+ LOG.info("browser_extension_websocket_replaced")
self._fail_pending_requests()
self.scoped_tabs = []
await self._call_on_disconnect()
@@ -806,7 +832,11 @@ class ExtensionRelayServer:
if message.event == "extension.hello":
await self._mark_hello_processed(websocket)
elif message.kind == "ping":
- await websocket.send_json({"v": 1, "type": "pong"})
+ await self._send_json(websocket, {"v": 1, "type": "pong"})
+
+ async def _send_json(self, websocket: web.WebSocketResponse, frame: dict) -> None:
+ async with self._send_lock:
+ await websocket.send_json(frame)
async def _mark_hello_processed(self, websocket: web.WebSocketResponse) -> None:
async with self._connection_lock:
@@ -871,7 +901,7 @@ class ExtensionRelayServer:
return
if now >= next_ping:
try:
- await websocket.send_json({"v": 1, "type": "ping"})
+ await self._send_json(websocket, {"v": 1, "type": "ping"})
except (ConnectionError, RuntimeError):
return
next_ping = now + _PING_INTERVAL_SECONDS
@@ -920,6 +950,16 @@ async def _read_json_object(request: web.Request) -> dict | None:
return payload if isinstance(payload, dict) else None
+def _auth_parse_failure_reason(raw: str) -> str:
+ try:
+ payload = json.loads(raw)
+ except (json.JSONDecodeError, TypeError):
+ return "bad_payload"
+ if isinstance(payload, dict) and type(payload.get("v")) is int and payload["v"] != PROTOCOL_VERSION:
+ return "protocol_mismatch"
+ return "bad_payload"
+
+
def _is_valid_client_nonce(client_nonce: str) -> bool:
if not client_nonce or "=" in client_nonce:
return False
diff --git a/tests/unit/browser_extension/test_cdp_adapter.py b/tests/unit/browser_extension/test_cdp_adapter.py
index e2e0bd12d..45e7dd24c 100644
--- a/tests/unit/browser_extension/test_cdp_adapter.py
+++ b/tests/unit/browser_extension/test_cdp_adapter.py
@@ -6,7 +6,7 @@ from unittest.mock import patch
import pytest
import pytest_asyncio
-from aiohttp import ClientSession, ClientWebSocketResponse, WSMsgType
+from aiohttp import ClientSession, ClientWebSocketResponse
from skyvern.browser_extension.cdp_adapter import ExtensionCdpAdapter
from skyvern.browser_extension.errors import ExtensionRequestError
@@ -23,6 +23,14 @@ class StubRelay:
self.block_attach_tab_id: int | None = None
self.attach_started = asyncio.Event()
self.release_attach = asyncio.Event()
+ self.block_detach_tab_id: int | None = None
+ self.detach_started = asyncio.Event()
+ self.release_detach = asyncio.Event()
+ self.main_frame_ids: dict[int, str] = {}
+ self.block_send_keys: set[tuple[str | None, str]] = set()
+ self.fail_send_keys: dict[tuple[str | None, str], ExtensionRequestError] = {}
+ self.send_started: dict[tuple[str | None, str], asyncio.Event] = {}
+ self.release_send: dict[tuple[str | None, str], asyncio.Event] = {}
async def request(self, op: str, args: dict, timeout: float = 30.0) -> dict:
self.calls.append((op, args))
@@ -37,11 +45,23 @@ class StubRelay:
if tab_id == self.block_attach_tab_id:
self.attach_started.set()
await self.release_attach.wait()
+ if op == "debugger.detach" and args["tabId"] == self.block_detach_tab_id:
+ self.detach_started.set()
+ await self.release_detach.wait()
if op == "tabs.create":
tab_id = self.next_tab_id
self.next_tab_id += 1
return {"tabId": tab_id}
if op == "debugger.send":
+ key = (args.get("sessionId"), args["method"])
+ if key in self.block_send_keys:
+ self.send_started.setdefault(key, asyncio.Event()).set()
+ await self.release_send.setdefault(key, asyncio.Event()).wait()
+ if key in self.fail_send_keys:
+ raise self.fail_send_keys.pop(key)
+ if args["method"] == "Page.getFrameTree" and args["tabId"] in self.main_frame_ids:
+ frame_id = self.main_frame_ids[args["tabId"]]
+ return {"result": {"frameTree": {"frame": {"id": frame_id}}}}
return {"result": {"forwardedMethod": args["method"]}}
return {}
@@ -202,6 +222,53 @@ async def test_auto_attach_existing_and_later_scoped_tabs() -> None:
assert popup["params"]["targetInfo"]["openerId"] == "tab-7"
+async def test_root_target_discovery_waits_for_auto_attach_while_session_commands_continue() -> None:
+ scoped_tabs = [
+ {"tabId": 41, "url": "https://one.example", "title": "One"},
+ {"tabId": 42, "url": "https://two.example", "title": "Two"},
+ ]
+ relay = StubRelay(scoped_tabs)
+ relay.main_frame_ids = {41: "frame-41", 42: "frame-42"}
+ relay.block_attach_tab_id = 42
+ registry = VirtualTargetRegistry()
+ adapter = ExtensionCdpAdapter(registry, relay)
+ await adapter.handle_extension_event("extension.hello", {"scopedTabs": scoped_tabs})
+ await adapter.start()
+ try:
+ async with ClientSession() as client, client.ws_connect(adapter.cdp_ws_url) as ws:
+ await ws.send_json({"id": 1, "method": "Target.setAutoAttach", "params": {"autoAttach": True}})
+ assert await receive_response(ws, 1) == {"id": 1, "result": {}}
+ await asyncio.wait_for(relay.attach_started.wait(), 1)
+
+ attached_session_id = registry.root_session_id(41)
+ await ws.send_json({"id": 2, "method": "Target.getTargets", "params": {}})
+ await ws.send_json({"id": 3, "sessionId": attached_session_id, "method": "Runtime.enable", "params": {}})
+
+ early_get_targets = None
+ session_response = None
+ while session_response is None:
+ message = await ws.receive_json(timeout=1)
+ if message.get("id") == 2:
+ early_get_targets = message
+ elif message.get("id") == 3:
+ session_response = message
+ assert early_get_targets is None
+ assert session_response == {
+ "id": 3,
+ "sessionId": attached_session_id,
+ "result": {"forwardedMethod": "Runtime.enable"},
+ }
+
+ relay.release_attach.set()
+ targets = await receive_response(ws, 2)
+ finally:
+ relay.release_attach.set()
+ await adapter.stop()
+
+ assert {info["targetId"] for info in targets["result"]["targetInfos"]} == {"frame-41", "frame-42"}
+
+
@pytest.mark.asyncio
async def test_auto_attach_post_reply_failure_skips_failed_tab_without_second_response() -> None:
relay = StubRelay(
@@ -446,7 +513,12 @@ async def test_child_attach_recurses_and_detach_unregisters(
"tabId": 12,
"sessionId": "child-12",
"method": "Target.setAutoAttach",
- "params": {"flatten": True, "autoAttach": True, "waitForDebuggerOnStart": False},
+ "params": {
+ "flatten": True,
+ "autoAttach": True,
+ "waitForDebuggerOnStart": False,
+ "filter": [{"type": "iframe", "exclude": False}],
+ },
},
)
assert relay.calls[1][1]["sessionId"] == "child-12"
@@ -455,6 +527,564 @@ async def test_child_attach_recurses_and_detach_unregisters(
registry.resolve_session("child-12")
+async def test_child_detach_during_post_registration_window_emits_and_unregisters(
+ adapter_server: tuple[ExtensionCdpAdapter, StubRelay, VirtualTargetRegistry],
+) -> None:
+ adapter, _, registry = adapter_server
+ registry.register_tab(17, "https://example.com", "Example")
+ child_session_id = "child-17"
+ replay_started = asyncio.Event()
+ release_replay = asyncio.Event()
+
+ async def block_replay(session_id: str) -> None:
+ assert session_id == child_session_id
+ replay_started.set()
+ await release_replay.wait()
+
+ async with ClientSession() as client, client.ws_connect(adapter.cdp_ws_url) as ws:
+ await ws.send_json({"id": 1, "method": "Target.setAutoAttach", "params": {"autoAttach": True}})
+ await receive_response(ws, 1)
+ await receive_event(ws, "Target.attachedToTarget")
+
+ with patch.object(adapter, "_replay_buffered_child_events", side_effect=block_replay):
+ await adapter.handle_extension_event(
+ "debugger.event",
+ {
+ "tabId": 17,
+ "method": "Target.attachedToTarget",
+ "params": {
+ "sessionId": child_session_id,
+ "targetInfo": {
+ "targetId": "frame-17",
+ "type": "iframe",
+ "title": "",
+ "url": "https://example.com/frame",
+ "attached": True,
+ "canAccessOpener": False,
+ "browserContextId": "skyvern-default",
+ },
+ "waitingForDebugger": False,
+ },
+ },
+ )
+ await receive_event(ws, "Target.attachedToTarget")
+ await asyncio.wait_for(replay_started.wait(), 1)
+ assert child_session_id in adapter._pending_child_sessions
+ assert registry.resolve_session(child_session_id) == (17, child_session_id)
+
+ await adapter.handle_extension_event(
+ "debugger.event",
+ {
+ "tabId": 17,
+ "method": "Target.detachedFromTarget",
+ "params": {"sessionId": child_session_id},
+ },
+ )
+ detached = await receive_event(ws, "Target.detachedFromTarget")
+
+ await ws.send_json({"id": 2, "sessionId": child_session_id, "method": "Runtime.enable", "params": {}})
+ missing = await receive_response(ws, 2)
+ release_replay.set()
+
+ assert detached["params"] == {"sessionId": child_session_id}
+ assert missing == {
+ "id": 2,
+ "sessionId": child_session_id,
+ "error": {"code": -32001, "message": "session not found"},
+ }
+
+
+async def test_child_detach_before_probe_completes_is_swallowed_and_tombstones_initialization(
+ adapter_server: tuple[ExtensionCdpAdapter, StubRelay, VirtualTargetRegistry],
+) -> None:
+ adapter, relay, registry = adapter_server
+ registry.register_tab(18, "https://example.com", "Example")
+ child_session_id = "child-18"
+ probe_key = (child_session_id, "Target.setAutoAttach")
+ relay.block_send_keys.add(probe_key)
+
+ async with ClientSession() as client, client.ws_connect(adapter.cdp_ws_url) as ws:
+ await ws.send_json({"id": 1, "method": "Target.setAutoAttach", "params": {"autoAttach": True}})
+ await receive_response(ws, 1)
+ await receive_event(ws, "Target.attachedToTarget")
+
+ await adapter.handle_extension_event(
+ "debugger.event",
+ {
+ "tabId": 18,
+ "method": "Target.attachedToTarget",
+ "params": {
+ "sessionId": child_session_id,
+ "targetInfo": {
+ "targetId": "frame-18",
+ "type": "iframe",
+ "title": "",
+ "url": "https://example.com/frame",
+ "attached": True,
+ "canAccessOpener": False,
+ "browserContextId": "skyvern-default",
+ },
+ "waitingForDebugger": False,
+ },
+ },
+ )
+ await asyncio.wait_for(relay.send_started.setdefault(probe_key, asyncio.Event()).wait(), 1)
+ initialization_task = next(task for task in adapter._background_tasks if not task.done())
+
+ await adapter.handle_extension_event(
+ "debugger.event",
+ {
+ "tabId": 18,
+ "method": "Target.detachedFromTarget",
+ "params": {"sessionId": child_session_id},
+ },
+ )
+ with pytest.raises(TimeoutError):
+ await ws.receive_json(timeout=0.05)
+
+ relay.release_send[probe_key].set()
+ await asyncio.wait_for(initialization_task, 1)
+
+ with pytest.raises(KeyError):
+ registry.resolve_session(child_session_id)
+
+
+async def test_nested_child_attach_during_parent_probe_is_replayed_in_parent_first_order(
+ adapter_server: tuple[ExtensionCdpAdapter, StubRelay, VirtualTargetRegistry],
+) -> None:
+ adapter, relay, registry = adapter_server
+ registry.register_tab(13, "https://parent.example", "Parent")
+ root_session_id = registry.root_session_id(13)
+ parent_session_id = "parent-child-13"
+ grandchild_session_id = "grandchild-13"
+ parent_probe_key = (parent_session_id, "Target.setAutoAttach")
+ relay.block_send_keys.add(parent_probe_key)
+
+ async with ClientSession() as client, client.ws_connect(adapter.cdp_ws_url) as ws:
+ await ws.send_json({"id": 1, "method": "Target.setAutoAttach", "params": {"autoAttach": True}})
+ await receive_response(ws, 1)
+ await receive_event(ws, "Target.attachedToTarget")
+ relay.calls.clear()
+
+ parent_params = {
+ "sessionId": parent_session_id,
+ "targetInfo": {
+ "targetId": "parent-frame-13",
+ "type": "iframe",
+ "title": "",
+ "url": "https://parent.example/frame",
+ "attached": True,
+ "canAccessOpener": False,
+ "browserContextId": "skyvern-default",
+ },
+ "waitingForDebugger": False,
+ }
+ await adapter.handle_extension_event(
+ "debugger.event",
+ {"tabId": 13, "method": "Target.attachedToTarget", "params": parent_params},
+ )
+ await asyncio.wait_for(relay.send_started.setdefault(parent_probe_key, asyncio.Event()).wait(), 1)
+
+ grandchild_params = {
+ "sessionId": grandchild_session_id,
+ "targetInfo": {
+ "targetId": "grandchild-frame-13",
+ "type": "iframe",
+ "title": "",
+ "url": "https://grandchild.example/frame",
+ "attached": True,
+ "canAccessOpener": False,
+ "browserContextId": "skyvern-default",
+ },
+ "waitingForDebugger": False,
+ }
+ await adapter.handle_extension_event(
+ "debugger.event",
+ {
+ "tabId": 13,
+ "sessionId": parent_session_id,
+ "method": "Target.attachedToTarget",
+ "params": grandchild_params,
+ },
+ )
+
+ relay.release_send[parent_probe_key].set()
+ parent_attached = await receive_event(ws, "Target.attachedToTarget")
+ grandchild_attached = await receive_event(ws, "Target.attachedToTarget")
+ assert registry.resolve_session(grandchild_session_id) == (13, grandchild_session_id)
+
+ assert parent_attached["sessionId"] == root_session_id
+ assert parent_attached["params"] == parent_params
+ assert grandchild_attached["sessionId"] == parent_session_id
+ assert grandchild_attached["params"] == grandchild_params
+
+
+async def test_nested_child_attach_is_detached_when_parent_probe_fails(
+ adapter_server: tuple[ExtensionCdpAdapter, StubRelay, VirtualTargetRegistry],
+) -> None:
+ adapter, relay, registry = adapter_server
+ registry.register_tab(15, "https://parent.example", "Parent")
+ parent_session_id = "parent-child-15"
+ grandchild_session_id = "grandchild-15"
+ parent_probe_key = (parent_session_id, "Target.setAutoAttach")
+ relay.block_send_keys.add(parent_probe_key)
+ relay.fail_send_keys[parent_probe_key] = ExtensionRequestError("INTERNAL", "parent probe failed")
+
+ async with ClientSession() as client, client.ws_connect(adapter.cdp_ws_url) as ws:
+ await ws.send_json({"id": 1, "method": "Target.setAutoAttach", "params": {"autoAttach": True}})
+ await receive_response(ws, 1)
+ await receive_event(ws, "Target.attachedToTarget")
+ relay.calls.clear()
+
+ await adapter.handle_extension_event(
+ "debugger.event",
+ {
+ "tabId": 15,
+ "method": "Target.attachedToTarget",
+ "params": {
+ "sessionId": parent_session_id,
+ "targetInfo": {
+ "targetId": "parent-frame-15",
+ "type": "iframe",
+ "title": "",
+ "url": "https://parent.example/frame",
+ "attached": True,
+ "canAccessOpener": False,
+ "browserContextId": "skyvern-default",
+ },
+ "waitingForDebugger": False,
+ },
+ },
+ )
+ await asyncio.wait_for(relay.send_started.setdefault(parent_probe_key, asyncio.Event()).wait(), 1)
+
+ await adapter.handle_extension_event(
+ "debugger.event",
+ {
+ "tabId": 15,
+ "sessionId": parent_session_id,
+ "method": "Target.attachedToTarget",
+ "params": {
+ "sessionId": grandchild_session_id,
+ "targetInfo": {
+ "targetId": "grandchild-frame-15",
+ "type": "iframe",
+ "title": "",
+ "url": "https://grandchild.example/frame",
+ "attached": True,
+ "canAccessOpener": False,
+ "browserContextId": "skyvern-default",
+ },
+ "waitingForDebugger": False,
+ },
+ },
+ )
+
+ relay.release_send[parent_probe_key].set()
+ with pytest.raises(TimeoutError):
+ await ws.receive_json(timeout=0.05)
+ with pytest.raises(KeyError):
+ registry.resolve_session(parent_session_id)
+ with pytest.raises(KeyError):
+ registry.resolve_session(grandchild_session_id)
+
+ assert (
+ "debugger.send",
+ {
+ "tabId": 15,
+ "sessionId": grandchild_session_id,
+ "method": "Runtime.runIfWaitingForDebugger",
+ "params": {},
+ },
+ ) in relay.calls
+ assert (
+ "debugger.send",
+ {
+ "tabId": 15,
+ "method": "Target.detachFromTarget",
+ "params": {"sessionId": grandchild_session_id},
+ },
+ ) in relay.calls
+
+
+async def test_pending_child_detach_does_not_wait_for_buffered_grandchild_cleanup(
+ adapter_server: tuple[ExtensionCdpAdapter, StubRelay, VirtualTargetRegistry],
+) -> None:
+ adapter, relay, registry = adapter_server
+ registry.register_tab(19, "https://parent.example", "Parent")
+ parent_session_id = "parent-child-19"
+ grandchild_session_id = "grandchild-19"
+ resume_key = (grandchild_session_id, "Runtime.runIfWaitingForDebugger")
+ relay.block_send_keys.add(resume_key)
+ adapter._pending_child_sessions.add(parent_session_id)
+ adapter._pending_child_events[parent_session_id] = [
+ {
+ "tabId": 19,
+ "sessionId": parent_session_id,
+ "method": "Target.attachedToTarget",
+ "params": {
+ "sessionId": grandchild_session_id,
+ "targetInfo": {
+ "targetId": "grandchild-frame-19",
+ "type": "iframe",
+ "title": "",
+ "url": "https://grandchild.example/frame",
+ "attached": True,
+ "canAccessOpener": False,
+ "browserContextId": "skyvern-default",
+ },
+ "waitingForDebugger": False,
+ },
+ }
+ ]
+
+ await asyncio.wait_for(
+ adapter.handle_extension_event(
+ "debugger.event",
+ {
+ "tabId": 19,
+ "method": "Target.detachedFromTarget",
+ "params": {"sessionId": parent_session_id},
+ },
+ ),
+ 0.5,
+ )
+ await asyncio.wait_for(relay.send_started.setdefault(resume_key, asyncio.Event()).wait(), 0.5)
+ relay.release_send.setdefault(resume_key, asyncio.Event()).set()
+
+ async def grandchild_detached() -> None:
+ expected = (
+ "debugger.send",
+ {
+ "tabId": 19,
+ "method": "Target.detachFromTarget",
+ "params": {"sessionId": grandchild_session_id},
+ },
+ )
+ while expected not in relay.calls:
+ await asyncio.sleep(0)
+
+ await asyncio.wait_for(grandchild_detached(), 0.5)
+ assert parent_session_id not in adapter._pending_child_sessions
+ assert parent_session_id not in adapter._pending_child_events
+
+
+async def test_child_initialization_keeps_live_outer_alias_when_another_alias_is_dead(
+ adapter_server: tuple[ExtensionCdpAdapter, StubRelay, VirtualTargetRegistry],
+) -> None:
+ adapter, _, registry = adapter_server
+ registry.register_tab(20, "https://parent.example", "Parent")
+ adapter._auto_attach = True
+ dead_alias = registry.create_root_session_alias(20)
+ live_alias = registry.create_root_session_alias(20)
+ assert registry.remove_root_session_alias(dead_alias)
+ child_session_id = "child-20"
+ target_info = {
+ "targetId": "frame-20",
+ "type": "iframe",
+ "title": "",
+ "url": "https://child.example/frame",
+ "attached": True,
+ "canAccessOpener": False,
+ "browserContextId": "skyvern-default",
+ }
+ event_params = {
+ "sessionId": child_session_id,
+ "targetInfo": target_info,
+ "waitingForDebugger": False,
+ }
+ adapter._pending_child_sessions.add(child_session_id)
+
+ with patch.object(adapter, "_emit_to_sessions", wraps=adapter._emit_to_sessions) as emit_to_sessions:
+ await adapter._initialize_child_target(
+ 20,
+ child_session_id,
+ target_info,
+ event_params,
+ [dead_alias, live_alias],
+ )
+
+ assert registry.resolve_session(child_session_id) == (20, child_session_id)
+ emit_to_sessions.assert_awaited_once_with("Target.attachedToTarget", event_params, [live_alias])
+
+
+async def test_child_initialization_resumes_and_detaches_when_all_outer_aliases_are_dead(
+ adapter_server: tuple[ExtensionCdpAdapter, StubRelay, VirtualTargetRegistry],
+) -> None:
+ adapter, relay, registry = adapter_server
+ registry.register_tab(21, "https://parent.example", "Parent")
+ adapter._auto_attach = True
+ outer_session_ids = registry.root_session_ids(21)
+ registry.remove_tab(21)
+ child_session_id = "child-21"
+ target_info = {
+ "targetId": "frame-21",
+ "type": "iframe",
+ "title": "",
+ "url": "https://child.example/frame",
+ "attached": True,
+ "canAccessOpener": False,
+ "browserContextId": "skyvern-default",
+ }
+ adapter._pending_child_sessions.add(child_session_id)
+
+ await adapter._initialize_child_target(
+ 21,
+ child_session_id,
+ target_info,
+ {"sessionId": child_session_id, "targetInfo": target_info, "waitingForDebugger": False},
+ outer_session_ids,
+ )
+
+ assert (
+ "debugger.send",
+ {
+ "tabId": 21,
+ "sessionId": child_session_id,
+ "method": "Runtime.runIfWaitingForDebugger",
+ "params": {},
+ },
+ ) in relay.calls
+ assert (
+ "debugger.send",
+ {
+ "tabId": 21,
+ "method": "Target.detachFromTarget",
+ "params": {"sessionId": child_session_id},
+ },
+ ) in relay.calls
+ assert child_session_id not in adapter._pending_child_sessions
+ with pytest.raises(KeyError):
+ registry.resolve_session(child_session_id)
+
+
+async def test_child_auto_attach_failure_skips_session_and_navigation_lifecycle_continues() -> None:
+ relay = StubRelay([{"tabId": 14, "url": "https://page.example", "title": "Page"}])
+ registry = VirtualTargetRegistry()
+ adapter = ExtensionCdpAdapter(registry, relay)
+ await adapter.start()
+ try:
+ async with ClientSession() as client, client.ws_connect(adapter.cdp_ws_url) as ws:
+ await ws.send_json({"id": 1, "method": "Target.setAutoAttach", "params": {"autoAttach": True}})
+ await receive_response(ws, 1)
+ await receive_event(ws, "Target.attachedToTarget")
+ relay.calls.clear()
+ relay.fail_send_keys[("unsupported-child", "Target.setAutoAttach")] = ExtensionRequestError(
+ "INTERNAL", "child target does not support auto-attach"
+ )
+
+ await adapter.handle_extension_event(
+ "debugger.event",
+ {
+ "tabId": 14,
+ "method": "Target.attachedToTarget",
+ "params": {
+ "sessionId": "unsupported-child",
+ "targetInfo": {
+ "targetId": "unsupported-target",
+ "type": "iframe",
+ "title": "",
+ "url": "https://frame.example",
+ "attached": True,
+ "canAccessOpener": False,
+ "browserContextId": "skyvern-default",
+ },
+ "waitingForDebugger": False,
+ },
+ },
+ )
+ await asyncio.sleep(0)
+
+ with pytest.raises(KeyError):
+ registry.resolve_session("unsupported-child")
+ with pytest.raises(TimeoutError):
+ await ws.receive_json(timeout=0.05)
+
+ root_session_id = registry.root_session_id(14)
+ await ws.send_json(
+ {
+ "id": 2,
+ "sessionId": root_session_id,
+ "method": "Page.navigate",
+ "params": {"url": "https://destination.example"},
+ }
+ )
+ assert (await receive_response(ws, 2))["result"] == {"forwardedMethod": "Page.navigate"}
+
+ lifecycle_params = {"name": "DOMContentLoaded", "frameId": "main"}
+ await adapter.handle_extension_event(
+ "debugger.event",
+ {
+ "tabId": 14,
+ "method": "Page.lifecycleEvent",
+ "params": lifecycle_params,
+ },
+ )
+ lifecycle = await receive_event(ws, "Page.lifecycleEvent")
+ assert lifecycle["sessionId"] == root_session_id
+ assert lifecycle["params"] == lifecycle_params
+ finally:
+ await adapter.stop()
+
+ assert (
+ "debugger.send",
+ {
+ "tabId": 14,
+ "method": "Target.detachFromTarget",
+ "params": {"sessionId": "unsupported-child"},
+ },
+ ) in relay.calls
+
+
+async def test_slow_session_command_does_not_block_independent_session_command() -> None:
+ relay = StubRelay()
+ registry = VirtualTargetRegistry()
+ registry.register_tab(16, "https://page.example", "Page")
+ session_id = registry.root_session_id(16)
+ navigate_key = (None, "Page.navigate")
+ relay.block_send_keys.add(navigate_key)
+ adapter = ExtensionCdpAdapter(registry, relay)
+ await adapter.start()
+ try:
+ async with ClientSession() as client, client.ws_connect(adapter.cdp_ws_url) as ws:
+ await ws.send_json(
+ {
+ "id": 1,
+ "sessionId": session_id,
+ "method": "Page.navigate",
+ "params": {"url": "https://destination.example"},
+ }
+ )
+ await asyncio.wait_for(relay.send_started.setdefault(navigate_key, asyncio.Event()).wait(), 1)
+
+ await ws.send_json(
+ {
+ "id": 2,
+ "sessionId": session_id,
+ "method": "Runtime.runIfWaitingForDebugger",
+ "params": {},
+ }
+ )
+ fast_response = await receive_response(ws, 2)
+ assert fast_response["result"] == {"forwardedMethod": "Runtime.runIfWaitingForDebugger"}
+
+ relay.release_send.setdefault(navigate_key, asyncio.Event()).set()
+ navigate_response = await receive_response(ws, 1)
+ assert navigate_response["result"] == {"forwardedMethod": "Page.navigate"}
+ finally:
+ relay.release_send.setdefault(navigate_key, asyncio.Event()).set()
+ await adapter.stop()
+
+
@pytest.mark.asyncio
async def test_get_target_info_returns_registered_child_target(
adapter_server: tuple[ExtensionCdpAdapter, StubRelay, VirtualTargetRegistry],
@@ -626,17 +1256,67 @@ async def test_second_client_rejected_and_disconnect_allows_future_client(
@pytest.mark.asyncio
-async def test_browser_close_replies_then_closes_without_removing_tabs(
+async def test_old_client_disconnect_does_not_cancel_new_client_command(
+ adapter_server: tuple[ExtensionCdpAdapter, StubRelay, VirtualTargetRegistry],
+) -> None:
+ adapter, relay, registry = adapter_server
+ old_cancel_started = asyncio.Event()
+ release_old_cancel = asyncio.Event()
+ original_cancel_client_tasks = adapter._cancel_client_tasks
+
+ async def block_old_cancel(*args: object) -> None:
+ old_cancel_started.set()
+ await release_old_cancel.wait()
+ await original_cancel_client_tasks(*args)
+
+ async with ClientSession() as client:
+ await client.ws_connect(adapter.cdp_ws_url)
+ with patch.object(adapter, "_cancel_client_tasks", side_effect=block_old_cancel):
+ disconnect_task = asyncio.create_task(adapter.on_extension_disconnect())
+ await asyncio.wait_for(old_cancel_started.wait(), 1)
+
+ new = await client.ws_connect(adapter.cdp_ws_url)
+ registry.register_tab(24, "https://example.com", "Example")
+ session_id = registry.root_session_id(24)
+ send_key = (None, "Runtime.evaluate")
+ relay.block_send_keys.add(send_key)
+ await new.send_json(
+ {"id": 1, "sessionId": session_id, "method": "Runtime.evaluate", "params": {"expression": "1+1"}}
+ )
+ await asyncio.wait_for(relay.send_started.setdefault(send_key, asyncio.Event()).wait(), 1)
+
+ release_old_cancel.set()
+ await asyncio.wait_for(disconnect_task, 2)
+ relay.release_send[send_key].set()
+
+ assert await receive_response(new, 1) == {
+ "id": 1,
+ "sessionId": session_id,
+ "result": {"forwardedMethod": "Runtime.evaluate"},
+ }
+ await asyncio.sleep(0)
+ assert adapter._client_tasks == {}
+ await new.close()
+
+
+async def test_browser_close_detaches_all_tabs_when_client_closes_after_reply(
adapter_server: tuple[ExtensionCdpAdapter, StubRelay, VirtualTargetRegistry],
) -> None:
adapter, relay, registry = adapter_server
registry.register_tab(22, "https://example.com", "Example")
+ registry.register_tab(23, "https://other.example", "Other")
+ relay.block_detach_tab_id = 22
- async with ClientSession() as client, client.ws_connect(adapter.cdp_ws_url) as ws:
+ async with ClientSession() as client:
+ ws = await client.ws_connect(adapter.cdp_ws_url)
await ws.send_json({"id": 9, "method": "Browser.close", "params": {}})
assert await receive_response(ws, 9) == {"id": 9, "result": {}}
- close_message = await ws.receive(timeout=2)
- assert close_message.type in {WSMsgType.CLOSE, WSMsgType.CLOSED}
+ close_task = asyncio.create_task(ws.close())
+ await asyncio.wait_for(relay.detach_started.wait(), 1)
+ await asyncio.sleep(0.05)
+ relay.release_detach.set()
+ await asyncio.wait_for(close_task, 2)
- assert ("debugger.detach", {"tabId": 22}) in relay.calls
+ assert {args["tabId"] for op, args in relay.calls if op == "debugger.detach"} == {22, 23}
assert all(op != "tabs.remove" for op, _ in relay.calls)
diff --git a/tests/unit/browser_extension/test_relay.py b/tests/unit/browser_extension/test_relay.py
index 21a43dcb2..d1ec3f01f 100644
--- a/tests/unit/browser_extension/test_relay.py
+++ b/tests/unit/browser_extension/test_relay.py
@@ -187,6 +187,27 @@ async def test_pair_claim_expired_nonce_is_forbidden(
assert await response.json() == {"error": "invalid_nonce"}
[email protected]("payload", [None, [], {"v": "1", "nonce": "present"}, {"v": 1}])
+async def test_pair_claim_bad_payload_is_forbidden_and_consumes_active_nonce(
+ relay_harness: RelayHarness,
+ payload: object,
+) -> None:
+ nonce = relay_harness.server.create_pairing_nonce()
+ async with ClientSession() as session:
+ response = await session.post(http_url(relay_harness, "/pair/claim"), json=payload)
+ consumed = await session.post(
+ http_url(relay_harness, "/pair/claim"),
+ json={"v": 1, "nonce": nonce},
+ )
+
+ assert response.status == 403
+ assert response.headers["Cache-Control"] == "no-store"
+ assert await response.json() == {"error": "invalid_nonce"}
+ assert consumed.status == 403
+ assert await consumed.json() == {"error": "invalid_nonce"}
+
+
@pytest.mark.asyncio
async def test_pair_begin_bad_proof_is_forbidden(relay_harness: RelayHarness) -> None:
async with ClientSession() as session:
@@ -343,6 +364,79 @@ async def test_request_response_error_and_timeout_paths(relay_harness: RelayHarn
assert timeout_info.value.message == "extension request timed out: tabs.list"
+async def test_concurrent_same_session_requests_are_written_in_issue_order() -> None:
+ server = ExtensionRelayServer(TOKEN, 0, lambda _event, _params: asyncio.sleep(0))
+ arrivals: list[int] = []
+
+ class OvertakingWebSocket:
+ closed = False
+
+ async def send_json(self, frame: dict) -> None:
+ sequence = frame["args"]["params"]["sequence"]
+ if sequence == 0:
+ await asyncio.sleep(0.01)
+ arrivals.append(sequence)
+ server._pending.pop(frame["id"]).set_result({"sequence": sequence})
+
+ server._websocket = OvertakingWebSocket() # type: ignore[assignment]
+ server._connected_event.set()
+ tasks = []
+ for sequence in range(5):
+ tasks.append(
+ asyncio.create_task(
+ server.request(
+ "debugger.send",
+ {
+ "tabId": 17,
+ "sessionId": "child-17",
+ "method": "Runtime.evaluate",
+ "params": {"sequence": sequence},
+ },
+ )
+ )
+ )
+ await asyncio.sleep(0)
+
+ assert await asyncio.gather(*tasks) == [{"sequence": sequence} for sequence in range(5)]
+ assert arrivals == list(range(5))
+
+
+async def test_slow_first_response_does_not_delay_second_request_arrival(
+ relay_harness: RelayHarness,
+) -> None:
+ async with ClientSession() as session:
+ websocket = await authenticate(session, relay_harness)
+ first_task = asyncio.create_task(
+ relay_harness.server.request(
+ "debugger.send",
+ {"tabId": 17, "sessionId": "child-17", "method": "Runtime.enable", "params": {}},
+ )
+ )
+ first_request = await websocket.receive_json()
+ second_task = asyncio.create_task(
+ relay_harness.server.request(
+ "debugger.send",
+ {
+ "tabId": 17,
+ "sessionId": "child-17",
+ "method": "Runtime.runIfWaitingForDebugger",
+ "params": {},
+ },
+ )
+ )
+
+ second_request = await websocket.receive_json(timeout=0.5)
+ assert [first_request["id"], second_request["id"]] == ["r-1", "r-2"]
+ await websocket.send_json({"v": 1, "type": "response", "id": second_request["id"], "ok": True, "result": {}})
+ assert await second_task == {}
+ assert not first_task.done()
+
+ await websocket.send_json({"v": 1, "type": "response", "id": first_request["id"], "ok": True, "result": {}})
+ assert await first_task == {}
+
+
@pytest.mark.asyncio
async def test_response_larger_than_default_aiohttp_limit_reaches_requester(relay_harness: RelayHarness) -> None:
async with ClientSession() as session: