Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Chat UI test: close the stale page browser-side instead of waiting on its renderer (#13708)

Daniel Han23e7248c642f
diff --git a/tests/studio/playwright_chat_ui.py b/tests/studio/playwright_chat_ui.py
index bcf3aef4550..16841c12c21 100644
--- a/tests/studio/playwright_chat_ui.py
+++ b/tests/studio/playwright_chat_ui.py
@@ -162,6 +162,30 @@ def new_throttled_page(ctx):
return page
+def close_page_without_waiting(ctx, page) -> None:
+ """Close a page without waiting on its renderer, on Chromium; page.close() elsewhere.
+
+ page.close() returns only once the page reports itself closed, and has no timeout. On the
+ Windows lane it has sat out a whole 540 s step budget on the persisted-monitor reset, the
+ last line printed being "closing the stale page". Attaching a CDP session, reading the
+ target id and Target.closeTarget are all answered by the browser process, so none of them
+ waits on a busy or throttled renderer, and closeTarget returns as soon as the close is
+ queued. Requests the page sends before it is gone are the caller's to discount.
+ """
+ if PLAYWRIGHT_BROWSER == "chromium":
+ try:
+ target = ctx.new_cdp_session(page).send("Target.getTargetInfo")["targetInfo"][
+ "targetId"
+ ]
+ ctx.browser.new_browser_cdp_session().send("Target.closeTarget", {"targetId": target})
+ return
+ except Exception as exc:
+ info(
+ f"WARN closing the page browser-side failed ({exc!r}); falling back to page.close()"
+ )
+ page.close()
+
+
def recover_or_replace_page(page, ctx, **kwargs):
"""The shared recovery, with the throttle carried onto a replacement page.
@@ -1130,14 +1154,21 @@ with sync_playwright() as p:
# See _playwright_robust.py.
install_view_transition_killer(ctx)
system_requests: list[str] = []
- ctx.on(
- "request",
- lambda request: (
- system_requests.append(request.url)
- if request.url.split("?", 1)[0].endswith("/api/system")
- else None
- ),
- )
+ # Pages closed without waiting (close_page_without_waiting) can still send a request or two while the
+ # browser tears them down; those are not the page the monitor steps observe.
+ stale_pages: set = set()
+
+ def _count_system_request(request) -> None:
+ if not request.url.split("?", 1)[0].endswith("/api/system"):
+ return
+ try:
+ if request.frame.page in stale_pages:
+ return
+ except Exception:
+ pass # no frame (a worker): counted, as before
+ system_requests.append(request.url)
+
+ ctx.on("request", _count_system_request)
page = new_throttled_page(ctx)
page_errors = []
page.on("pageerror", lambda e: page_errors.append(str(e)))
@@ -2675,9 +2706,10 @@ with sync_playwright() as p:
# clearing cookies are browser-side calls, and the storage writes go through the
# fresh page parked on a same-origin JSON endpoint where no app code runs. Each call
# announces itself first, so a wedge that remains names the call it sits in.
+ stale_pages.add(page)
info("closing the stale page")
try:
- page.close()
+ close_page_without_waiting(ctx, page)
except Exception as exc:
info(f"WARN closing the stale page failed: {exc!r}")
info("clearing stale session cookies")
diff --git a/tests/studio/test_chat_ui_driver_cpu_throttle.py b/tests/studio/test_chat_ui_driver_cpu_throttle.py
index 3c85a655e2f..e2cdfb5f7cd 100644
--- a/tests/studio/test_chat_ui_driver_cpu_throttle.py
+++ b/tests/studio/test_chat_ui_driver_cpu_throttle.py
@@ -97,10 +97,21 @@ def test_every_page_the_driver_opens_goes_through_one_factory():
def test_the_cdp_call_has_exactly_one_owner():
- assert CODE.count("new_cdp_session") == 1, (
- "a second CDP session would sit outside apply_cpu_throttle, so the "
- "refusal and the recovery path would both miss it"
+ # Two owners: apply_cpu_throttle, and close_page_without_waiting, which only reads the page's
+ # target id and is gated on chromium itself. The throttle itself is set in one place only.
+ assert CODE.count("new_cdp_session") == 2, (
+ "a CDP session outside apply_cpu_throttle and close_page_without_waiting would "
+ "sit outside the chromium refusal and the recovery path"
)
+ assert CODE.count("Emulation.setCPUThrottlingRate") == 1
+ closer = re.search(
+ r"def close_page_without_waiting\([^)]*\)[^:]*:(?P<body>(?:\n(?:[ \t].*)?)+?)(?=\ndef |\nclass |\Z)",
+ CODE,
+ )
+ assert closer and "new_cdp_session" in closer.group("body")
+ assert 'PLAYWRIGHT_BROWSER == "chromium"' in closer.group(
+ "body"
+ ), "the closer's CDP session is not gated on chromium, so firefox/webkit would hit a CDP error"
owner = re.search(
r"def apply_cpu_throttle\([^)]*\):(?P<body>(?:\n(?:[ \t].*)?)+?)(?=\ndef |\nclass |\Z)",
CODE,
diff --git a/tests/studio/test_chat_ui_persisted_monitor_reset.py b/tests/studio/test_chat_ui_persisted_monitor_reset.py
index b02233ca9b7..fb43f39967e 100644
--- a/tests/studio/test_chat_ui_persisted_monitor_reset.py
+++ b/tests/studio/test_chat_ui_persisted_monitor_reset.py
@@ -34,7 +34,9 @@ def test_the_stale_page_runs_no_script_before_it_is_replaced():
def test_the_stale_page_is_closed_before_the_fresh_one_writes_storage():
body = _step_body()
- assert body.index("page.close()") < body.index("new_throttled_page(ctx)"), (
+ assert body.index("close_page_without_waiting(ctx, page)") < body.index(
+ "new_throttled_page(ctx)"
+ ), (
"close the stale page first, or its app can rewrite the shared localStorage "
"after the fresh page has set it"
)
@@ -58,7 +60,9 @@ def test_every_blocking_call_announces_itself_first():
body = _step_body()
handoff = body.index("page = _fresh_page")
calls = {
- "page.close()": body.index("page.close()"),
+ "close_page_without_waiting(ctx, page)": body.index(
+ "close_page_without_waiting(ctx, page)"
+ ),
"ctx.clear_cookies()": body.index("ctx.clear_cookies()"),
"new_throttled_page(ctx)": body.index("new_throttled_page(ctx)"),
"page.goto(": body.index("page.goto("),
@@ -76,3 +80,33 @@ def test_every_blocking_call_announces_itself_first():
assert preceding and preceding[-1].startswith(
"info("
), f"{call} has no breadcrumb directly before it, so a wedge there names the wrong call"
+
+
+def test_the_stale_page_is_closed_without_waiting_on_its_renderer():
+ """page.close() has no timeout and has sat out the step's whole budget on Windows.
+
+ The step closes through close_page_without_waiting (browser-side Target.closeTarget on
+ Chromium), and marks the page stale first, so a request it sends while the browser tears
+ it down is not counted as the fresh page's /api/system poll.
+ """
+ body = _step_body()
+ handoff = body.index("page = _fresh_page")
+ assert "page.close()" not in body[:handoff], "the reset waits on the stale renderer again"
+ close = body.index("close_page_without_waiting(ctx, page)")
+ assert (
+ body.index("stale_pages.add(page)") < close
+ ), "mark the page stale before it can still send"
+ helper = CODE[CODE.index("def close_page_without_waiting(") :]
+ helper = helper[: helper.index("\ndef ")]
+ assert '"Target.closeTarget"' in helper and "new_browser_cdp_session()" in helper
+ assert (
+ 'PLAYWRIGHT_BROWSER == "chromium"' in helper
+ ), "CDP is Chromium only; other engines fall back"
+
+
+def test_the_system_request_count_skips_a_stale_page():
+ counter = CODE[CODE.index("def _count_system_request(") :]
+ counter = counter[: counter.index('ctx.on("request", _count_system_request)')]
+ assert (
+ "in stale_pages" in counter
+ ), "requests from a page closed without waiting would read as the monitor polling"