Skip to content

Automate browser based workflows with AI

fix(SKY-12530): don't remove shared workflow-run context on SDK run_action cleanup (#7545)

Aaron Perez05041750619f
diff --git a/skyvern/forge/sdk/routes/sdk.py b/skyvern/forge/sdk/routes/sdk.py
index 9dce33147..977259e8b 100644
--- a/skyvern/forge/sdk/routes/sdk.py
+++ b/skyvern/forge/sdk/routes/sdk.py
@@ -54,6 +54,7 @@ async def run_sdk_action(
action = action_request.action
# Use existing workflow_run_id if provided, otherwise create a new one
+ created_workflow_run = not action_request.workflow_run_id
if action_request.workflow_run_id:
workflow_run = await app.DATABASE.workflow_runs.get_workflow_run(
workflow_run_id=action_request.workflow_run_id,
@@ -273,7 +274,11 @@ async def run_sdk_action(
await app.ARTIFACT_MANAGER.wait_for_upload_aiotasks([task.task_id])
except Exception:
LOG.warning("Failed to drain artifact uploads for SDK action", task_id=task.task_id, exc_info=True)
- app.WORKFLOW_CONTEXT_MANAGER.remove_workflow_run_context(workflow_run.workflow_run_id)
+ # Only tear down a run we created. A client-provided workflow_run_id is shared across
+ # concurrent run_action calls on this process-global manager, so removing it here yanks the
+ # context out from under an in-flight sibling call.
+ if created_workflow_run:
+ app.WORKFLOW_CONTEXT_MANAGER.remove_workflow_run_context(workflow_run.workflow_run_id)
skyvern_context.reset()
return RunSdkActionResponse(
diff --git a/skyvern/webeye/actions/handler.py b/skyvern/webeye/actions/handler.py
index 1098d5295..5c98ef214 100644
--- a/skyvern/webeye/actions/handler.py
+++ b/skyvern/webeye/actions/handler.py
@@ -166,6 +166,7 @@ from skyvern.webeye.utils.dom import (
InteractiveElement,
SkyvernElement,
SkyvernOptionType,
+ is_incompatible_text_input_error,
is_post_dispatch_click_timeout,
)
from skyvern.webeye.utils.page import SkyvernFrame
@@ -9721,11 +9722,21 @@ async def click_listbox_option(
async def get_input_value(tag_name: str, locator: Locator) -> str | None:
- if tag_name in COMMON_INPUT_TAGS:
- return await locator.input_value()
- # for span, div, p or other tags:
- # we need to trim the unicode space for these tags
- return (await locator.inner_text()).replace("\xa0", " ").strip()
+ # input_value() rejects non-<input>/<textarea>/<select> nodes and inner_text() rejects
+ # non-HTMLElement nodes; the live node can disagree with the scraped tag_name after a
+ # re-render. Treat an incompatible read as "value unknown" so the caller's own
+ # element-type classification runs instead of a raw Playwright exception escaping here.
+ try:
+ if tag_name in COMMON_INPUT_TAGS:
+ return await locator.input_value()
+ # for span, div, p or other tags:
+ # we need to trim the unicode space for these tags
+ return (await locator.inner_text()).replace("\xa0", " ").strip()
+ except PlaywrightError as exc:
+ if is_incompatible_text_input_error(exc):
+ LOG.info("Skipping value read on an incompatible element", tag_name=tag_name, error=str(exc))
+ return None
+ raise
class AbstractActionForContextParse(BaseModel):
diff --git a/skyvern/webeye/utils/dom.py b/skyvern/webeye/utils/dom.py
index 4c56918db..b711f7664 100644
--- a/skyvern/webeye/utils/dom.py
+++ b/skyvern/webeye/utils/dom.py
@@ -8,7 +8,18 @@ from random import uniform
from urllib.parse import urljoin, urlparse
import structlog
-from playwright.async_api import ElementHandle, FloatRect, Frame, FrameLocator, Locator, Page, TimeoutError
+from playwright.async_api import (
+ ElementHandle,
+)
+from playwright.async_api import Error as PlaywrightError
+from playwright.async_api import (
+ FloatRect,
+ Frame,
+ FrameLocator,
+ Locator,
+ Page,
+ TimeoutError,
+)
from skyvern.config import settings
from skyvern.constants import SKYVERN_ID_ATTR
@@ -16,6 +27,7 @@ from skyvern.exceptions import (
ElementIsNotLabel,
ElementOutOfCurrentViewport,
InteractWithDisabledElement,
+ InvalidElementForTextInput,
MissingElement,
MissingElementDict,
MissingElementInCSSMap,
@@ -36,6 +48,14 @@ LOG = structlog.get_logger()
COMMON_INPUT_TAGS = {"input", "textarea", "select"}
+def is_incompatible_text_input_error(exc: BaseException) -> bool:
+ # Playwright raises "Element is not an <input>, <textarea> or [contenteditable] element"
+ # from fill()/clear() when the resolved node can't accept text (button, link, span,
+ # dialog/container, iframe). The static tag_name can disagree with the live node after
+ # a re-render, so callers relying on the tag alone still reach these APIs.
+ return "is not an" in str(exc).lower()
+
+
def is_post_dispatch_click_timeout(exc: BaseException) -> bool:
"""A Playwright `TimeoutError` whose message references the post-click
auto-wait for scheduled navigations means the click was physically
@@ -939,11 +959,21 @@ class SkyvernElement:
await self.input_sequentially(text=text, default_timeout=timeout)
async def input_fill(self, text: str, timeout: float = settings.BROWSER_ACTION_TIMEOUT_MS) -> None:
- await self.get_locator().fill(text, timeout=timeout)
+ try:
+ await self.get_locator().fill(text, timeout=timeout)
+ except PlaywrightError as exc:
+ if is_incompatible_text_input_error(exc):
+ raise InvalidElementForTextInput(element_id=self.get_id(), tag_name=self.get_tag_name())
+ raise
async def input_clear(self, timeout: float = settings.BROWSER_ACTION_TIMEOUT_MS) -> None:
locator = self.get_locator()
- await EventStrategyFactory.clear_field(locator.page, locator, char_count=0)
+ try:
+ await EventStrategyFactory.clear_field(locator.page, locator, char_count=0)
+ except PlaywrightError as exc:
+ if is_incompatible_text_input_error(exc):
+ raise InvalidElementForTextInput(element_id=self.get_id(), tag_name=self.get_tag_name())
+ raise
async def check(
self,
diff --git a/tests/unit/test_incompatible_element_guards.py b/tests/unit/test_incompatible_element_guards.py
new file mode 100644
index 000000000..b12c9157a
--- /dev/null
+++ b/tests/unit/test_incompatible_element_guards.py
@@ -0,0 +1,78 @@
+from __future__ import annotations
+
+from unittest.mock import AsyncMock, MagicMock
+
+import pytest
+from playwright.async_api import Error as PlaywrightError
+
+from skyvern.exceptions import InvalidElementForTextInput
+from skyvern.webeye.actions.handler import get_input_value
+from skyvern.webeye.utils.dom import SkyvernElement, is_incompatible_text_input_error
+
+_NOT_INPUT_ERROR = "Element is not an <input>, <textarea> or [contenteditable] element"
+_NOT_HTMLELEMENT_ERROR = "Node is not an HTMLElement"
+_NOT_INPUTELEMENT_ERROR = "Node is not an HTMLInputElement, HTMLTextAreaElement or HTMLSelectElement"
+_TIMEOUT_ERROR = "Timeout 30000ms exceeded"
+
+
+def _make_element(tag_name: str, locator: MagicMock) -> SkyvernElement:
+ return SkyvernElement(locator, MagicMock(), {"id": "el-1", "tagName": tag_name})
+
+
+def test_predicate_matches_incompatible_type_errors() -> None:
+ assert is_incompatible_text_input_error(PlaywrightError(_NOT_INPUT_ERROR)) is True
+ assert is_incompatible_text_input_error(PlaywrightError(_NOT_HTMLELEMENT_ERROR)) is True
+ assert is_incompatible_text_input_error(PlaywrightError(_NOT_INPUTELEMENT_ERROR)) is True
+ assert is_incompatible_text_input_error(PlaywrightError(_TIMEOUT_ERROR)) is False
+
+
+async def test_get_input_value_returns_none_when_input_value_rejects_node() -> None:
+ locator = MagicMock()
+ locator.input_value = AsyncMock(side_effect=PlaywrightError(_NOT_INPUTELEMENT_ERROR))
+ assert await get_input_value("input", locator) is None
+
+
+async def test_get_input_value_returns_none_when_inner_text_rejects_node() -> None:
+ locator = MagicMock()
+ locator.inner_text = AsyncMock(side_effect=PlaywrightError(_NOT_HTMLELEMENT_ERROR))
+ assert await get_input_value("svg", locator) is None
+
+
+async def test_get_input_value_reraises_unrelated_errors() -> None:
+ locator = MagicMock()
+ locator.input_value = AsyncMock(side_effect=PlaywrightError(_TIMEOUT_ERROR))
+ with pytest.raises(PlaywrightError):
+ await get_input_value("input", locator)
+
+
+async def test_input_fill_classifies_incompatible_element() -> None:
+ locator = MagicMock()
+ locator.fill = AsyncMock(side_effect=PlaywrightError(_NOT_INPUT_ERROR))
+ element = _make_element("a", locator)
+ with pytest.raises(InvalidElementForTextInput):
+ await element.input_fill("hello")
+
+
+async def test_input_fill_reraises_unrelated_errors() -> None:
+ locator = MagicMock()
+ locator.fill = AsyncMock(side_effect=PlaywrightError(_TIMEOUT_ERROR))
+ element = _make_element("input", locator)
+ with pytest.raises(PlaywrightError):
+ await element.input_fill("hello")
+
+
+async def test_input_clear_classifies_incompatible_element(monkeypatch: pytest.MonkeyPatch) -> None:
+ element = _make_element("button", MagicMock())
+
+ async def _raise(*args: object, **kwargs: object) -> None:
+ raise PlaywrightError(_NOT_INPUT_ERROR)
+
+ monkeypatch.setattr("skyvern.webeye.utils.dom.EventStrategyFactory.clear_field", _raise)
+ with pytest.raises(InvalidElementForTextInput):
+ await element.input_clear()
diff --git a/tests/unit/test_run_sdk_action_failures.py b/tests/unit/test_run_sdk_action_failures.py
index d63fdc211..f96a3f267 100644
--- a/tests/unit/test_run_sdk_action_failures.py
+++ b/tests/unit/test_run_sdk_action_failures.py
@@ -131,3 +131,59 @@ async def test_handler_propagates_unknown_exception(mock_request: Any, mock_orga
await run_sdk_action(mock_request, organization=mock_organization)
mock_app.DATABASE.tasks.update_task.assert_awaited()
+
+
+async def test_client_provided_workflow_run_context_is_not_removed_on_cleanup(
+ mock_request: Any, mock_organization: Any, mock_app: Any
+) -> None:
+ # A client-provided workflow_run_id is shared across concurrent run_action calls; the
+ # process-global context manager is keyed by that id, so tearing it down in this request's
+ # cleanup yanks the context out from under an in-flight sibling call, which then raises
+ # WorkflowRunContextNotInitialized in _render_template_with_label.
+ mock_request.workflow_run_id = "wr_shared"
+ mock_app.DATABASE.workflow_runs.get_workflow_run = AsyncMock(
+ return_value=MagicMock(workflow_run_id="wr_shared", workflow_id="w_test")
+ )
+ with (
+ patch("skyvern.forge.sdk.routes.sdk.app", mock_app),
+ patch("skyvern.forge.sdk.routes.sdk.skyvern_context") as mock_ctx,
+ patch(
+ "skyvern.core.script_generations.script_skyvern_page.ScriptSkyvernPage.create_scraped_page",
+ new_callable=AsyncMock,
+ side_effect=RuntimeError("stop inside try"),
+ ),
+ ):
+ mock_ctx.ensure_context.return_value = MagicMock(request_id="req_test", tz_info=None, prompt=None)
+ with pytest.raises(RuntimeError, match="stop inside try"):
+ await run_sdk_action(mock_request, organization=mock_organization)
+
+ mock_app.WORKFLOW_CONTEXT_MANAGER.remove_workflow_run_context.assert_not_called()
+
+
+async def test_owned_workflow_run_context_is_removed_on_cleanup(
+ mock_request: Any, mock_organization: Any, mock_app: Any
+) -> None:
+ # A workflow_run we created ourselves is unique to this request, so removing it in cleanup
+ # both prevents the SKY-12524 leak and is safe (no sibling call can share the id).
+ mock_request.workflow_run_id = None
+ workflow = MagicMock(workflow_id="w_test", workflow_permanent_id="wpid_test", title="t")
+ workflow_run = MagicMock(workflow_run_id="wr_owned", workflow_id="w_test")
+ mock_app.WORKFLOW_SERVICE.create_empty_workflow = AsyncMock(return_value=workflow)
+ mock_app.WORKFLOW_SERVICE.setup_workflow_run = AsyncMock(return_value=workflow_run)
+ mock_app.WORKFLOW_SERVICE.mark_workflow_run_as_completed = AsyncMock(return_value=workflow_run)
+ with (
+ patch("skyvern.forge.sdk.routes.sdk.app", mock_app),
+ patch("skyvern.forge.sdk.routes.sdk.skyvern_context") as mock_ctx,
+ patch(
+ "skyvern.core.script_generations.script_skyvern_page.ScriptSkyvernPage.create_scraped_page",
+ new_callable=AsyncMock,
+ side_effect=RuntimeError("stop inside try"),
+ ),
+ ):
+ mock_ctx.ensure_context.return_value = MagicMock(request_id="req_test", tz_info=None, prompt=None)
+ with pytest.raises(RuntimeError, match="stop inside try"):
+ await run_sdk_action(mock_request, organization=mock_organization)
+
+ mock_app.WORKFLOW_CONTEXT_MANAGER.remove_workflow_run_context.assert_called_once_with("wr_owned")