Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix(SKY-12490): retry profile creation when startup sweep tombstones a freshly locked dir (#7621)
Shuchang Zheng1d66418b57d3
diff --git a/skyvern/library/local_browser_profile.py b/skyvern/library/local_browser_profile.pyindex 071458c66..64a16de33 100644--- a/skyvern/library/local_browser_profile.py+++ b/skyvern/library/local_browser_profile.py@@ -204,14 +204,16 @@ def create_local_browser_profile() -> LocalBrowserProfile | None:sweep_local_browser_profiles_once_in_background()- # A concurrent startup sweep can win the race in the mkdtemp->flock window: reaping the- # still-lockless dir (FileNotFoundError) or acquiring the just-published lock first- # (BlockingIOError — nobody else can hold a lock we created O_EXCL). Retry with a fresh dir.- last_error: OSError | None = None+ # A concurrent startup sweep can win the race in the mkdtemp->revalidate window: reaping the+ # still-lockless dir (FileNotFoundError), acquiring the just-published lock first+ # (BlockingIOError — nobody else can hold a lock we created O_EXCL), or tombstone-renaming a+ # dir it saw lockless just as we locked it, failing our revalidate before its ENOTEMPTY rmdir+ # makes it rename the dir back (_ProfileIdentityChangedError). Retry with a fresh dir.+ last_error: Exception | None = Nonefor _ in range(3):try:return _create_local_browser_profile_once()- except (FileNotFoundError, BlockingIOError) as exc:+ except (FileNotFoundError, BlockingIOError, _ProfileIdentityChangedError) as exc:last_error = excraise RuntimeError("Local browser profile creation kept losing its directory") from last_errordiff --git a/tests/unit/test_local_browser_profile.py b/tests/unit/test_local_browser_profile.pyindex ea3e98fde..452f12491 100644--- a/tests/unit/test_local_browser_profile.py+++ b/tests/unit/test_local_browser_profile.py@@ -839,6 +839,38 @@ def test_create_retries_when_directory_reaped_mid_creation(tmp_path: Path, monkeprofile.release()[email protected](os.name == "nt", reason="flock is POSIX-only")+def test_create_retries_when_sweep_tombstones_freshly_locked_profile(+ tmp_path: Path,+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ monkeypatch.setattr(tempfile, "gettempdir", lambda: str(tmp_path))+ real_revalidate = local_browser_profile.LocalBrowserProfile.revalidate+ calls = {"count": 0}++ def tombstoned_during_first_revalidate(profile: local_browser_profile.LocalBrowserProfile) -> bool:+ calls["count"] += 1+ if calls["count"] > 1:+ return real_revalidate(profile)+ tombstone = profile.path.with_name(".reap-test")+ profile.path.rename(tombstone)+ try:+ return real_revalidate(profile)+ finally:+ tombstone.rename(profile.path)++ monkeypatch.setattr(local_browser_profile.LocalBrowserProfile, "revalidate", tombstoned_during_first_revalidate)+ profile = local_browser_profile.create_local_browser_profile()+ try:+ assert profile is not None+ assert calls["count"] == 2+ assert profile.path.exists()+ assert profile.revalidate()+ finally:+ if profile is not None:+ profile.release()++@pytest.mark.skipif(os.name == "nt", reason="flock is POSIX-only")def test_create_retries_when_sweep_wins_published_lock_race(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:monkeypatch.setattr(tempfile, "gettempdir", lambda: str(tmp_path))