Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Stop a cancelled driver call being dropped un-awaited before it starts (#8891)
Shuchang Zhengc14c1e694634
diff --git a/skyvern/webeye/action_deadline.py b/skyvern/webeye/action_deadline.pyindex acba4263e..e29d0083c 100644--- a/skyvern/webeye/action_deadline.py+++ b/skyvern/webeye/action_deadline.py@@ -74,10 +74,22 @@ async def cancellable_driver_call(call: Callable[[], Awaitable[T]]) -> T:call_task = asyncio.create_task(issue(), context=context)call_task.add_done_callback(_retrieve_outcome)try:- return await call_task+ # Not ``await call_task``: that hands the caller's cancellation to a task that may not have taken its first+ # step, and the OTel asyncio instrumentor drops such a task's coroutine, which CPython reports as a+ # "never awaited" RuntimeWarning at error level.+ await asyncio.wait({call_task})+ return call_task.result()except asyncio.CancelledError:- # A cancellation that arrives after the call has answered never reached the task.- call_task.cancel()+ # asyncio.wait does not forward cancellation. Cancel the call once it has had a chance to start, and again+ # for every later cancellation (a deadline re-arm), as awaiting the task directly did; return only once it ends.+ while True:+ try:+ await asyncio.sleep(0)+ call_task.cancel()+ await asyncio.wait({call_task})+ break+ except asyncio.CancelledError:+ continueraisefinally:# The task ran in a copy of this context, so a write inside the call would otherwise be lost.diff --git a/skyvern/webeye/actions/handler.py b/skyvern/webeye/actions/handler.pyindex 92063c06d..8109233ec 100644--- a/skyvern/webeye/actions/handler.py+++ b/skyvern/webeye/actions/handler.py@@ -532,7 +532,12 @@ class _DownloadProgressSampler:async def aclose(self) -> None:if self._task is not None:- self._task.cancel()+ # A task cancelled before its first step is dropped un-awaited by the OTel asyncio instrumentor, which logs a+ # "never awaited" RuntimeWarning at error level; yield once so it starts first.+ try:+ await asyncio.sleep(0)+ finally:+ self._task.cancel()await asyncio.gather(self._task, return_exceptions=True)diff --git a/tests/unit/test_download_file_action_handler.py b/tests/unit/test_download_file_action_handler.pyindex 11350de0c..78504cbed 100644--- a/tests/unit/test_download_file_action_handler.py+++ b/tests/unit/test_download_file_action_handler.py@@ -1,10 +1,12 @@import asyncioimport contextlib+import gcimport ioimport loggingimport osimport tempfileimport time+import warningsfrom collections.abc import Callable, Iteratorfrom datetime import UTC, datetimefrom pathlib import Path@@ -18,7 +20,7 @@ from pydantic import BaseModel, ValidationErrorfrom structlog.testing import capture_logsfrom skyvern.config import settings-from skyvern.constants import BROWSER_DOWNLOAD_TIMEOUT+from skyvern.constants import BROWSER_DOWNLOAD_TIMEOUT, BROWSER_DOWNLOADING_SUFFIXfrom skyvern.exceptions import BlockedHostfrom skyvern.forge.agent import ForgeAgentfrom skyvern.forge.sdk.core.skyvern_context import SkyvernContext@@ -39,6 +41,7 @@ from skyvern.webeye.actions.handler import (_browser_dispatch_committed,_cleanup_captured_download_popup,_collect_inline_iframe_src_candidates,+ _DownloadProgressSampler,_EagerAdoptedBlobCapture,_looks_like_pdf,_PageDeltaBaseline,@@ -7871,3 +7874,54 @@ async def test_provider_poll_cancellation_propagates_without_recording_error() -# not report a measured negative it never actually observed.assert logs[-1]["materialized_file_delta"] is Nonelist_signal_files.assert_not_awaited()+++async def test_download_progress_sampler_closed_before_its_first_step_is_not_orphaned(tmp_path: Path) -> None:+ """A download that finishes without suspending closes the sampler in the event-loop iteration it was+ started in. Under the OTel asyncio instrumentor that cancel dropped the polling coroutine un-awaited,+ which reaches the logs as an error-tier "never awaited" RuntimeWarning on every such download.+ """+ # The instrumentor ships in the cloud dependency group only, so OSS CI skips this test.+ instrumentation = pytest.importorskip("opentelemetry.instrumentation.asyncio")+ partial = tmp_path / f"download{BROWSER_DOWNLOADING_SUFFIX}"+ partial.write_bytes(b"x")+ sampler = _DownloadProgressSampler(tmp_path, None, set())++ instrumentor = instrumentation.AsyncioInstrumentor()+ instrumentor.instrument()+ try:+ with warnings.catch_warnings(record=True) as caught:+ warnings.simplefilter("always")+ sampler.start_if_partial_seen([str(partial)])+ await sampler.aclose()+ # The sampler still references its cancelled task; the warning is raised only when that is collected.+ del sampler+ for _ in range(5):+ await asyncio.sleep(0)+ gc.collect()+ await asyncio.sleep(0)+ finally:+ instrumentor.uninstrument()++ assert [str(entry.message) for entry in caught if "never awaited" in str(entry.message)] == []+++async def test_download_progress_sampler_cancelled_while_closing_still_stops_its_task(tmp_path: Path) -> None:+ """A second cancellation landing while aclose() yields must not leave the polling task running forever."""+ partial = tmp_path / f"download{BROWSER_DOWNLOADING_SUFFIX}"+ partial.write_bytes(b"x")+ sampler = _DownloadProgressSampler(tmp_path, None, set())+ sampler.start_if_partial_seen([str(partial)])+ polling = sampler._task+ assert polling is not None++ closing = asyncio.create_task(sampler.aclose())+ await asyncio.sleep(0)+ closing.cancel()+ with pytest.raises(asyncio.CancelledError):+ await closing+ await asyncio.sleep(0)++ assert polling.cancelled()diff --git a/tests/unit/webeye/test_action_deadline.py b/tests/unit/webeye/test_action_deadline.pynew file mode 100644index 000000000..a165ad2e7--- /dev/null+++ b/tests/unit/webeye/test_action_deadline.py@@ -0,0 +1,66 @@+from __future__ import annotations++import asyncio+import gc+import warnings++import pytest++from skyvern.exceptions import ActionDeadlineExceeded+from skyvern.webeye import action_deadline+from skyvern.webeye.action_deadline import cancellable_driver_call+++async def test_a_caller_cancelled_before_the_call_takes_its_first_step_still_starts_it() -> None:+ """The OTel asyncio instrumentor (cloud workers) drops the coroutine of a task cancelled before its first step,+ and CPython reports that as an error-level "never awaited" warning on every such cancellation."""+ entered = asyncio.Event()++ async def call() -> None:+ entered.set()+ await asyncio.sleep(30)++ instrumentation = pytest.importorskip("opentelemetry.instrumentation.asyncio")+ instrumentor = instrumentation.AsyncioInstrumentor()+ instrumentor.instrument()+ try:+ with warnings.catch_warnings(record=True) as caught:+ warnings.simplefilter("always")+ with pytest.raises(TimeoutError):+ async with asyncio.timeout(0):+ await cancellable_driver_call(call)+ for _ in range(5):+ await asyncio.sleep(0)+ gc.collect()+ await asyncio.sleep(0)+ finally:+ instrumentor.uninstrument()++ assert [str(entry.message) for entry in caught if "never awaited" in str(entry.message)] == []+ assert entered.is_set()+++async def test_each_deadline_rearm_reaches_a_call_that_ignored_the_first_cancellation(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ monkeypatch.setattr(action_deadline, "DEADLINE_REARM_INTERVAL_SECONDS", 0.05)+ monkeypatch.setattr(action_deadline, "ACTION_DEADLINE_HEADROOM_MS", 0)+ cancels = 0++ async def call() -> None:+ nonlocal cancels+ while True:+ try:+ await asyncio.sleep(3600)+ except asyncio.CancelledError:+ cancels += 1+ if cancels > 1:+ raise++ with pytest.raises(ActionDeadlineExceeded):+ async with action_deadline.under_action_deadline(budget_ms=50):+ await cancellable_driver_call(call)++ assert cancels == 2