Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix: run background work when FastAPI BackgroundTasks is unavailable (#7518)
Aaron Perez27583a01ae06
diff --git a/skyvern/forge/sdk/executor/background_task_executor.py b/skyvern/forge/sdk/executor/background_task_executor.pyindex 3fb9beb00..70d9d2047 100644--- a/skyvern/forge/sdk/executor/background_task_executor.py+++ b/skyvern/forge/sdk/executor/background_task_executor.py@@ -1,3 +1,6 @@+import asyncio+from collections.abc import Callable, Coroutine+from dataclasses import replacefrom typing import Anyimport structlog@@ -20,7 +23,45 @@ from skyvern.utils.files import initialize_skyvern_state_fileLOG = structlog.get_logger()+async def _run_with_own_context(+ func: Callable[..., Coroutine[Any, Any, Any]],+ /,+ *args: Any,+ **kwargs: Any,+) -> None:+ """Give a fire-and-forget run its own SkyvernContext instance.++ asyncio.create_task copies the ContextVar binding but not the object it points at, and the+ caller keeps running (WorkflowTriggerBlock dispatches a child and resumes). Both would then+ write the same context — execute_workflow assigns generate_script on it, block execution+ assigns task_id/step_id — and clobber each other. Shallow-copy so scalar writes stay local+ while inherited values survive.+ """+ parent = skyvern_context.current()+ if parent is not None:+ skyvern_context.set(replace(parent))+ await func(*args, **kwargs)++class BackgroundTaskExecutor(AsyncExecutor):+ # Prevent GC of fire-and-forget asyncio tasks (e.g. when running without FastAPI BackgroundTasks).+ _background_tasks: set[asyncio.Task] = set() # noqa: RUF012++ def _schedule(+ self,+ background_tasks: BackgroundTasks | None,+ func: Callable[..., Coroutine[Any, Any, Any]],+ /,+ *args: Any,+ **kwargs: Any,+ ) -> None:+ if background_tasks:+ background_tasks.add_task(func, *args, **kwargs)+ return+ task = asyncio.create_task(_run_with_own_context(func, *args, **kwargs))+ self._background_tasks.add(task)+ task.add_done_callback(self._background_tasks.discard)+async def execute_task(self,request: Request | None,@@ -70,19 +111,19 @@ class BackgroundTaskExecutor(AsyncExecutor):context.max_steps_override = max_steps_overridecontext.max_screenshot_scrolls = task.max_screenshot_scrolls- if background_tasks:- await load_custom_llm_configs_for_organization(app.DATABASE, organization_id)- await initialize_skyvern_state_file(task_id=task_id, organization_id=organization_id)- background_tasks.add_task(- app.agent.execute_step,- organization,- task,- step,- api_key,- close_browser_on_completion=close_browser_on_completion,- browser_session_id=browser_session_id,- engine=engine,- )+ await load_custom_llm_configs_for_organization(app.DATABASE, organization_id)+ await initialize_skyvern_state_file(task_id=task_id, organization_id=organization_id)+ self._schedule(+ background_tasks,+ app.agent.execute_step,+ organization,+ task,+ step,+ api_key,+ close_browser_on_completion=close_browser_on_completion,+ browser_session_id=browser_session_id,+ engine=engine,+ )async def execute_workflow(self,@@ -99,28 +140,26 @@ class BackgroundTaskExecutor(AsyncExecutor):block_outputs: dict[str, Any] | None,**kwargs: dict,) -> None:- if background_tasks:- LOG.info(- "Executing workflow using background task executor",- workflow_run_id=workflow_run_id,- )-- await initialize_skyvern_state_file(- workflow_run_id=workflow_run_id, organization_id=organization.organization_id- )- await load_custom_llm_configs_for_organization(app.DATABASE, organization.organization_id)-- background_tasks.add_task(- app.WORKFLOW_SERVICE.execute_workflow,- workflow_run_id=workflow_run_id,- api_key=api_key,- organization=organization,- browser_session_id=browser_session_id,- block_labels=block_labels,- block_outputs=block_outputs,- )- else:- LOG.warning("Background tasks not enabled, skipping workflow execution")+ LOG.info(+ "Executing workflow using background task executor",+ workflow_run_id=workflow_run_id,+ )++ await initialize_skyvern_state_file(+ workflow_run_id=workflow_run_id, organization_id=organization.organization_id+ )+ await load_custom_llm_configs_for_organization(app.DATABASE, organization.organization_id)++ self._schedule(+ background_tasks,+ app.WORKFLOW_SERVICE.execute_workflow,+ workflow_run_id=workflow_run_id,+ api_key=api_key,+ organization=organization,+ browser_session_id=browser_session_id,+ block_labels=block_labels,+ block_outputs=block_outputs,+ )async def execute_task_v2(self,@@ -157,18 +196,16 @@ class BackgroundTaskExecutor(AsyncExecutor):status=WorkflowRunStatus.queued,)- if background_tasks:- await initialize_skyvern_state_file(- workflow_run_id=task_v2.workflow_run_id, organization_id=organization_id- )- background_tasks.add_task(- task_v2_service.run_task_v2,- organization=organization,- task_v2_id=task_v2_id,- max_steps_override=max_steps_override,- max_iterations_override=max_iterations_override,- browser_session_id=browser_session_id,- )+ await initialize_skyvern_state_file(workflow_run_id=task_v2.workflow_run_id, organization_id=organization_id)+ self._schedule(+ background_tasks,+ task_v2_service.run_task_v2,+ organization=organization,+ task_v2_id=task_v2_id,+ max_steps_override=max_steps_override,+ max_iterations_override=max_iterations_override,+ browser_session_id=browser_session_id,+ )async def execute_script(self,@@ -180,12 +217,12 @@ class BackgroundTaskExecutor(AsyncExecutor):background_tasks: BackgroundTasks | None = None,**kwargs: dict,) -> None:- if background_tasks:- background_tasks.add_task(- script_service.execute_script,- script_id=script_id,- organization_id=organization_id,- parameters=parameters,- workflow_run_id=workflow_run_id,- background_tasks=background_tasks,- )+ self._schedule(+ background_tasks,+ script_service.execute_script,+ script_id=script_id,+ organization_id=organization_id,+ parameters=parameters,+ workflow_run_id=workflow_run_id,+ background_tasks=background_tasks,+ )diff --git a/tests/unit/test_background_task_executor.py b/tests/unit/test_background_task_executor.pynew file mode 100644index 000000000..443d04f0b--- /dev/null+++ b/tests/unit/test_background_task_executor.py@@ -0,0 +1,87 @@+import asyncio++import pytest+from fastapi import BackgroundTasks++from skyvern.forge.sdk.core import skyvern_context+from skyvern.forge.sdk.core.skyvern_context import SkyvernContext+from skyvern.forge.sdk.executor.background_task_executor import BackgroundTaskExecutor+++async def test_schedule_runs_work_without_background_tasks() -> None:+ """Without a FastAPI BackgroundTasks the work used to be dropped silently."""+ ran = asyncio.Event()++ async def work(value: str, *, keyword: str) -> None:+ assert value == "positional"+ assert keyword == "keyword"+ ran.set()++ BackgroundTaskExecutor()._schedule(None, work, "positional", keyword="keyword")++ await asyncio.wait_for(ran.wait(), timeout=1)+++async def test_schedule_defers_to_background_tasks_when_present() -> None:+ calls: list[tuple[str, str]] = []++ async def work(value: str, *, keyword: str) -> None:+ calls.append((value, keyword))++ background_tasks = BackgroundTasks()+ BackgroundTaskExecutor()._schedule(background_tasks, work, "positional", keyword="keyword")++ # Queued on the request's BackgroundTasks rather than started eagerly.+ assert calls == []+ await background_tasks()+ assert calls == [("positional", "keyword")]+++async def test_scheduled_run_cannot_clobber_the_callers_context() -> None:+ """The caller keeps running after dispatching; both must not write one context object."""+ ran = asyncio.Event()+ child_context: list[SkyvernContext | None] = []++ async def work() -> None:+ context = skyvern_context.current()+ child_context.append(context)+ assert context is not None+ # execute_workflow assigns this on whatever context it finds.+ context.generate_script = False+ context.task_id = "tsk_child"+ ran.set()++ parent = SkyvernContext(organization_id="org_1", task_id="tsk_parent", generate_script=True)+ with skyvern_context.scoped(parent):+ BackgroundTaskExecutor()._schedule(None, work)+ await asyncio.wait_for(ran.wait(), timeout=1)++ assert child_context[0] is not parent+ # The caller's context survives the child's writes.+ assert parent.task_id == "tsk_parent"+ assert parent.generate_script is True+ # ...while inherited values still reach the child.+ assert child_context[0].organization_id == "org_1"+++async def test_scheduled_task_is_retained_until_done() -> None:+ """A bare create_task reference can be garbage collected mid-flight."""+ release = asyncio.Event()++ async def work() -> None:+ await release.wait()++ executor = BackgroundTaskExecutor()+ executor._schedule(None, work)++ await asyncio.sleep(0)+ assert len(executor._background_tasks) == 1++ release.set()+ await asyncio.sleep(0)+ await asyncio.sleep(0)+ assert executor._background_tasks == set()