Skip to content

Commit 91a79c7

Browse files
authored
test: fix Stagehand binary cache race in plugin tests (#2460)
`test_methods_raise_error_when_not_active` creates a real `AsyncStagehand`, which copies the Stagehand binary into the per-user cache. It did not use the `stagehand_binary` fixture, so it skipped the `FileLock` added in #2418. With `--dist worksteal`, it can run on one worker while another worker is inside the locked fixture. Both write the same `.tmp` file and then rename it, and the rename fails with `WinError 32` on Windows ([failed run](https://github.com/apify/crawlee-python/actions/runs/44047531630/job/133916850608)) or `FileNotFoundError` on Linux. Changes: - Every test in `test_stagehand_browser_plugin.py` uses the `stagehand_binary` fixture through a module-level `pytestmark`, so a new test in the file can't skip the lock. - The lock file sits next to the per-user cache, not in the pytest session's temp directory. Two pytest sessions running at the same time (for example, in two worktrees) share one lock. - The fixture resolves the binary for `stagehand.__version__`, the version `AsyncStagehand` uses. Before, a set `STAGEHAND_VERSION` made it fill a different cache directory than the client. Each change was checked locally with a 3 s delay patched in between the write and the rename, and a fresh cache for each run: - Plugin tests: 20 of 20 runs failed before, 0 of 100 after. - Two concurrent pytest sessions: 3 of 3 failed with the old lock path, 0 of 3 with the new one. - `STAGEHAND_VERSION` set: 3 of 3 failed before, 0 of 3 after. *✍️ Drafted by Claude Code*
1 parent 4275339 commit 91a79c7

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

‎tests/integration/browsers/test_stagehand_browser_plugin.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
_PATCH_MODULE = 'crawlee.browsers._stagehand_browser_plugin'
1616

17+
pytestmark = pytest.mark.usefixtures('stagehand_binary')
18+
1719

1820
@pytest.fixture
1921
def stagehand_client_mock() -> MagicMock:
@@ -24,7 +26,7 @@ def stagehand_client_mock() -> MagicMock:
2426

2527

2628
@pytest.fixture
27-
async def plugin(stagehand_binary: None) -> AsyncGenerator[StagehandBrowserPlugin, None]: # noqa: ARG001
29+
async def plugin() -> AsyncGenerator[StagehandBrowserPlugin, None]:
2830
async with StagehandBrowserPlugin() as plugin:
2931
yield plugin
3032

‎tests/integration/conftest.py‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import pytest
77
from filelock import FileLock
88
from proxy import Proxy
9-
from stagehand._custom.sea_binary import resolve_binary_path
9+
from stagehand import __version__ as stagehand_version
10+
from stagehand._custom.sea_binary import _cache_dir, resolve_binary_path
1011
from uvicorn.config import Config
1112

1213
from crawlee.proxy_configuration import ProxyInfo
@@ -29,12 +30,16 @@
2930

3031

3132
@pytest.fixture(scope='session')
32-
def stagehand_binary(tmp_path_factory: pytest.TempPathFactory) -> None:
33-
"""Populate Stagehand's per-user binary cache once, serialized across xdist workers."""
33+
def stagehand_binary() -> None:
34+
"""Populate Stagehand's per-user binary cache once, serialized across xdist workers and pytest sessions."""
3435
# Stagehand copies its server binary into the cache on first use without a lock, so concurrent first uses race on
35-
# the copy (`WinError 32` on Windows, `FileNotFoundError` elsewhere).
36-
with FileLock(tmp_path_factory.getbasetemp().parent / 'stagehand_binary.lock'):
37-
resolve_binary_path()
36+
# the copy (`WinError 32` on Windows, `FileNotFoundError` elsewhere). The lock sits next to the cache, so it also
37+
# covers concurrent pytest sessions of the same user. The version matches the one `AsyncStagehand` resolves, so a
38+
# set `STAGEHAND_VERSION` can't make the fixture fill a different cache directory.
39+
cache_dir = _cache_dir()
40+
cache_dir.mkdir(parents=True, exist_ok=True)
41+
with FileLock(cache_dir / 'populate.lock'):
42+
resolve_binary_path(version=stagehand_version)
3843

3944

4045
def _proxy_info(port: int) -> ProxyInfo:

0 commit comments

Comments
 (0)