Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pytest-embedded/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ requires-python = ">=3.7"
dependencies = [
"pytest>=7.0",
"pexpect>=4.4",
"filelock>=3.12.2"
]

[project.urls]
Expand Down
21 changes: 12 additions & 9 deletions pytest-embedded/pytest_embedded/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One question, should we check the lock file at the beginning of the whole process, and remove it if it exists? (fixture session)

Will the lock file be left over if the last pytest process got terminated for whatever reason?

Copy link
Copy Markdown
Collaborator Author

@horw horw Jun 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you mentioned, the main problem with pytest-xdist is that it runs independent processes. I guess if we try to remove files manually, there might be a race condition. If the processes are killed, the system automatically releases the file lock, so I think it's okay. This library uses system calls to achieve synchronization.

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')
Expand Down
Loading