Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve potential race condition in JSONFileCache #2721

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

msw-kialo
Copy link

The JSONFileCache.__setitems__ might raise a FileExistsError if it is
run in parallel by two threads / processes:

  1. Both processes discover the working dir hasn't been created yet.
  2. The first process creates the working dir.
  3. The second process tries to create the working dir, too and fails
    as it already exists.

Accept this potential race — exist_ok=True will prevent os.makedirs
to raise an exception.

The os.path.isdir condition is technically no longer needed but kept
keeping the change small. Furthermore, it should be slightly faster
as the cache should exist most of the time.

The exist_ok parameter is supported since Python 3.2 / fixed in 3.4.1
and therefore available in all needed python versions.

The `JSONFileCache.__setitems__` function might raise a
`FileExistsError` if it is run in parallel by two threads / processes:
1. Both processes discover the working dir hasn't been created yet.
2. The first process creates the working dir.
3. The second process tries to create the working dir, too and fails
   as it already exists.

Accept this potential race — `exist_ok=True` will prevent `os.makedirs`
to raise an exception.

The `os.path.isdir` condition is technically no longer needed but kept
keeping the change small. Furthermore, it should be slightly faster
as the cache should exist most of the time.
@dlm6693 dlm6693 self-requested a review October 3, 2022 16:26
dlm6693
dlm6693 previously approved these changes Oct 3, 2022
Copy link
Contributor

@dlm6693 dlm6693 left a comment

Choose a reason for hiding this comment

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

Thanks for your submission @msw-kialo! This looks good to me, but we need more time to review this internally.

@dlm6693 dlm6693 self-requested a review October 3, 2022 19:29
@dlm6693 dlm6693 dismissed their stale review October 3, 2022 19:31

needs more internal review

@dlm6693 dlm6693 removed their request for review February 17, 2023 15:04
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.

None yet

2 participants