diff --git a/pytest-embedded/pyproject.toml b/pytest-embedded/pyproject.toml index 1b003fc1..c2366184 100644 --- a/pytest-embedded/pyproject.toml +++ b/pytest-embedded/pyproject.toml @@ -31,6 +31,7 @@ requires-python = ">=3.7" dependencies = [ "pytest>=7.0", "pexpect>=4.4", + "filelock>=3.12.2" ] [project.urls] diff --git a/pytest-embedded/pytest_embedded/plugin.py b/pytest-embedded/pytest_embedded/plugin.py index 7df02e78..e95bbdbf 100644 --- a/pytest-embedded/pytest_embedded/plugin.py +++ b/pytest-embedded/pytest_embedded/plugin.py @@ -17,6 +17,7 @@ from collections import Counter from operator import itemgetter +import filelock import pytest from _pytest.config import Config from _pytest.fixtures import ( @@ -625,18 +626,20 @@ def cache_dir(request: FixtureRequest) -> str: def port_target_cache(cache_dir) -> t.Dict[str, str]: """Session scoped port-target cache, for esp only""" _cache_file_path = os.path.join(cache_dir, 'port_target_cache') + lock = filelock.FileLock(f'{_cache_file_path}.lock') resp: t.Dict[str, str] = {} - try: - with shelve.open(_cache_file_path) as f: - resp = dict(f) - except dbm.error: - os.remove(_cache_file_path) + with lock: + try: + with shelve.open(_cache_file_path) as f: + resp = dict(f) + except dbm.error: + os.remove(_cache_file_path) yield resp - - with shelve.open(_cache_file_path) as f: - for k, v in resp.items(): - f[k] = v + with lock: + with shelve.open(_cache_file_path) as f: + for k, v in resp.items(): + f[k] = v @pytest.fixture(scope='session')