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

cacheprovider: fix "Directory not empty" crash from cache directory creation #12408

Merged
merged 1 commit into from
Jun 3, 2024
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 changelog/12381.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix possible "Directory not empty" crashes arising from concurent cache dir (``.pytest_cache``) creation. Regressed in pytest 8.2.0.
27 changes: 19 additions & 8 deletions src/_pytest/cacheprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# This plugin was not named "cache" to avoid conflicts with the external
# pytest-cache version.
import dataclasses
import errno
import json
import os
from pathlib import Path
Expand Down Expand Up @@ -227,14 +228,24 @@
with open(path.joinpath("CACHEDIR.TAG"), "xb") as f:
f.write(CACHEDIR_TAG_CONTENT)

path.rename(self._cachedir)
# Create a directory in place of the one we just moved so that `TemporaryDirectory`'s
# cleanup doesn't complain.
#
# TODO: pass ignore_cleanup_errors=True when we no longer support python < 3.10. See
# https://github.com/python/cpython/issues/74168. Note that passing delete=False would
# do the wrong thing in case of errors and isn't supported until python 3.12.
path.mkdir()
try:
Copy link
Member

Choose a reason for hiding this comment

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

making this testable needs to the act of filling and renaming into a new utility method,
its then possible to just invoke that method twice

this will be a slightly dirty test, but much smaller than trying threads or processes

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes it's possible, just over my effort threshold for this...

path.rename(self._cachedir)
except OSError as e:

Check warning on line 233 in src/_pytest/cacheprovider.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/cacheprovider.py#L233

Added line #L233 was not covered by tests
# If 2 concurrent pytests both race to the rename, the loser
# gets "Directory not empty" from the rename. In this case,
# everything is handled so just continue (while letting the
# temporary directory be cleaned up).
if e.errno != errno.ENOTEMPTY:
raise

Check warning on line 239 in src/_pytest/cacheprovider.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/cacheprovider.py#L239

Added line #L239 was not covered by tests
else:
# Create a directory in place of the one we just moved so that
# `TemporaryDirectory`'s cleanup doesn't complain.
#
# TODO: pass ignore_cleanup_errors=True when we no longer support python < 3.10.
# See https://github.com/python/cpython/issues/74168. Note that passing
# delete=False would do the wrong thing in case of errors and isn't supported
# until python 3.12.
path.mkdir()


class LFPluginCollWrapper:
Expand Down
Loading