Skip to content

Automate browser based workflows with AI

Evict detached CodeBlock frames on every page operation, not only in the frame report (SKY-15626) (#8518)

Aaron Perez4f233c013f44
diff --git a/skyvern/cli/mcp_tools/argument_validation.py b/skyvern/cli/mcp_tools/argument_validation.py
index de906bfae..4d1f7b58d 100644
--- a/skyvern/cli/mcp_tools/argument_validation.py
+++ b/skyvern/cli/mcp_tools/argument_validation.py
@@ -63,7 +63,7 @@ def _schema_accepts_array(schema: Any) -> bool:
def _split_comma_separated_list(value: Any) -> Any:
- if not isinstance(value, str) or "," not in value or len(value) > _MAX_REPAIR_STRING_LENGTH:
+ if not isinstance(value, str) or len(value) > _MAX_REPAIR_STRING_LENGTH:
return value
stripped = value.strip()
if not stripped or stripped[0] in "[{(":
diff --git a/skyvern/webeye/real_browser_state.py b/skyvern/webeye/real_browser_state.py
index cb5534518..8dc32ad2c 100644
--- a/skyvern/webeye/real_browser_state.py
+++ b/skyvern/webeye/real_browser_state.py
@@ -95,6 +95,15 @@ class RealBrowserState(BrowserState):
self._disconnect_listener_browser: Browser | None = None
self._crash_listener_pages: weakref.WeakSet[Page] = weakref.WeakSet()
self._crashed_pages: weakref.WeakSet[Page] = weakref.WeakSet()
+ self._crashed_targets_closing: weakref.WeakSet[Page] = weakref.WeakSet()
+ # Cleared while a recovery is producing the working page (opening it, closing crashed targets,
+ # restoring its URL): consumers wait for the finished page instead of driving about:blank or
+ # opening a tab of their own.
+ self._working_page_ready = asyncio.Event()
+ self._working_page_ready.set()
+ # The crash reaper and the caller whose action just failed both reach for a replacement page
+ # at the same moment; single-flight keeps that one page, not one per racer.
+ self._reopen_working_page_lock = asyncio.Lock()
# Browsers without a persistent-session id cannot use the S3 registry. The state is still
# the exclusive owner across an in-process remote-driver reconnect, so it retains the
# trusted registration records until the state itself closes.
@@ -190,25 +199,42 @@ class RealBrowserState(BrowserState):
# page treats "no page" as fatal, so one of them recovering only relocates the failure.
if self.browser_context is None or not self.is_connected():
return None
- lost_page = self.__page
- restore_url = lost_page.url if lost_page is not None else ""
- try:
- page = await self.browser_context.new_page()
- except Exception:
- LOG.warning("Failed to re-open a working page after the previous one was lost", exc_info=True)
- return None
- await self.set_working_page(page)
- if restore_url and restore_url not in BLANK_PAGE_URLS:
+ async with self._reopen_working_page_lock:
+ already_reopened = await self.get_working_page()
+ if already_reopened is not None:
+ await self._close_crashed_targets()
+ return already_reopened
+ lost_page = self.__page
+ restore_url = lost_page.url if lost_page is not None else ""
+ # A fresh event per recovery rather than clear(): the browser-API census counts every
+ # .clear() call site as a page sink.
+ ready = self._working_page_ready = asyncio.Event()
try:
- await self.navigate_to_url(page=page, url=restore_url)
- except Exception:
- LOG.warning(
- "Re-opened the working page but could not restore the URL it was on",
- url=restore_url,
- exc_info=True,
- )
- LOG.info("Re-opened the working page after it was lost", url=restore_url)
- return page
+ # Bounded: every get_working_page consumer waits on this recovery, so a hung CDP call must
+ # fail it within the bound rather than hold them all.
+ try:
+ async with asyncio.timeout(BROWSER_PAGE_CLOSE_TIMEOUT):
+ page = await self.browser_context.new_page()
+ except Exception:
+ LOG.warning("Failed to re-open a working page after the previous one was lost", exc_info=True)
+ return None
+ await self.set_working_page(page)
+ # Whoever opens the replacement closes the crashed targets, and does it before a restore
+ # that can retry for minutes: the reaper may be queued behind this lock.
+ await self._close_crashed_targets()
+ if restore_url and restore_url not in BLANK_PAGE_URLS:
+ try:
+ await self.navigate_to_url(page=page, url=restore_url)
+ except Exception:
+ LOG.warning(
+ "Re-opened the working page but could not restore the URL it was on",
+ url=restore_url,
+ exc_info=True,
+ )
+ finally:
+ ready.set()
+ LOG.info("Re-opened the working page after it was lost", url=restore_url)
+ return page
async def _close_all_other_pages(self, discard_orphaned_videos: bool = False) -> None:
cur_page = await self.get_working_page()
@@ -424,6 +450,7 @@ class RealBrowserState(BrowserState):
await app.AGENT_FUNCTION.wait_for_challenge_solver(page=page)
async def get_working_page(self) -> Page | None:
+ await self._working_page_ready.wait()
if self.__page is None or self.browser_context is None:
return None
@@ -668,24 +695,59 @@ class RealBrowserState(BrowserState):
self._own_detached_task(task, "close_crashed_page")
async def _close_crashed_page(self, page: Page) -> None:
+ self._crashed_pages.add(page)
+ context = self.browser_context
+ if context is None or any(other is not page and other not in self._crashed_pages for other in context.pages):
+ await self._close_crashed_target(page)
+ return
+ # A headed Chromium on Linux exits when its last tab closes, and a persistent-session pod runs
+ # exactly that: closing a crashed only-tab took the whole browser and its CDP endpoint with it,
+ # leaving the pod answering 502 until its timeout. Reopen first so the browser always keeps a
+ # window; the recovery closes the crashed target the moment a replacement exists, whichever
+ # caller opened it. If none can be opened, leave the crashed tab: it is already excluded from
+ # selection, and a stranded tab is recoverable where a dead browser is not.
+ if await self._reopen_lost_working_page() is None:
+ LOG.warning(
+ "Could not open a replacement for the crashed last tab; leaving it open so the browser stays alive",
+ url=page.url,
+ )
+
+ async def _close_crashed_targets(self) -> None:
+ for page in list(self._crashed_pages):
+ await self._close_crashed_target(page)
+
+ async def _close_crashed_target(self, page: Page) -> None:
+ if page in self._crashed_targets_closing:
+ return
+ self._crashed_targets_closing.add(page)
# The crash event fires once. Excluding the page from selection keeps this process healthy, but
# only closing the target unblocks later CDP attaches, so a hung close is retried up to
# CRASHED_PAGE_CLOSE_ATTEMPTS before the target is reported stranded. Bounded by racing the
# close rather than by asyncio.timeout, for the reason _run_bounded_detachable already
# documents: a Playwright close can ignore the cancel a timeout delivers, and that would look
# like success here and skip the retry. A close that raises has finished, so it is not retried.
- for _ in range(CRASHED_PAGE_CLOSE_ATTEMPTS):
- closing = asyncio.ensure_future(page.close())
- done, _pending = await asyncio.wait({closing}, timeout=BROWSER_PAGE_CLOSE_TIMEOUT)
- if closing not in done:
+ closing: asyncio.Task[None] | None = None
+ try:
+ for _ in range(CRASHED_PAGE_CLOSE_ATTEMPTS):
+ closing = asyncio.ensure_future(page.close())
+ done, _pending = await asyncio.wait({closing}, timeout=BROWSER_PAGE_CLOSE_TIMEOUT)
+ if closing not in done:
+ closing.cancel()
+ self._own_detached_task(closing, "close_crashed_page_attempt")
+ continue
+ error = closing.exception() if not closing.cancelled() else None
+ if error is not None:
+ LOG.warning("Error while closing a crashed page", url=page.url, error_type=type(error).__name__)
+ return
+ LOG.warning("Timeout closing a crashed page; the target stays stranded", url=page.url)
+ except asyncio.CancelledError:
+ # The owner (a cancelled caller's recovery) leaves mid-close: hand the attempt off and unmark
+ # the target so the next reaper or recovery retries instead of skipping a close nobody drives.
+ if closing is not None and not closing.done():
closing.cancel()
self._own_detached_task(closing, "close_crashed_page_attempt")
- continue
- error = closing.exception() if not closing.cancelled() else None
- if error is not None:
- LOG.warning("Error while closing a crashed page", url=page.url, error_type=type(error).__name__)
- return
- LOG.warning("Timeout closing a crashed page; the target stays stranded", url=page.url)
+ self._crashed_targets_closing.discard(page)
+ raise
def _on_browser_context_closed(self, context: BrowserContext) -> None:
if context is not self.browser_context:
diff --git a/tests/unit/test_copilot_candidate_navigation_guard.py b/tests/unit/test_copilot_candidate_navigation_guard.py
index f67800507..8c9d0c585 100644
--- a/tests/unit/test_copilot_candidate_navigation_guard.py
+++ b/tests/unit/test_copilot_candidate_navigation_guard.py
@@ -100,6 +100,9 @@ class _Browser:
self.candidate_context = _BrowserContext(self)
return self.candidate_context
+ def is_connected(self) -> bool:
+ return not self.closed
+
async def close(self) -> None:
self.closed = True
diff --git a/tests/unit/test_mcp_argument_validation.py b/tests/unit/test_mcp_argument_validation.py
index 8b44ee2f6..63e19e646 100644
--- a/tests/unit/test_mcp_argument_validation.py
+++ b/tests/unit/test_mcp_argument_validation.py
@@ -128,6 +128,19 @@ async def test_workflow_run_list_comma_separated_status_is_split_for_list_parame
assert _awaited_kwargs(list_runs)["status"] == ["terminated", "failed"]
+async def test_workflow_run_list_bare_status_string_is_wrapped_into_list_before_validation(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ list_runs = AsyncMock(return_value=[])
+ monkeypatch.setattr(mcp_workflow, "list_workflow_runs_raw", list_runs)
+
+ payload = await _call("skyvern_workflow_run_list", {"workflow_id": "wpid_test", "status": "completed"})
+
+ assert payload["ok"] is True
+ assert _awaited_kwargs(list_runs)["status"] == ["completed"]
+
+
@pytest.mark.asyncio
async def test_comma_separated_string_is_not_split_for_non_list_parameter(monkeypatch: pytest.MonkeyPatch) -> None:
list_runs = AsyncMock(return_value=[])
@@ -154,7 +167,7 @@ async def test_comma_separated_string_is_not_split_for_unapproved_list_parameter
@pytest.mark.parametrize(
"value",
- ['["terminated","failed"]', "terminated,,failed", "terminated"],
+ ['["terminated","failed"]', "terminated,,failed", ""],
)
def test_csv_list_repair_leaves_structured_or_ambiguous_strings_for_other_validation(value: str) -> None:
assert _split_comma_separated_list(value) == value
@@ -215,31 +228,17 @@ async def test_type_repair_preserves_structured_missing_key_rejection() -> None:
@pytest.mark.asyncio
- ("arguments", "expected_field"),
- [
- # SKY-15319: page below the tool's documented 1-based minimum.
- ({"workflow_id": "wpid_test", "page": 0}, "page"),
- # SKY-14997: a single status value sent as a bare string instead of a list. Not
- # comma-separated, so the narrow CSV repair correctly leaves it for validation.
- ({"workflow_id": "wpid_test", "status": "completed"}, "status"),
- ],
-)
-async def test_out_of_schema_value_gets_structured_error_instead_of_raising(
- monkeypatch: pytest.MonkeyPatch,
- arguments: dict,
- expected_field: str,
-) -> None:
+async def test_out_of_schema_value_gets_structured_error_instead_of_raising(monkeypatch: pytest.MonkeyPatch) -> None:
list_runs = AsyncMock(return_value=[])
monkeypatch.setattr(mcp_workflow, "list_workflow_runs_raw", list_runs)
- # Regression proof: before the fix, FastMCP's own pydantic validation raised
- # fastmcp.exceptions.ValidationError out of call_next, uncaught by this middleware.
- payload = await _call("skyvern_workflow_run_list", arguments)
+ # SKY-15319: page below the tool's documented 1-based minimum. Before the fix, FastMCP's own
+ # pydantic validation raised fastmcp.exceptions.ValidationError out of call_next, uncaught here.
+ payload = await _call("skyvern_workflow_run_list", {"workflow_id": "wpid_test", "page": 0})
assert payload["ok"] is False
error = payload["error"]
assert error["code"] == "INVALID_INPUT"
- assert expected_field in error["message"]
+ assert "page" in error["message"]
assert "skyvern_workflow_run_list" in error["hint"]
list_runs.assert_not_awaited()
diff --git a/tests/unit/workflow/test_block_browser_state_recovery.py b/tests/unit/workflow/test_block_browser_state_recovery.py
index d7f35d777..96f3be5f3 100644
--- a/tests/unit/workflow/test_block_browser_state_recovery.py
+++ b/tests/unit/workflow/test_block_browser_state_recovery.py
@@ -1254,6 +1254,31 @@ def _crashable_page() -> MagicMock:
return page
+def _crashable_context(*pages: MagicMock) -> MagicMock:
+ """A connected context: a renderer crash leaves the browser itself alive."""
+ context = MagicMock(pages=list(pages))
+ context.browser.is_connected = MagicMock(return_value=True)
+ context._impl_obj = MagicMock(_close_was_called=False, _closed=False, _connection=MagicMock(_closed_error=None))
+ context.new_page = AsyncMock()
+ return context
+
+
+def _replacement_page_opener(context: MagicMock, replacement: MagicMock, order: list[str]) -> AsyncMock:
+ """new_page as Playwright behaves: the new tab joins context.pages before the call returns."""
+
+ async def _open() -> MagicMock:
+ order.append("new_page")
+ context.pages.append(replacement)
+ return replacement
+
+ return AsyncMock(side_effect=_open)
+
+
+async def _reap_crashed_pages(state: RealBrowserState) -> None:
+ async with asyncio.timeout(10):
+ await asyncio.gather(*list(state._detached_teardown_tasks))
+
+
async def _never_returns() -> None:
await asyncio.sleep(3600)
@@ -1304,6 +1329,340 @@ async def test_a_crashed_page_is_excluded_before_its_close_lands() -> None:
task.cancel()
+async def test_closing_the_crashed_only_tab_opens_its_replacement_first() -> None:
+ """A headed Linux Chromium exits with its last tab and takes the CDP endpoint with it, so the
+ replacement must exist before the crashed only-tab closes, and it must be the existing recovery
+ so the next caller resumes on the URL the crashed tab was on (SKY-16016)."""
+ order: list[str] = []
+ crashed = _crashable_page()
+ crashed.url = "https://example.invalid/form"
+ crashed.close = AsyncMock(side_effect=lambda: order.append("close"))
+ replacement = _crashable_page()
+ replacement.url = "about:blank"
+ context = _crashable_context(crashed)
+ context.new_page = _replacement_page_opener(context, replacement, order)
+ state = RealBrowserState(pw=MagicMock(), browser_context=context, page=crashed)
+ state.navigate_to_url = AsyncMock(side_effect=lambda page, url: order.append(f"navigate:{url}"))
+
+ state._on_page_crashed(crashed)
+ await _reap_crashed_pages(state)
+
+ assert order == ["new_page", "close", "navigate:https://example.invalid/form"]
+ assert await state.must_get_working_page() is replacement
+
+
+async def test_a_stalled_url_restore_does_not_hold_the_crashed_target_open() -> None:
+ """The restore navigation can retry for minutes against an unreachable URL; the dead target
+ must already be closed by then, or every later CDP attach still lands on it."""
+ order: list[str] = []
+ closed = asyncio.Event()
+ crashed = _crashable_page()
+ crashed.url = "https://example.invalid/form"
+
+ async def _close() -> None:
+ order.append("close")
+ closed.set()
+
+ crashed.close = AsyncMock(side_effect=_close)
+ replacement = _crashable_page()
+ replacement.url = "about:blank"
+ context = _crashable_context(crashed)
+ context.new_page = _replacement_page_opener(context, replacement, order)
+ state = RealBrowserState(pw=MagicMock(), browser_context=context, page=crashed)
+
+ async def _restore_stalls(**_: object) -> None:
+ order.append("navigate")
+ await _never_returns()
+
+ state.navigate_to_url = AsyncMock(side_effect=_restore_stalls)
+
+ state._on_page_crashed(crashed)
+
+ try:
+ await asyncio.wait_for(closed.wait(), timeout=1)
+ # The event fires inside the close; the stalled navigate may not have started yet.
+ assert order[:2] == ["new_page", "close"]
+ finally:
+ for task in list(state._detached_teardown_tasks):
+ task.cancel()
+
+
+async def test_a_caller_that_wins_the_replacement_closes_the_crashed_target_before_its_restore() -> None:
+ """The caller's own recovery can hold the reopen lock through a restore that retries for minutes;
+ the crashed target must close as soon as that caller's replacement exists, not after the reaper
+ finally gets the lock."""
+ order: list[str] = []
+ gate = asyncio.Event()
+ closed = asyncio.Event()
+ crashed = _crashable_page()
+ crashed.url = "https://example.invalid/form"
+
+ async def _close() -> None:
+ order.append("close")
+ closed.set()
+
+ crashed.close = AsyncMock(side_effect=_close)
+ replacement = _crashable_page()
+ replacement.url = "about:blank"
+ context = _crashable_context(crashed)
+
+ async def _open_when_released() -> MagicMock:
+ order.append("new_page")
+ await gate.wait()
+ context.pages.append(replacement)
+ return replacement
+
+ context.new_page = AsyncMock(side_effect=_open_when_released)
+ state = RealBrowserState(pw=MagicMock(), browser_context=context, page=crashed)
+
+ async def _restore_stalls(**_: object) -> None:
+ order.append("navigate")
+ await _never_returns()
+
+ state.navigate_to_url = AsyncMock(side_effect=_restore_stalls)
+
+ # The crash mark lands synchronously at crash time; here the caller reacts before the reaper task
+ # runs, so its recovery holds the lock inside new_page() when the reaper queues behind it.
+ state._crashed_pages.add(crashed)
+ caller = asyncio.create_task(state.must_get_working_page())
+ await asyncio.sleep(0)
+ state._on_page_crashed(crashed)
+ gate.set()
+
+ try:
+ await asyncio.wait_for(closed.wait(), timeout=1)
+ assert order[:2] == ["new_page", "close"]
+ assert context.new_page.await_count == 1
+ finally:
+ caller.cancel()
+ for task in list(state._detached_teardown_tasks):
+ task.cancel()
+
+
+async def test_consumers_wait_for_the_recovered_page_instead_of_the_blank_replacement() -> None:
+ """While a recovery is restoring the replacement's URL, every consumer waits for the finished
+ page: none is handed about:blank to drive or to navigate away from, and get_or_create_page does
+ not open a tab of its own and close the one being recovered."""
+ order: list[str] = []
+ restore_started = asyncio.Event()
+ restore_gate = asyncio.Event()
+ crashed = _crashable_page()
+ crashed.url = "https://example.invalid/form"
+ replacement = _crashable_page()
+ replacement.url = "about:blank"
+ context = _crashable_context(crashed)
+ context.new_page = _replacement_page_opener(context, replacement, order)
+ state = RealBrowserState(pw=MagicMock(), browser_context=context, page=crashed)
+
+ async def _restore_when_released(**_: object) -> None:
+ restore_started.set()
+ await restore_gate.wait()
+
+ state.navigate_to_url = AsyncMock(side_effect=_restore_when_released)
+
+ state._on_page_crashed(crashed)
+ await asyncio.wait_for(restore_started.wait(), timeout=1)
+ consumers = [
+ asyncio.create_task(state.get_working_page()),
+ asyncio.create_task(state.must_get_working_page()),
+ asyncio.create_task(state.get_or_create_page()),
+ ]
+ await asyncio.sleep(0)
+ assert not any(consumer.done() for consumer in consumers)
+
+ restore_gate.set()
+ await _reap_crashed_pages(state)
+
+ assert [await consumer for consumer in consumers] == [replacement, replacement, replacement]
+ assert context.new_page.await_count == 1
+ state.navigate_to_url.assert_awaited_once()
+
+
+async def test_a_slow_crashed_target_close_does_not_expose_the_blank_replacement() -> None:
+ """The gate closes before the replacement appears, so the crashed-target close that runs ahead of
+ the restore cannot leak the blank tab to a consumer either."""
+ close_started = asyncio.Event()
+ close_gate = asyncio.Event()
+ crashed = _crashable_page()
+ crashed.url = "https://example.invalid/form"
+
+ async def _close_when_released() -> None:
+ close_started.set()
+ await close_gate.wait()
+
+ crashed.close = AsyncMock(side_effect=_close_when_released)
+ replacement = _crashable_page()
+ replacement.url = "about:blank"
+ context = _crashable_context(crashed)
+ context.new_page = _replacement_page_opener(context, replacement, [])
+ state = RealBrowserState(pw=MagicMock(), browser_context=context, page=crashed)
+ state.navigate_to_url = AsyncMock()
+
+ state._on_page_crashed(crashed)
+ await asyncio.wait_for(close_started.wait(), timeout=1)
+ consumer = asyncio.create_task(state.get_working_page())
+ await asyncio.sleep(0)
+ assert not consumer.done()
+
+ close_gate.set()
+ await _reap_crashed_pages(state)
+
+ assert await consumer is replacement
+
+
+async def test_a_cancelled_recovery_owner_leaves_the_crashed_target_for_the_reaper_to_close() -> None:
+ """A caller whose recovery wins can be cancelled while it awaits a slow close; the target must not
+ stay marked as closing, or the queued reaper skips it and nothing ever retries."""
+ closed = asyncio.Event()
+ attempts: list[int] = []
+
+ async def _hang_then_close() -> None:
+ attempts.append(1)
+ if len(attempts) == 1:
+ await _never_returns()
+ closed.set()
+
+ crashed = _crashable_page()
+ crashed.url = "https://example.invalid/form"
+ crashed.close = AsyncMock(side_effect=_hang_then_close)
+ replacement = _crashable_page()
+ replacement.url = "about:blank"
+ context = _crashable_context(crashed)
+ context.new_page = _replacement_page_opener(context, replacement, [])
+ state = RealBrowserState(pw=MagicMock(), browser_context=context, page=crashed)
+ state.navigate_to_url = AsyncMock()
+
+ state._crashed_pages.add(crashed)
+ owner = asyncio.create_task(state.must_get_working_page())
+ for _ in range(3):
+ await asyncio.sleep(0)
+ assert attempts == [1]
+ owner.cancel()
+ with pytest.raises(asyncio.CancelledError):
+ await owner
+
+ state._on_page_crashed(crashed)
+ try:
+ await asyncio.wait_for(closed.wait(), timeout=1)
+ assert crashed.close.await_count == 2
+ finally:
+ for task in list(state._detached_teardown_tasks):
+ task.cancel()
+
+
+async def test_a_hung_new_page_fails_the_recovery_within_the_bound_and_releases_consumers(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """Every consumer waits on the recovery, so a CDP new_page() that never answers must fail it
+ within the bound; the crashed tab is then left open rather than the browser losing its last tab."""
+ monkeypatch.setattr(real_browser_state_module, "BROWSER_PAGE_CLOSE_TIMEOUT", 0.05)
+ crashed = _crashable_page()
+ crashed.url = "https://example.invalid/form"
+ context = _crashable_context(crashed)
+ context.new_page = AsyncMock(side_effect=_never_returns)
+ state = RealBrowserState(pw=MagicMock(), browser_context=context, page=crashed)
+
+ state._on_page_crashed(crashed)
+ await asyncio.sleep(0)
+ consumer = asyncio.create_task(state.get_working_page())
+
+ try:
+ assert await asyncio.wait_for(consumer, timeout=1) is None
+ await _reap_crashed_pages(state)
+ crashed.close.assert_not_awaited()
+ finally:
+ for task in list(state._detached_teardown_tasks):
+ task.cancel()
+
+
+async def test_a_caller_that_recovered_first_still_gets_the_crashed_target_closed() -> None:
+ crashed = _crashable_page()
+ crashed.url = "https://example.invalid/form"
+ replacement = _crashable_page()
+ replacement.url = "about:blank"
+ context = _crashable_context(crashed)
+ context.new_page = _replacement_page_opener(context, replacement, [])
+ state = RealBrowserState(pw=MagicMock(), browser_context=context, page=crashed)
+ state.navigate_to_url = AsyncMock()
+
+ state._crashed_pages.add(crashed)
+ assert await state.must_get_working_page() is replacement
+ state._on_page_crashed(crashed)
+ await _reap_crashed_pages(state)
+
+ crashed.close.assert_awaited_once()
+ assert context.new_page.await_count == 1
+ state.navigate_to_url.assert_awaited_once()
+
+
+async def test_a_caller_recovering_during_the_reap_gets_the_reaper_replacement() -> None:
+ """The caller whose action just failed reaches for a page while the reaper is still opening
+ one; it must wait for that page rather than open a second tab."""
+ order: list[str] = []
+ gate = asyncio.Event()
+ crashed = _crashable_page()
+ crashed.url = "https://example.invalid/form"
+ replacement = _crashable_page()
+ replacement.url = "about:blank"
+ context = _crashable_context(crashed)
+
+ async def _open_when_released() -> MagicMock:
+ order.append("new_page")
+ await gate.wait()
+ context.pages.append(replacement)
+ return replacement
+
+ context.new_page = AsyncMock(side_effect=_open_when_released)
+ state = RealBrowserState(pw=MagicMock(), browser_context=context, page=crashed)
+ state.navigate_to_url = AsyncMock()
+
+ state._on_page_crashed(crashed)
+ caller = asyncio.create_task(state.must_get_working_page())
+ await asyncio.sleep(0)
+ gate.set()
+ await _reap_crashed_pages(state)
+
+ assert await caller is replacement
+ assert context.new_page.await_count == 1
+
+
+async def test_a_crashed_tab_with_a_live_sibling_closes_without_opening_another() -> None:
+ crashed = _crashable_page()
+ survivor = _crashable_page()
+ context = _crashable_context(crashed, survivor)
+ state = RealBrowserState(pw=MagicMock(), browser_context=context)
+
+ state._on_page_crashed(crashed)
+ await _reap_crashed_pages(state)
+
+ context.new_page.assert_not_awaited()
+ crashed.close.assert_awaited_once()
+
+
+async def test_a_crashed_only_tab_stays_open_when_no_replacement_can_be_opened() -> None:
+ crashed = _crashable_page()
+ context = _crashable_context(crashed)
+ context.new_page = AsyncMock(side_effect=RuntimeError("target could not be created"))
+ state = RealBrowserState(pw=MagicMock(), browser_context=context)
+
+ state._on_page_crashed(crashed)
+ await _reap_crashed_pages(state)
+
+ crashed.close.assert_not_awaited()
+ assert await state.list_valid_pages() == []
+
+
@pytest.mark.asyncio
async def test_a_close_that_swallows_cancellation_still_exhausts_its_attempts(
monkeypatch: pytest.MonkeyPatch,
@@ -1320,7 +1679,7 @@ async def test_a_close_that_swallows_cancellation_still_exhausts_its_attempts(
return
page.close = AsyncMock(side_effect=_swallow_cancellation)
- state = RealBrowserState(pw=MagicMock(), browser_context=MagicMock(pages=[page]))
+ state = RealBrowserState(pw=MagicMock(), browser_context=_crashable_context(page))
await state._close_crashed_page(page)
@@ -1339,7 +1698,7 @@ async def test_a_timed_out_crashed_page_close_is_retried_once(monkeypatch: pytes
await _never_returns()
page.close = AsyncMock(side_effect=_hang_once_then_succeed)
- state = RealBrowserState(pw=MagicMock(), browser_context=MagicMock(pages=[page]))
+ state = RealBrowserState(pw=MagicMock(), browser_context=_crashable_context(page))
await state._close_crashed_page(page)
@@ -1354,14 +1713,13 @@ async def test_crashed_page_close_is_bounded_and_never_raises(monkeypatch: pytes
stubborn.close = AsyncMock(side_effect=RuntimeError("close refused"))
hanging = _crashable_page()
hanging.close = AsyncMock(side_effect=_never_returns)
- context = MagicMock(pages=[crashed, stubborn, hanging])
+ context = _crashable_context(crashed, stubborn, hanging)
state = RealBrowserState(pw=MagicMock(), browser_context=context)
state._on_page_crashed(crashed)
state._on_page_crashed(stubborn)
state._on_page_crashed(hanging)
- async with asyncio.timeout(10):
- await asyncio.gather(*list(state._detached_teardown_tasks))
+ await _reap_crashed_pages(state)
crashed.close.assert_awaited_once()
stubborn.close.assert_awaited_once()