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.

Retry opening the native Safari session, and only that (#11629)

Daniel Hanb9f815582955
diff --git a/tests/studio/_playwright_robust.py b/tests/studio/_playwright_robust.py
index f894f836d98..b0c69aa203f 100644
--- a/tests/studio/_playwright_robust.py
+++ b/tests/studio/_playwright_robust.py
@@ -263,6 +263,35 @@ def stop_process(proc: subprocess.Popen[str]) -> None:
pass
+def open_session_with_retry(
+ factory: Callable[[], Any],
+ *,
+ retry_on: type[BaseException] | tuple[type[BaseException], ...],
+ attempts: int = 3,
+ pause: float = 5.0,
+ sleep: Callable[[float], None] = time.sleep,
+ log: Callable[[str], None] = print,
+) -> Any:
+ """Open a browser session, retrying only the failure to open one.
+
+ On hosted macOS runners safaridriver sometimes times out "finding or launching a compatible
+ local Safari", which fails the whole leg before a page is loaded. That is the runner, not
+ the change under test, so creating the session gets a bounded retry. Nothing after it
+ does: every assertion still runs once, against the session that did open.
+ """
+ if attempts < 1:
+ raise ValueError("attempts must be at least 1")
+ for attempt in range(1, attempts + 1):
+ try:
+ return factory()
+ except retry_on as exc:
+ if attempt == attempts:
+ raise
+ log(f"browser session not created (attempt {attempt}/{attempts}), retrying: {exc}")
+ sleep(pause)
+ raise AssertionError("unreachable")
+
+
def wait_for_smoke_page(
url: str,
entry: str,
diff --git a/tests/studio/selenium_composer_safari.py b/tests/studio/selenium_composer_safari.py
index 013f92f5184..98f55290165 100644
--- a/tests/studio/selenium_composer_safari.py
+++ b/tests/studio/selenium_composer_safari.py
@@ -10,7 +10,13 @@ from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
-from _playwright_robust import start_vite, stop_process, wait_for_smoke_page
+from selenium.common.exceptions import SessionNotCreatedException
+from _playwright_robust import (
+ open_session_with_retry,
+ start_vite,
+ stop_process,
+ wait_for_smoke_page,
+)
def main():
@@ -26,7 +32,7 @@ def main():
"/smoke-prompt-queue-actions-main.tsx",
proc = server,
)
- driver = webdriver.Safari()
+ driver = open_session_with_retry(webdriver.Safari, retry_on = SessionNotCreatedException)
driver.set_window_size(1100, 900)
report["version"] = driver.capabilities.get("browserVersion")
wait = WebDriverWait(driver, 10)
diff --git a/tests/studio/test_safari_session_retry.py b/tests/studio/test_safari_session_retry.py
new file mode 100644
index 00000000000..95ae3a28517
--- /dev/null
+++ b/tests/studio/test_safari_session_retry.py
@@ -0,0 +1,94 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""The native Safari leg retries opening its session, and nothing else.
+
+`Composer (macos-latest, safari)` failed on unrelated pull requests with
+`SessionNotCreatedException: The session timed out while connecting to a Safari instance`,
+raised by `webdriver.Safari()` before a page was loaded. That is the hosted runner. The
+retry is bounded, covers only session creation, and re-raises the last failure, so a Safari
+that never starts still fails the leg.
+"""
+
+from __future__ import annotations
+
+import ast
+import sys
+from pathlib import Path
+
+import pytest
+
+HERE = Path(__file__).resolve().parent
+sys.path.insert(0, str(HERE))
+
+from _playwright_robust import open_session_with_retry # noqa: E402
+
+
+class _NotCreated(Exception):
+ pass
+
+
+def _factory(failures: int):
+ calls = {"n": 0}
+
+ def make():
+ calls["n"] += 1
+ if calls["n"] <= failures:
+ raise _NotCreated(f"timed out {calls['n']}")
+ return "session"
+
+ return make, calls
+
+
+def test_a_slow_start_is_retried_until_a_session_opens():
+ make, calls = _factory(failures = 2)
+ slept = []
+ assert (
+ open_session_with_retry(make, retry_on = _NotCreated, sleep = slept.append, log = lambda _m: None)
+ == "session"
+ )
+ assert calls["n"] == 3
+ assert slept == [5.0, 5.0]
+
+
+def test_a_safari_that_never_starts_still_fails():
+ make, calls = _factory(failures = 10)
+ with pytest.raises(_NotCreated, match = "timed out 3"):
+ open_session_with_retry(
+ make, retry_on = _NotCreated, sleep = lambda _s: None, log = lambda _m: None
+ )
+ assert calls["n"] == 3, "the retry must stay bounded"
+
+
+def test_other_errors_are_not_retried():
+ calls = {"n": 0}
+
+ def make():
+ calls["n"] += 1
+ raise ValueError("a real bug")
+
+ with pytest.raises(ValueError):
+ open_session_with_retry(
+ make, retry_on = _NotCreated, sleep = lambda _s: None, log = lambda _m: None
+ )
+ assert calls["n"] == 1
+
+
+def test_the_safari_driver_opens_its_session_through_the_retry_and_only_there():
+ tree = ast.parse((HERE / "selenium_composer_safari.py").read_text(encoding = "utf-8"))
+ direct = [
+ n
+ for n in ast.walk(tree)
+ if isinstance(n, ast.Call) and isinstance(n.func, ast.Attribute) and n.func.attr == "Safari"
+ ]
+ assert not direct, "webdriver.Safari() is called directly again, outside the retry"
+ wrapped = [
+ n
+ for n in ast.walk(tree)
+ if isinstance(n, ast.Call) and getattr(n.func, "id", None) == "open_session_with_retry"
+ ]
+ assert len(wrapped) == 1
+ retry_on = {k.arg: k.value for k in wrapped[0].keywords}.get("retry_on")
+ assert (
+ getattr(retry_on, "id", None) == "SessionNotCreatedException"
+ ), "only a failure to create the session may be retried"