From 14c76802662ab96cd82544a85f9ca5c198cf886d Mon Sep 17 00:00:00 2001 From: Vlada Dusek Date: Tue, 17 Nov 2026 10:30:34 +0100 Subject: [PATCH 1/4] test: fix Stagehand binary cache race in plugin tests --- tests/integration/browsers/test_stagehand_browser_plugin.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/integration/browsers/test_stagehand_browser_plugin.py b/tests/integration/browsers/test_stagehand_browser_plugin.py index e63d9f29e0..21ddd09f90 100644 --- a/tests/integration/browsers/test_stagehand_browser_plugin.py +++ b/tests/integration/browsers/test_stagehand_browser_plugin.py @@ -69,6 +69,7 @@ async def test_stagehand_called_with_browserbase_params(stagehand_client_mock: M assert 'local_chrome_path' not in call_kwargs +@pytest.mark.usefixtures('stagehand_binary') async def test_methods_raise_error_when_not_active() -> None: plugin = StagehandBrowserPlugin() From d3e8660a83f37c8313ddfe45d8cd0156c1718dc0 Mon Sep 17 00:00:00 2001 From: Vlada Dusek Date: Tue, 17 Nov 2026 10:50:04 +0100 Subject: [PATCH 2/4] test: apply the Stagehand binary fixture to the whole plugin test module --- tests/integration/browsers/test_stagehand_browser_plugin.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/integration/browsers/test_stagehand_browser_plugin.py b/tests/integration/browsers/test_stagehand_browser_plugin.py index 21ddd09f90..23592a26b5 100644 --- a/tests/integration/browsers/test_stagehand_browser_plugin.py +++ b/tests/integration/browsers/test_stagehand_browser_plugin.py @@ -14,6 +14,8 @@ _PATCH_MODULE = 'crawlee.browsers._stagehand_browser_plugin' +pytestmark = pytest.mark.usefixtures('stagehand_binary') + @pytest.fixture def stagehand_client_mock() -> MagicMock: @@ -24,7 +26,7 @@ def stagehand_client_mock() -> MagicMock: @pytest.fixture -async def plugin(stagehand_binary: None) -> AsyncGenerator[StagehandBrowserPlugin, None]: # noqa: ARG001 +async def plugin() -> AsyncGenerator[StagehandBrowserPlugin, None]: async with StagehandBrowserPlugin() as plugin: yield plugin @@ -69,7 +71,6 @@ async def test_stagehand_called_with_browserbase_params(stagehand_client_mock: M assert 'local_chrome_path' not in call_kwargs -@pytest.mark.usefixtures('stagehand_binary') async def test_methods_raise_error_when_not_active() -> None: plugin = StagehandBrowserPlugin() From 8fd7afa77ef76c8566be6a9c00cd61454854faca Mon Sep 17 00:00:00 2001 From: Vlada Dusek Date: Tue, 17 Nov 2026 10:51:23 +0100 Subject: [PATCH 3/4] test: lock the Stagehand binary cache across concurrent pytest sessions --- tests/integration/conftest.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index ca252f0917..26913ef535 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -6,7 +6,7 @@ import pytest from filelock import FileLock from proxy import Proxy -from stagehand._custom.sea_binary import resolve_binary_path +from stagehand._custom.sea_binary import _cache_dir, resolve_binary_path from uvicorn.config import Config from crawlee.proxy_configuration import ProxyInfo @@ -29,11 +29,14 @@ @pytest.fixture(scope='session') -def stagehand_binary(tmp_path_factory: pytest.TempPathFactory) -> None: - """Populate Stagehand's per-user binary cache once, serialized across xdist workers.""" +def stagehand_binary() -> None: + """Populate Stagehand's per-user binary cache once, serialized across xdist workers and pytest sessions.""" # Stagehand copies its server binary into the cache on first use without a lock, so concurrent first uses race on - # the copy (`WinError 32` on Windows, `FileNotFoundError` elsewhere). - with FileLock(tmp_path_factory.getbasetemp().parent / 'stagehand_binary.lock'): + # the copy (`WinError 32` on Windows, `FileNotFoundError` elsewhere). The lock sits next to the cache, so it also + # covers concurrent pytest sessions of the same user. + cache_dir = _cache_dir() + cache_dir.mkdir(parents=True, exist_ok=True) + with FileLock(cache_dir / 'populate.lock'): resolve_binary_path() From ce8ebb9c3a568164e488eca3baf9d56820eaf3d3 Mon Sep 17 00:00:00 2001 From: Vlada Dusek Date: Tue, 17 Nov 2026 10:52:52 +0100 Subject: [PATCH 4/4] test: resolve the Stagehand binary for the version the client uses --- tests/integration/conftest.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 26913ef535..d66ed82eae 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -6,6 +6,7 @@ import pytest from filelock import FileLock from proxy import Proxy +from stagehand import __version__ as stagehand_version from stagehand._custom.sea_binary import _cache_dir, resolve_binary_path from uvicorn.config import Config @@ -33,11 +34,12 @@ def stagehand_binary() -> None: """Populate Stagehand's per-user binary cache once, serialized across xdist workers and pytest sessions.""" # Stagehand copies its server binary into the cache on first use without a lock, so concurrent first uses race on # the copy (`WinError 32` on Windows, `FileNotFoundError` elsewhere). The lock sits next to the cache, so it also - # covers concurrent pytest sessions of the same user. + # covers concurrent pytest sessions of the same user. The version matches the one `AsyncStagehand` resolves, so a + # set `STAGEHAND_VERSION` can't make the fixture fill a different cache directory. cache_dir = _cache_dir() cache_dir.mkdir(parents=True, exist_ok=True) with FileLock(cache_dir / 'populate.lock'): - resolve_binary_path() + resolve_binary_path(version=stagehand_version) def _proxy_info(port: int) -> ProxyInfo: