Skip to content

Automate browser based workflows with AI

fix(SKY-12367): release recording exfiltration channel driver and unpin JS asset cache (#7396)

LawyZheng7d3f4bed97de
diff --git a/skyvern/forge/sdk/routes/streaming/channels/cdp.py b/skyvern/forge/sdk/routes/streaming/channels/cdp.py
index 9000c43af..9d534e505 100644
--- a/skyvern/forge/sdk/routes/streaming/channels/cdp.py
+++ b/skyvern/forge/sdk/routes/streaming/channels/cdp.py
@@ -34,6 +34,23 @@ if t.TYPE_CHECKING:
LOG = structlog.get_logger()
[email protected]_cache(maxsize=None)
+def _load_js_asset(file_name: str) -> str:
+ # Module-level so the cache key is the asset name only. A method-level lru_cache
+ # keys on `self`, pinning every channel instance (and its Playwright driver) for
+ # the lifetime of the process.
+ base_path = pathlib.Path(__file__).parent / "js"
+ file_name = file_name.lstrip("/")
+
+ if not file_name.endswith(".js"):
+ file_name += ".js"
+
+ full_path = base_path / pathlib.Path(file_name)
+
+ with open(full_path, encoding="utf-8") as f:
+ return f.read()
+
+
class CdpChannel:
"""
CdpChannel. Relies on a VncChannel - without one, a CdpChannel has no
@@ -54,6 +71,10 @@ class CdpChannel:
self.page: Page | None = None
self.pw: Playwright | None = None
self.url: str | None = None
+ # Set True by a terminal stop() so the browser "disconnected" callback (registered
+ # in connect()) does not resurrect the connection during teardown. close() stays
+ # reconnect-neutral because connect() calls it to recycle a dropped connection.
+ self._closing = False
@property
def class_name(self) -> str:
@@ -70,6 +91,11 @@ class CdpChannel:
Idempotent.
"""
+ # A channel torn down by stop() never reconnects: this also neutralizes a
+ # reconnect task the "disconnected" callback may have scheduled just before teardown.
+ if self._closing:
+ return self
+
if self.browser and self.browser.is_connected():
return self
@@ -100,6 +126,8 @@ class CdpChannel:
headers["X-Session-Id"] = self.vnc_channel.browser_session.persistent_browser_session_id
def on_close() -> None:
+ if self._closing:
+ return
LOG.warning(
f"{self.class_name} closing because the persistent browser disconnected itself.", **self.identity
)
@@ -193,16 +221,5 @@ class CdpChannel:
LOG.exception(f"{self.class_name} failed to evaluate js", expression=expression, **self.identity)
raise
- @functools.lru_cache(maxsize=None)
def js(self, file_name: str) -> str:
- base_path = pathlib.Path(__file__).parent / "js"
- file_name = file_name.lstrip("/")
-
- if not file_name.endswith(".js"):
- file_name += ".js"
-
- relative_path = pathlib.Path(file_name)
- full_path = base_path / relative_path
-
- with open(full_path, encoding="utf-8") as f:
- return f.read()
+ return _load_js_asset(file_name)
diff --git a/skyvern/forge/sdk/routes/streaming/channels/execution.py b/skyvern/forge/sdk/routes/streaming/channels/execution.py
index ad4bad250..40763730a 100644
--- a/skyvern/forge/sdk/routes/streaming/channels/execution.py
+++ b/skyvern/forge/sdk/routes/streaming/channels/execution.py
@@ -264,6 +264,7 @@ class LocalExecutionChannel(ExecutionChannel):
self.page = page
self.pw = None
self.url = None
+ self._closing = False
@property
def identity(self) -> dict[str, t.Any]:
diff --git a/skyvern/forge/sdk/routes/streaming/channels/exfiltration.py b/skyvern/forge/sdk/routes/streaming/channels/exfiltration.py
index 76737a4cf..c1d30d328 100644
--- a/skyvern/forge/sdk/routes/streaming/channels/exfiltration.py
+++ b/skyvern/forge/sdk/routes/streaming/channels/exfiltration.py
@@ -486,6 +486,9 @@ class ExfiltrationChannel(CdpChannel):
# from skyvern.config import settings
# await super().connect(cdp_url or settings.BROWSER_REMOTE_DEBUGGING_URL)
+ if self._closing:
+ return self
+
page = self.page
if not page:
@@ -665,67 +668,76 @@ class ExfiltrationChannel(CdpChannel):
async def stop(self) -> t.Self:
LOG.info(f"{self.class_name} stopping.")
- if self._refresh_task:
- self._refresh_task.cancel()
- with contextlib.suppress(asyncio.CancelledError):
- await self._refresh_task
- self._refresh_task = None
-
- if self._network_activity_flush_task and not self._network_activity_flush_task.done():
- self._network_activity_flush_task.cancel()
- with contextlib.suppress(asyncio.CancelledError):
- await self._network_activity_flush_task
- self._network_activity_flush_task = None
-
- pending_event_tasks = list(self._pending_event_tasks)
- for task in pending_event_tasks:
- task.cancel()
- if pending_event_tasks:
- await asyncio.gather(*pending_event_tasks, return_exceptions=True)
- self._pending_event_tasks.clear()
-
- if self.cdp_session:
- try:
- await self.cdp_session.detach()
- except Exception:
- pass
-
- self.cdp_session = None
+ try:
+ if self._refresh_task:
+ self._refresh_task.cancel()
+ with contextlib.suppress(asyncio.CancelledError):
+ await self._refresh_task
+ self._refresh_task = None
+
+ if self._network_activity_flush_task and not self._network_activity_flush_task.done():
+ self._network_activity_flush_task.cancel()
+ with contextlib.suppress(asyncio.CancelledError):
+ await self._network_activity_flush_task
+ self._network_activity_flush_task = None
+
+ pending_event_tasks = list(self._pending_event_tasks)
+ for task in pending_event_tasks:
+ task.cancel()
+ if pending_event_tasks:
+ await asyncio.gather(*pending_event_tasks, return_exceptions=True)
+ self._pending_event_tasks.clear()
+
+ if self.cdp_session:
+ try:
+ await self.cdp_session.detach()
+ except Exception:
+ pass
- captures = list(self._page_console_captures.items())
- self._page_console_captures.clear()
- pages = [page for page, _ in captures]
+ self.cdp_session = None
- if self.browser_context:
- for page in self.browser_context.pages:
- if all(existing is not page for existing in pages):
- pages.append(page)
+ captures = list(self._page_console_captures.items())
+ self._page_console_captures.clear()
+ pages = [page for page, _ in captures]
- for page, capture in captures:
- try:
- page.remove_listener("console", capture.console_listener)
- except KeyError:
- pass
+ if self.browser_context:
+ for page in self.browser_context.pages:
+ if all(existing is not page for existing in pages):
+ pages.append(page)
- if capture.cdp_session:
+ for page, capture in captures:
try:
- await capture.cdp_session.detach()
- except Exception:
+ page.remove_listener("console", capture.console_listener)
+ except KeyError:
pass
- for page in pages:
- if self._active_binding_channels.get(page) is self:
- self._active_binding_channels.pop(page, None)
- self._binding_registered_pages.discard(page)
+ if capture.cdp_session:
+ try:
+ await capture.cdp_session.detach()
+ except Exception:
+ pass
- try:
- await page.evaluate("window.__skyvern_exfiltration_binding_name = null;")
- except Exception:
- LOG.debug(f"{self.class_name} failed to clear exfiltration binding name", url=page.url, exc_info=True)
+ for page in pages:
+ if self._active_binding_channels.get(page) is self:
+ self._active_binding_channels.pop(page, None)
+ self._binding_registered_pages.discard(page)
- await self.undecorate(page)
+ try:
+ await page.evaluate("window.__skyvern_exfiltration_binding_name = null;")
+ except Exception:
+ LOG.debug(
+ f"{self.class_name} failed to clear exfiltration binding name", url=page.url, exc_info=True
+ )
- self._recent_console_event_fingerprints.clear()
+ await self.undecorate(page)
+
+ self._recent_console_event_fingerprints.clear()
+ finally:
+ # Release the dedicated Playwright driver + browser graph even if page-level
+ # cleanup above raised on an already-dead page. _closing keeps this close()
+ # from tripping the browser "disconnected" callback into a reconnect.
+ self._closing = True
+ await self.close()
LOG.info(f"{self.class_name} stopped.")
diff --git a/tests/unit/forge/sdk/routes/streaming/test_exfiltration_channel.py b/tests/unit/forge/sdk/routes/streaming/test_exfiltration_channel.py
index 25bd15e18..cda7f3520 100644
--- a/tests/unit/forge/sdk/routes/streaming/test_exfiltration_channel.py
+++ b/tests/unit/forge/sdk/routes/streaming/test_exfiltration_channel.py
@@ -3,9 +3,11 @@
from __future__ import annotations
import asyncio
+import gc
import json
import typing as t
import weakref
+from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock
import pytest
@@ -470,3 +472,252 @@ class TestNavigationReExfiltration:
on_event.assert_called_once()
channel.exfiltrate.assert_not_called()
channel.adorn.assert_not_called()
+
+
+class _FakeCdpSession:
+ def __init__(self) -> None:
+ self.detached = False
+
+ async def send(self, method: str, params: dict | None = None) -> None:
+ return None
+
+ async def detach(self) -> None:
+ self.detached = True
+
+
+class _FakePwPage:
+ def __init__(self, context: _FakeContext) -> None:
+ self.url = "https://example.com"
+ self.context = context
+
+ async def add_init_script(self, *args: object, **kwargs: object) -> None:
+ return None
+
+ async def evaluate(self, *args: object, **kwargs: object) -> None:
+ return None
+
+ def on(self, *args: object, **kwargs: object) -> None:
+ return None
+
+ def remove_listener(self, *args: object, **kwargs: object) -> None:
+ return None
+
+
+class _FakeContext:
+ def __init__(self) -> None:
+ self.pages: list[_FakePwPage] = [_FakePwPage(self)]
+
+ async def new_cdp_session(self, page: object) -> _FakeCdpSession:
+ return _FakeCdpSession()
+
+
+class _FakePwBrowser:
+ """A stand-in for a Playwright Browser obtained via connect_over_cdp."""
+
+ def __init__(self, *, fire_disconnect_on_close: bool) -> None:
+ self._connected = True
+ self._fire_disconnect_on_close = fire_disconnect_on_close
+ self.handlers: dict[str, t.Callable[[], None]] = {}
+ self.context = _FakeContext()
+ self.contexts = [self.context]
+ self.close_calls = 0
+
+ def is_connected(self) -> bool:
+ return self._connected
+
+ def on(self, event: str, callback: t.Callable[[], None]) -> None:
+ self.handlers[event] = callback
+
+ async def new_browser_cdp_session(self) -> _FakeCdpSession:
+ return _FakeCdpSession()
+
+ async def new_context(self) -> _FakeContext:
+ return self.context
+
+ async def close(self) -> None:
+ self.close_calls += 1
+ self._connected = False
+ # Real playwright fires "disconnected" when a connected browser is closed;
+ # this reproduces that so the reconnect-guard can be exercised end-to-end.
+ if self._fire_disconnect_on_close:
+ handler = self.handlers.get("disconnected")
+ if handler is not None:
+ handler()
+
+
+class _FakePw:
+ def __init__(self) -> None:
+ self.stopped = False
+
+ async def stop(self) -> None:
+ self.stopped = True
+
+
+class _FakePwManager:
+ def __init__(self, on_start: t.Callable[[], _FakePw]) -> None:
+ self._on_start = on_start
+
+ async def start(self) -> _FakePw:
+ return self._on_start()
+
+
+def _patch_pw_stack(monkeypatch: pytest.MonkeyPatch, *, fire_disconnect_on_close: bool = False) -> SimpleNamespace:
+ """Replace the Playwright driver + CDP connect helper with counting fakes.
+
+ `start_calls` counts local Playwright driver spawns, so a reconnect (which starts
+ a fresh driver) is observable without launching a real Node subprocess.
+ """
+ import skyvern.forge.sdk.routes.streaming.channels.cdp as cdp_mod
+
+ state = SimpleNamespace(start_calls=0, pws=[], browsers=[])
+
+ def _make_pw() -> _FakePw:
+ state.start_calls += 1
+ pw = _FakePw()
+ state.pws.append(pw)
+ return pw
+
+ def _fake_async_playwright() -> _FakePwManager:
+ return _FakePwManager(_make_pw)
+
+ async def _fake_connect(pw: object, url: str, headers: dict | None = None) -> _FakePwBrowser:
+ browser = _FakePwBrowser(fire_disconnect_on_close=fire_disconnect_on_close)
+ state.browsers.append(browser)
+ return browser
+
+ monkeypatch.setattr(cdp_mod, "async_playwright", _fake_async_playwright)
+ monkeypatch.setattr(cdp_mod, "connect_over_cdp_with_diagnostics", _fake_connect)
+ return state
+
+
+class TestExfiltrationChannelLifecycle:
+ def test_js_asset_cache_is_keyed_by_file_not_instance(self) -> None:
+ from skyvern.forge.sdk.routes.streaming.channels.cdp import _load_js_asset
+
+ _load_js_asset.cache_clear()
+
+ channel_a, _ = _make_channel()
+ channel_b, _ = _make_channel()
+
+ assert channel_a.js("exfiltrate") == channel_b.js("exfiltrate")
+ # Two instances, one asset -> a single cache entry keyed by file name, not one
+ # entry per (self, file_name) as the old bound-method lru_cache produced.
+ assert _load_js_asset.cache_info().currsize == 1
+
+ channel_a.js("adorn")
+ assert _load_js_asset.cache_info().currsize == 2
+
+ def test_using_js_does_not_pin_channel_instance(self) -> None:
+ # A cycle-free vnc stand-in so the channel is reclaimed by refcounting the
+ # moment the JS cache stops pinning it (a MagicMock carries internal cycles).
+ vnc_channel = SimpleNamespace(identity={"client_id": "c"}, browser_session=None, x_api_key=None)
+ channel = ExfiltrationChannel(on_event=lambda _events: None, vnc_channel=vnc_channel) # type: ignore[arg-type]
+
+ channel.js("exfiltrate")
+ ref = weakref.ref(channel)
+
+ del channel
+ gc.collect()
+
+ assert ref() is None
+
+ @pytest.mark.asyncio
+ async def test_stop_releases_playwright_driver(self) -> None:
+ channel, _ = _make_channel()
+ browser = _FakePwBrowser(fire_disconnect_on_close=False)
+ pw = _FakePw()
+ channel.browser = browser # type: ignore[assignment]
+ channel.pw = pw # type: ignore[assignment]
+ channel.browser_context = browser.context # type: ignore[assignment]
+ channel.page = browser.context.pages[0] # type: ignore[assignment]
+
+ await channel.stop()
+
+ assert browser.close_calls == 1
+ assert pw.stopped is True
+ assert channel.browser is None
+ assert channel.pw is None
+
+ @pytest.mark.asyncio
+ async def test_repeated_stop_is_idempotent(self) -> None:
+ channel, _ = _make_channel()
+ browser = _FakePwBrowser(fire_disconnect_on_close=False)
+ pw = _FakePw()
+ channel.browser = browser # type: ignore[assignment]
+ channel.pw = pw # type: ignore[assignment]
+ channel.browser_context = browser.context # type: ignore[assignment]
+ channel.page = browser.context.pages[0] # type: ignore[assignment]
+
+ await channel.stop()
+ await channel.stop()
+
+ assert browser.close_calls == 1
+ assert pw.stopped is True
+ assert channel.browser is None
+ assert channel.pw is None
+
+ @pytest.mark.asyncio
+ async def test_intentional_stop_does_not_reconnect(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ state = _patch_pw_stack(monkeypatch, fire_disconnect_on_close=True)
+ channel, _ = _make_channel()
+
+ await channel.connect()
+ assert state.start_calls == 1
+
+ await channel.stop()
+ for _ in range(20):
+ await asyncio.sleep(0)
+
+ # The browser's "disconnected" event fires during the intentional close, but
+ # the guard must keep it from spawning a replacement driver.
+ assert state.start_calls == 1
+ assert state.browsers[0].close_calls >= 1
+ assert state.pws[0].stopped is True
+
+ @pytest.mark.asyncio
+ async def test_connect_is_suppressed_after_intentional_close(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ # A reconnect task scheduled by a genuine mid-recording disconnect, arriving just
+ # after teardown, must not resurrect the driver once the channel is marked closing.
+ state = _patch_pw_stack(monkeypatch, fire_disconnect_on_close=False)
+ channel, _ = _make_channel()
+
+ await channel.connect()
+ assert state.start_calls == 1
+
+ channel._closing = True
+ await channel.close()
+ await channel.connect()
+
+ assert state.start_calls == 1
+
+ @pytest.mark.asyncio
+ async def test_unexpected_disconnect_reconnects_when_not_closing(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ state = _patch_pw_stack(monkeypatch, fire_disconnect_on_close=False)
+ channel, _ = _make_channel()
+
+ await channel.connect()
+ assert state.start_calls == 1
+ on_close = state.browsers[0].handlers["disconnected"]
+
+ # An unexpected drop (channel not intentionally closed) must still reconnect.
+ on_close()
+ for _ in range(50):
+ await asyncio.sleep(0)
+ if state.start_calls >= 2:
+ break
+
+ assert state.start_calls == 2
+
+ @pytest.mark.asyncio
+ async def test_adorn_loads_asset_through_shared_cache(self) -> None:
+ from skyvern.forge.sdk.routes.streaming.channels.cdp import _load_js_asset
+
+ _load_js_asset.cache_clear()
+ channel, _ = _make_channel()
+ page = _make_page()
+
+ await channel.adorn(page)
+
+ page.evaluate.assert_awaited()
+ page.add_init_script.assert_awaited()
+ assert _load_js_asset.cache_info().currsize == 1