Skip to content

Commit f7e2991

Browse files
authored
Merge pull request #359 from espressif/fix/filelock
fix: synced cache file by adding support for filelock
2 parents 5f412f1 + 793c1be commit f7e2991

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

pytest-embedded/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ requires-python = ">=3.7"
3131
dependencies = [
3232
"pytest>=7.0",
3333
"pexpect>=4.4",
34+
"filelock>=3.12.2"
3435
]
3536

3637
[project.urls]

pytest-embedded/pytest_embedded/plugin.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from collections import Counter
1818
from operator import itemgetter
1919

20+
import filelock
2021
import pytest
2122
from _pytest.config import Config
2223
from _pytest.fixtures import (
@@ -625,18 +626,20 @@ def cache_dir(request: FixtureRequest) -> str:
625626
def port_target_cache(cache_dir) -> t.Dict[str, str]:
626627
"""Session scoped port-target cache, for esp only"""
627628
_cache_file_path = os.path.join(cache_dir, 'port_target_cache')
629+
lock = filelock.FileLock(f'{_cache_file_path}.lock')
628630
resp: t.Dict[str, str] = {}
629-
try:
630-
with shelve.open(_cache_file_path) as f:
631-
resp = dict(f)
632-
except dbm.error:
633-
os.remove(_cache_file_path)
631+
with lock:
632+
try:
633+
with shelve.open(_cache_file_path) as f:
634+
resp = dict(f)
635+
except dbm.error:
636+
os.remove(_cache_file_path)
634637

635638
yield resp
636-
637-
with shelve.open(_cache_file_path) as f:
638-
for k, v in resp.items():
639-
f[k] = v
639+
with lock:
640+
with shelve.open(_cache_file_path) as f:
641+
for k, v in resp.items():
642+
f[k] = v
640643

641644

642645
@pytest.fixture(scope='session')

0 commit comments

Comments
 (0)