Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Route download.path() awaiters through shielded resolve_artifact_path (SKY-12982) (#7829)
Aaron Perez88a702a5bc55
diff --git a/skyvern/webeye/actions/handler.py b/skyvern/webeye/actions/handler.pyindex b52f2958d..012f7f76e 100644--- a/skyvern/webeye/actions/handler.py+++ b/skyvern/webeye/actions/handler.py@@ -143,7 +143,7 @@ from skyvern.webeye.actions.actions import ()from skyvern.webeye.actions.responses import ActionAbort, ActionFailure, ActionResult, ActionSuccessfrom skyvern.webeye.browser_engine import BrowserEngineSelection-from skyvern.webeye.browser_factory import initialize_download_dir+from skyvern.webeye.browser_factory import initialize_download_dir, resolve_artifact_pathfrom skyvern.webeye.browser_state import BrowserStatefrom skyvern.webeye.cdp_download_interceptor import (DOWNLOAD_MIME_TYPES,@@ -829,7 +829,7 @@ async def _persist_captured_download(return _CapturedDownloadPersistence(None, "download_failed")if target is None:try:- local_path_value = await download.path()+ local_path_value = await resolve_artifact_path(download, timeout)if local_path_value and (local_path := Path(local_path_value)).is_file():if local_path.stat().st_size:return _CapturedDownloadPersistence(local_path, "local_path")diff --git a/skyvern/webeye/browser_factory.py b/skyvern/webeye/browser_factory.pyindex 31be9621c..74032957c 100644--- a/skyvern/webeye/browser_factory.py+++ b/skyvern/webeye/browser_factory.py@@ -178,9 +178,15 @@ def set_browser_console_log(browser_context: BrowserContext, browser_artifacts:browser_context.on("console", browser_console_log)-async def resolve_video_path(video: Video, timeout_seconds: float) -> str | None:- """Wait for video.path() without ever cancelling patchright's shared artifact future"""- path_task = asyncio.ensure_future(video.path())+async def resolve_artifact_path(source: Video | Download, timeout_seconds: float) -> str | None:+ """Await source.path() without ever cancelling a shared artifact future another awaiter may hold.++ Patchright's Video resolves path() from one future shared across awaiters, so a bare+ timeout-cancel on any awaiter poisons it for the others (SKY-12852 / SKY-12860). Download.path()+ resolves via a fresh per-call future today and is not exposed, but is routed here too so a pin+ bump that reintroduces a shared Download future cannot revive that orphan-hang class.+ """+ path_task = asyncio.ensure_future(source.path())# Consume the task's eventual outcome even when it outlives this wait (timeout / caller# cancelled), so a late PlaywrightError on page close doesn't log "Task exception was# never retrieved".@@ -214,7 +220,7 @@ def set_popup_video_listener(browser_context: BrowserContext, browser_artifacts:video = page.videoif not video:return- video_path_or_none = await resolve_video_path(video, settings.POPUP_VIDEO_PATH_TIMEOUT_SECONDS)+ video_path_or_none = await resolve_artifact_path(video, settings.POPUP_VIDEO_PATH_TIMEOUT_SECONDS)if video_path_or_none is None:try:page_origin = urlparse(page.url).hostname or "unknown"@@ -264,72 +270,76 @@ def set_download_file_listener(workflow_run_id = (context.workflow_run_id if context else None) or kwargs.get("workflow_run_id")task_id = (context.task_id if context else None) or kwargs.get("task_id")try:- async with asyncio.timeout(download_timeout or BROWSER_DOWNLOAD_TIMEOUT):- file_path = await download.path()- if not file_path.exists():- # On an adopted persistent session the bytes live on the run connection, not- # this worker connection; saving is the run side's job, so skip rather than crash.- LOG.debug(- "Download artifact absent on this connection; skipping worker-side rename",- workflow_run_id=workflow_run_id,- task_id=task_id,- suggested_filename=download.suggested_filename,- )- return- if file_path.suffix:- return-- LOG.info(- "No file extensions, going to add file extension automatically",+ # Route path() through resolve_artifact_path so a timeout can never cancel a shared+ # artifact future (see resolve_artifact_path). Everything after the await is synchronous+ # filesystem work, so it does not need to run under a timeout.+ resolved_path = await resolve_artifact_path(download, download_timeout or BROWSER_DOWNLOAD_TIMEOUT)+ if resolved_path is None:+ LOG.error(+ "timeout to download file, going to cancel the download",+ workflow_run_id=workflow_run_id,+ task_id=task_id,+ )+ await download.cancel()+ return+ file_path = Path(resolved_path)+ if not file_path.exists():+ # On an adopted persistent session the bytes live on the run connection, not+ # this worker connection; saving is the run side's job, so skip rather than crash.+ LOG.debug(+ "Download artifact absent on this connection; skipping worker-side rename",workflow_run_id=workflow_run_id,task_id=task_id,suggested_filename=download.suggested_filename,- url=_redact_url_query(download.url),)- suffix = Path(download.suggested_filename).suffix- if suffix:- LOG.info(- "Add extension according to suggested filename",- workflow_run_id=workflow_run_id,- task_id=task_id,- filepath=str(file_path) + suffix,- )- file_path.rename(str(file_path) + suffix)- return+ return+ if file_path.suffix:+ return- parsed_url = urlparse(download.url)- parsed_qs = parse_qsl(parsed_url.query)- for key, value in parsed_qs:- if key.lower() == "filename":- suffix = Path(value).suffix- if suffix:- LOG.info(- "Add extension according to the parsed query params of download url",- workflow_run_id=workflow_run_id,- task_id=task_id,- filename=value,- )- file_path.rename(str(file_path) + suffix)- return-- suffix = Path(parsed_url.path).suffix- if suffix:- LOG.info(- "Add extension according to download url path",- workflow_run_id=workflow_run_id,- task_id=task_id,- filepath=str(file_path) + suffix,- )- file_path.rename(str(file_path) + suffix)- return- # TODO: maybe should try to parse it from URL response- except asyncio.TimeoutError:- LOG.error(- "timeout to download file, going to cancel the download",+ LOG.info(+ "No file extensions, going to add file extension automatically",workflow_run_id=workflow_run_id,task_id=task_id,+ suggested_filename=download.suggested_filename,+ url=_redact_url_query(download.url),)- await download.cancel()+ suffix = Path(download.suggested_filename).suffix+ if suffix:+ LOG.info(+ "Add extension according to suggested filename",+ workflow_run_id=workflow_run_id,+ task_id=task_id,+ filepath=str(file_path) + suffix,+ )+ file_path.rename(str(file_path) + suffix)+ return++ parsed_url = urlparse(download.url)+ parsed_qs = parse_qsl(parsed_url.query)+ for key, value in parsed_qs:+ if key.lower() == "filename":+ suffix = Path(value).suffix+ if suffix:+ LOG.info(+ "Add extension according to the parsed query params of download url",+ workflow_run_id=workflow_run_id,+ task_id=task_id,+ filename=value,+ )+ file_path.rename(str(file_path) + suffix)+ return++ suffix = Path(parsed_url.path).suffix+ if suffix:+ LOG.info(+ "Add extension according to download url path",+ workflow_run_id=workflow_run_id,+ task_id=task_id,+ filepath=str(file_path) + suffix,+ )+ file_path.rename(str(file_path) + suffix)+ return+ # TODO: maybe should try to parse it from URL responseexcept Exception:LOG.exception(diff --git a/skyvern/webeye/real_browser_state.py b/skyvern/webeye/real_browser_state.pyindex db9f4c5a5..f9e128032 100644--- a/skyvern/webeye/real_browser_state.py+++ b/skyvern/webeye/real_browser_state.py@@ -29,7 +29,7 @@ from skyvern.forge.sdk.trace import tracedfrom skyvern.schemas.runs import ProxyLocationInputfrom skyvern.webeye.browser_artifacts import BrowserArtifactsfrom skyvern.webeye.browser_engine import BrowserEngineSelection-from skyvern.webeye.browser_factory import BrowserCleanupFunc, BrowserContextFactory, resolve_video_path+from skyvern.webeye.browser_factory import BrowserCleanupFunc, BrowserContextFactory, resolve_artifact_pathfrom skyvern.webeye.browser_state import BrowserStatefrom skyvern.webeye.cdp_download_interceptor import disable_download_interceptor_for_contextfrom skyvern.webeye.navigation import is_permanent_navigation_error, navigate_with_retry@@ -150,7 +150,7 @@ class RealBrowserState(BrowserState):except Exception:passtry:- path = await resolve_video_path(video, settings.POPUP_VIDEO_PATH_TIMEOUT_SECONDS)+ path = await resolve_artifact_path(video, settings.POPUP_VIDEO_PATH_TIMEOUT_SECONDS)except Exception:LOG.warning("Could not get video path to discard orphaned artifact", page_origin=page_origin, exc_info=True)returndiff --git a/tests/unit/test_download_dir_rebind.py b/tests/unit/test_download_dir_rebind.pyindex 28e1307e0..41e4cdbc0 100644--- a/tests/unit/test_download_dir_rebind.py+++ b/tests/unit/test_download_dir_rebind.py@@ -6,6 +6,7 @@ the dir must be rebound to downloads/<workflow_run_id>/ so downloads landrun-scoped and the listener logs the real run identity."""+from pathlib import Pathfrom types import SimpleNamespacefrom unittest.mock import AsyncMock, MagicMock, patch@@ -236,18 +237,18 @@ async def test_apply_download_behaviour_falls_back_to_workflow_run_id() -> None:@pytest.mark.asyncio-async def test_listener_logs_run_identity_from_context() -> None:+async def test_listener_logs_run_identity_from_context(tmp_path: Path) -> None:captured: dict[str, object] = {}def capture_log(_msg: str, **kwargs: object) -> None:captured.update(kwargs)+ extensionless_file = tmp_path / "statement"+ extensionless_file.write_bytes(b"data")download = MagicMock()- download.suggested_filename = "statement.pdf"+ download.suggested_filename = "statement"download.url = "https://example.com/d"- path = MagicMock()- path.suffix = ""- download.path = AsyncMock(return_value=path)+ download.path = AsyncMock(return_value=extensionless_file)captured_handler: dict[str, object] = {}@@ -277,18 +278,18 @@ async def test_listener_logs_run_identity_from_context() -> None:@pytest.mark.asyncio-async def test_listener_falls_back_to_kwargs_without_context() -> None:+async def test_listener_falls_back_to_kwargs_without_context(tmp_path: Path) -> None:captured: dict[str, object] = {}def capture_log(_msg: str, **kwargs: object) -> None:captured.update(kwargs)+ extensionless_file = tmp_path / "statement"+ extensionless_file.write_bytes(b"data")download = MagicMock()- download.suggested_filename = "statement.pdf"+ download.suggested_filename = "statement"download.url = "https://example.com/d"- path = MagicMock()- path.suffix = ""- download.path = AsyncMock(return_value=path)+ download.path = AsyncMock(return_value=extensionless_file)captured_handler: dict[str, object] = {}browser_context = MagicMock()diff --git a/tests/unit/webeye/test_browser_factory_video_path.py b/tests/unit/webeye/test_browser_factory_artifact_path.pysimilarity index 50%rename from tests/unit/webeye/test_browser_factory_video_path.pyrename to tests/unit/webeye/test_browser_factory_artifact_path.pyindex 4ff0e158f..c2c818d5f 100644--- a/tests/unit/webeye/test_browser_factory_video_path.py+++ b/tests/unit/webeye/test_browser_factory_artifact_path.py@@ -1,8 +1,9 @@import asyncio+from pathlib import Pathimport pytest-from skyvern.webeye.browser_factory import resolve_video_path+from skyvern.webeye.browser_factory import resolve_artifact_pathclass _FakeVideo:@@ -15,19 +16,31 @@ class _FakeVideo:return await self._artifact_future+class _FakeSharedFutureDownload:+ """Mirrors the hazard resolve_artifact_path future-proofs against: a Download whose path()+ awaits a single shared artifact future (as a pin bump could reintroduce). path() returns a+ pathlib.Path, like the real Download."""++ def __init__(self, artifact_future: asyncio.Future) -> None:+ self._artifact_future = artifact_future++ async def path(self) -> Path:+ return await self._artifact_future++@pytest.mark.asyncioasync def test_returns_path_when_future_resolves_in_time() -> None:fut: asyncio.Future = asyncio.get_running_loop().create_future()fut.set_result("/videos/a.webm")- assert await resolve_video_path(_FakeVideo(fut), timeout_seconds=1) == "/videos/a.webm"+ assert await resolve_artifact_path(_FakeVideo(fut), timeout_seconds=1) == "/videos/a.webm"@pytest.mark.asyncioasync def test_timeout_returns_none_without_cancelling_shared_future() -> None:fut: asyncio.Future = asyncio.get_running_loop().create_future()- result = await resolve_video_path(_FakeVideo(fut), timeout_seconds=0.05)+ result = await resolve_artifact_path(_FakeVideo(fut), timeout_seconds=0.05)assert result is Noneassert not fut.cancelled()@@ -41,7 +54,7 @@ async def test_poisoned_future_returns_none_instead_of_cancelling_caller() -> Nofut: asyncio.Future = asyncio.get_running_loop().create_future()fut.cancel()- caller = asyncio.ensure_future(resolve_video_path(_FakeVideo(fut), timeout_seconds=1))+ caller = asyncio.ensure_future(resolve_artifact_path(_FakeVideo(fut), timeout_seconds=1))result = await asyncio.wait_for(caller, timeout=1)assert result is None@@ -55,8 +68,8 @@ async def test_concurrent_waiter_timeout_does_not_cancel_other_caller() -> None:fut: asyncio.Future = asyncio.get_running_loop().create_future()video = _FakeVideo(fut)- listener = asyncio.ensure_future(resolve_video_path(video, timeout_seconds=0.05))- caller = asyncio.ensure_future(resolve_video_path(video, timeout_seconds=5))+ listener = asyncio.ensure_future(resolve_artifact_path(video, timeout_seconds=0.05))+ caller = asyncio.ensure_future(resolve_artifact_path(video, timeout_seconds=5))assert await listener is Noneawait asyncio.sleep(0) # let any stray cancellation propagate@@ -82,7 +95,7 @@ async def test_poisoned_future_returns_none_with_stale_cancel_count() -> None:except asyncio.CancelledError:passassert current.cancelling() > 0- return await resolve_video_path(_FakeVideo(fut), timeout_seconds=1)+ return await resolve_artifact_path(_FakeVideo(fut), timeout_seconds=1)caller = asyncio.ensure_future(caller_with_stale_cancel_count())assert await asyncio.wait_for(caller, timeout=1) is None@@ -93,10 +106,49 @@ async def test_poisoned_future_returns_none_with_stale_cancel_count() -> None:async def test_real_task_cancellation_still_propagates() -> None:fut: asyncio.Future = asyncio.get_running_loop().create_future()- caller = asyncio.ensure_future(resolve_video_path(_FakeVideo(fut), timeout_seconds=5))+ caller = asyncio.ensure_future(resolve_artifact_path(_FakeVideo(fut), timeout_seconds=5))await asyncio.sleep(0.01)caller.cancel()with pytest.raises(asyncio.CancelledError):await callerassert caller.cancelled()+++async def test_download_path_object_is_stringified() -> None:+ fut: asyncio.Future = asyncio.get_running_loop().create_future()+ fut.set_result(Path("/downloads/report"))++ assert await resolve_artifact_path(_FakeSharedFutureDownload(fut), timeout_seconds=1) == "/downloads/report"+++async def test_download_concurrent_waiter_timeout_does_not_cancel_other_caller() -> None:+ """Pin-bump regression guard: if Download ever awaits a shared artifact future (as Video did),+ the download listener timing out must not poison the future for _persist_captured_download's+ awaiter (the SKY-12860 orphan-hang class returning via downloads)."""+ fut: asyncio.Future = asyncio.get_running_loop().create_future()+ download = _FakeSharedFutureDownload(fut)++ listener = asyncio.ensure_future(resolve_artifact_path(download, timeout_seconds=0.05))+ caller = asyncio.ensure_future(resolve_artifact_path(download, timeout_seconds=5))++ assert await listener is None+ await asyncio.sleep(0) # let any stray cancellation propagate+ fut.set_result(Path("/downloads/report.pdf"))++ assert await asyncio.wait_for(caller, timeout=1) == "/downloads/report.pdf"+ assert not caller.cancelled()+++async def test_download_poisoned_future_returns_none_instead_of_cancelling_caller() -> None:+ fut: asyncio.Future = asyncio.get_running_loop().create_future()+ fut.cancel()++ caller = asyncio.ensure_future(resolve_artifact_path(_FakeSharedFutureDownload(fut), timeout_seconds=1))+ result = await asyncio.wait_for(caller, timeout=1)++ assert result is None+ assert not caller.cancelled()