Skip to content

Conversation

@horw
Copy link
Collaborator

@horw horw commented Jun 13, 2025

Description

We had an issue with a cache file race condition while running pytest-xdist. One possible solution is to add filelock, which ensures synchronized access.

Related

Fixed #354

Checklist

Before submitting a Pull Request, please ensure the following:

  • 🚨 This PR does not introduce breaking changes.
  • All CI checks (GH Actions) pass.
  • Documentation is updated as needed.
  • Tests are updated or added as necessary.
  • Code is well-commented, especially in complex areas.
  • Git history is clean — commits are squashed to the minimum necessary.

@horw
Copy link
Collaborator Author

horw commented Jun 13, 2025

I’m trying to use filelock>=3.12.2 due to compatibility issues with the Python version.

@horw horw requested a review from hfudev June 13, 2025 08:34
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
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
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.

@hfudev
Copy link
Member

hfudev commented Jun 13, 2025

LGTM. I like the idea with the file lock which could work on all OSes.

@hfudev hfudev merged commit f7e2991 into main Jun 13, 2025
3 of 5 checks passed
@hfudev hfudev deleted the fix/filelock branch June 13, 2025 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

port-target-cache not available with pytest-xdist (RDT-1277)

3 participants