Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Fix sequential child runs failing with missing browser page (SKY-13182) (#7841)
Aaron Perez314799ce0add
diff --git a/skyvern/webeye/real_browser_manager.py b/skyvern/webeye/real_browser_manager.pyindex b90a2f107..dde67e06c 100644--- a/skyvern/webeye/real_browser_manager.py+++ b/skyvern/webeye/real_browser_manager.py@@ -557,19 +557,36 @@ class RealBrowserManager(BrowserManager):workflow_run_id=workflow_run_id, parent_workflow_run_id=parent_workflow_run_id)if browser_state:- # always keep the browser state for the workflow run and the parent workflow run synced- self.pages[workflow_run_id] = browser_state- if parent_workflow_run_id:- self.pages[parent_workflow_run_id] = browser_state- # The workflow-run streaming endpoint reads ``{workflow_run_id}.png``, so the- # child needs its own publisher even when reusing the parent's browser state —- # the parent's publisher writes a different key.- await self._start_frame_publisher(- browser_state=browser_state,+ # A parent_workflow_run_id entry is shared by every child run of the same parent.+ # Independent children dispatched to one long-lived worker (e.g. a sequential+ # fan-out) can therefore find an earlier, already-completed sibling's torn-down+ # browser here. This early-return path skips get_or_create_page, so returning a+ # page-less state would fail the run's first browser block with a missing page.+ # Only adopt the inherited browser when it still has a working page; otherwise+ # drop the stale entry and fall through to create a fresh browser for this run.+ if await browser_state.get_working_page() is not None:+ # always keep the browser state for the workflow run and the parent workflow run synced+ self.pages[workflow_run_id] = browser_state+ if parent_workflow_run_id:+ self.pages[parent_workflow_run_id] = browser_state+ # The workflow-run streaming endpoint reads ``{workflow_run_id}.png``, so the+ # child needs its own publisher even when reusing the parent's browser state —+ # the parent's publisher writes a different key.+ await self._start_frame_publisher(+ browser_state=browser_state,+ workflow_run_id=workflow_run_id,+ organization_id=workflow_run.organization_id,+ )+ return browser_state+ LOG.warning(+ "Inherited parent browser state has no working page; creating a fresh browser",workflow_run_id=workflow_run_id,- organization_id=workflow_run.organization_id,+ parent_workflow_run_id=parent_workflow_run_id,)- return browser_state+ for stale_key in (workflow_run_id, parent_workflow_run_id):+ if stale_key and self.pages.get(stale_key) is browser_state:+ self.pages.pop(stale_key, None)+ browser_state = Noneif browser_session_id:LOG.info(diff --git a/tests/unit/test_real_browser_manager.py b/tests/unit/test_real_browser_manager.pyindex e81fa0485..c01fd436f 100644--- a/tests/unit/test_real_browser_manager.py+++ b/tests/unit/test_real_browser_manager.py@@ -151,9 +151,10 @@ async def test_non_pbs_workflow_run_cache_hit_on_second_call() -> None:@pytest.mark.asyncioasync def test_non_pbs_workflow_run_inherits_parent_browser() -> None:- """Non-PBS child runs must still inherit the parent's browser when no browser_session_id."""+ """Non-PBS child runs must still inherit a healthy parent browser when no browser_session_id."""manager = RealBrowserManager()parent_state = MagicMock()+ parent_state.get_working_page = AsyncMock(return_value=MagicMock())manager.pages["wfr_parent"] = parent_stateworkflow_run = make_workflow_run("wfr_child", parent_workflow_run_id="wfr_parent")@@ -170,6 +171,46 @@ async def test_non_pbs_workflow_run_inherits_parent_browser() -> None:assert manager.pages["wfr_parent"] is parent_state+async def test_child_run_does_not_adopt_stale_sibling_browser_without_page() -> None:+ """A parent_workflow_run_id entry is shared by every child run of the same parent.++ When independent child runs are dispatched to one long-lived worker (e.g. a+ sequential fan-out), a later child can find an earlier, already-completed+ sibling's torn-down browser under the parent key. It must NOT be adopted:+ its page is gone, so the first browser block would raise+ ``MissingBrowserStatePage`` ("Browser state page is missing"). The manager+ must instead evict the stale entry and create a fresh browser for the run.+ """+ manager = RealBrowserManager()+ # Sibling C1 completed and left a torn-down browser under the shared parent key.+ stale_state = MagicMock()+ stale_state.get_working_page = AsyncMock(return_value=None)+ manager.pages["wfr_parent"] = stale_state++ workflow_run = make_workflow_run("wfr_child_2", parent_workflow_run_id="wfr_parent")++ fresh_state = MagicMock()+ fresh_state.get_or_create_page = AsyncMock()++ with patch.object(manager, "_create_browser_state", new=AsyncMock(return_value=fresh_state)) as mock_create:+ result = await manager.get_or_create_for_workflow_run(+ workflow_run=workflow_run,+ url="https://example.com",+ browser_session_id=None,+ )++ # A brand-new browser must be created, not the stale sibling state.+ mock_create.assert_awaited_once()+ assert result is fresh_state+ assert result is not stale_state+ # The fresh browser gets a page (the parent early-return path skips this).+ fresh_state.get_or_create_page.assert_awaited_once()+ # The stale entry must be replaced by the fresh browser, not left dangling.+ assert manager.pages["wfr_child_2"] is fresh_state+ assert manager.pages["wfr_parent"] is fresh_state++def make_task(task_id: str,organization_id: str = "org_test",@@ -720,6 +761,7 @@ async def test_non_pbs_workflow_run_does_not_rebind() -> None:"""The own-browser (no browser_session_id) path must run zero new download-rebind code (SKY-11083 regression guard)."""manager = RealBrowserManager()parent_state = MagicMock()+ parent_state.get_working_page = AsyncMock(return_value=MagicMock())manager.pages["wfr_parent"] = parent_stateworkflow_run = make_workflow_run("wfr_child", parent_workflow_run_id="wfr_parent")diff --git a/tests/unit/test_real_browser_manager_frame_publisher.py b/tests/unit/test_real_browser_manager_frame_publisher.pyindex 76a6ebfe4..8bb84b4a5 100644--- a/tests/unit/test_real_browser_manager_frame_publisher.py+++ b/tests/unit/test_real_browser_manager_frame_publisher.py@@ -250,6 +250,8 @@ async def test_inherited_child_workflow_starts_publisher_for_child_key(# Parent was created via a remote-CDP creator; the marker is set to True.# Bool literal (not a Mock auto-attribute) so the gate accepts it.parent_state.browser_artifacts.needs_cdp_frame_publisher = True+ # A live parent browser has a working page, so the child adopts it.+ parent_state.get_working_page = AsyncMock(return_value=MagicMock())manager.pages["wfr_parent"] = parent_stateworkflow_run = MagicMock()