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

OSError: [Errno 39] Directory not empty when doing cache.mkdir() from session fixture + pytest-xdist #12381

Closed
4 tasks done
amezin opened this issue May 27, 2024 · 1 comment · Fixed by #12408
Closed
4 tasks done
Labels
plugin: cache related to the cache builtin plugin plugin: xdist related to the xdist external plugin type: bug problem that needs to be addressed type: regression indicates a problem that was introduced in a release which was working previously

Comments

@amezin
Copy link

amezin commented May 27, 2024

  • a detailed description of the bug or problem you are having
  • output of pip list from the virtual environment you are using
  • pytest and operating system versions
  • minimal example if possible

cache.mkdir() in a session-level fixture, in combination with xdist, sometimes results in OSError: [Errno 39] Directory not empty when pytest tries to rename the cache directory.

This is a regression in 8.2.0, most likely introduced by #12168 / #12167

On 8.1.x cache.mkdir() works fine in the same case.

In conftest.py:

@pytest.fixture(scope='session')
def my_fixture(request):
    return request.config.cache.mkdir('testdir')

In test_tests.py:

@pytest.mark.parametrize('n', list((n,) for n in range(42)))
def test_test(my_fixture, n):
    pass

Running with pytest -n auto results in:

self = PosixPath('/home/runner/work/pytest-cache-race/pytest-cache-race/.tox/python/pytest-cache-files-8ulls0nm')
target = PosixPath('/home/runner/work/pytest-cache-race/pytest-cache-race/.tox/python/.pytest_cache')

    def rename(self, target):
        """
        Rename this path to the target path.
    
        The target path may be absolute or relative. Relative paths are
        interpreted relative to the current working directory, *not* the
        directory of the Path object.
    
        Returns the new Path instance pointing to the target path.
        """
>       self._accessor.rename(self, target)
E       OSError: [Errno 39] Directory not empty: '/home/runner/work/pytest-cache-race/pytest-cache-race/.tox/python/pytest-cache-files-8ulls0nm' -> '/home/runner/work/pytest-cache-race/pytest-cache-race/.tox/python/.pytest_cache'

Operating System: Ubuntu 22.04.4 LTS
Python 3.10.12, pytest-8.2.1
Virtualenv content: exceptiongroup==1.2.1,execnet==2.1.1,iniconfig==2.0.0,packaging==24.0,pluggy==1.5.0,pytest==8.2.1,pytest-xdist==3.6.1,tomli==2.0.1

https://github.com/amezin/pytest-cache-race

Reproduced in GitHub Actions: https://github.com/amezin/pytest-cache-race/actions/runs/9261012859/job/25475719651

@bluetech
Copy link
Member

bluetech commented Jun 2, 2024

Hmm, so now we have a different race. I think we should be able to ignore this error, i.e. if it happens just continue (while removing the directory). This is because we can assume that the first worker correctly created the directory atomically.

@bluetech bluetech added type: bug problem that needs to be addressed plugin: xdist related to the xdist external plugin plugin: cache related to the cache builtin plugin type: regression indicates a problem that was introduced in a release which was working previously labels Jun 2, 2024
bluetech added a commit to bluetech/pytest that referenced this issue Jun 2, 2024
…reation

Fix pytest-dev#12381

Test plan:
It's possible to write a deterministic test case for this, but somewhat
of a hassle so I tested it manually. I reproduced by removing existing
`.pytest_cache`, adding a sleep before the rename and running two
pytests. I verified that it doesn't reproduce after the fix.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin: cache related to the cache builtin plugin plugin: xdist related to the xdist external plugin type: bug problem that needs to be addressed type: regression indicates a problem that was introduced in a release which was working previously
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants