Skip to content

Filesystem race condition in junitxml #10604

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

Closed
Kadino opened this issue Dec 21, 2022 · 1 comment · Fixed by #10607
Closed

Filesystem race condition in junitxml #10604

Kadino opened this issue Dec 21, 2022 · 1 comment · Fixed by #10607
Labels
good first issue easy issue that is friendly to new contributor plugin: junitxml related to the junitxml builtin plugin type: bug problem that needs to be addressed

Comments

@Kadino
Copy link
Contributor

Kadino commented Dec 21, 2022

PyTest can intermittently fail when finalizing the run, if flag --junitxml=<path_to_file.xml> is set. This causes to errors similar to:

[...]
  File "D:\workspace\o3de\python\runtime\python-3.10.5-rev1-windows\python\lib\site-packages\_pytest\junitxml.py", line 650, in pytest_sessionfinish
    os.makedirs(dirname)
  File "D:\workspace\o3de\python\runtime\python-3.10.5-rev1-windows\python\lib\os.py", line 225, in makedirs
    mkdir(name, mode)
FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'D:\\workspace\\o3de\\build\\windows\\Testing\\Pytest'

This appears to primarily happen when running multiple invocations of pytest in parallel interpreters, where two instances may try to create the same folder at the same time. Though this has also reproduced in a self-test of a plugin, which invoked pytest within the context of another pytest run.

This has reproduced on pytest 6.2.5 and 7.2.0 on both Windows and Linux.

Output of pip list is attached piplist.txt

I am currently testing a fix to this by monkeypatching https://github.com/pytest-dev/pytest/blob/7.2.x/src/_pytest/junitxml.py#L649 to use os.makedirs(dirname, exist_ok=True) ...but I plan to collect more data on whether other race conditions still exist before proposing a PR.

@RonnyPfannschmidt
Copy link
Member

The code is from before exidts_ok was a thing

Im Absolutely on board with changing it as its a low hanging fruit nobody really hit before

@Zac-HD Zac-HD added type: bug problem that needs to be addressed good first issue easy issue that is friendly to new contributor plugin: junitxml related to the junitxml builtin plugin labels Dec 23, 2022
Kadino added a commit to Kadino/pytest that referenced this issue Dec 23, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
Fixes pytest-dev#10604 which could intermittently display unexpected behavior between checking if the path exists and requesting creation. This was fairly prevalent when pytest was being invoked in parallel by another test runner (CTest) and trying to create the same parent-folder for multiple XMLs. A modest amount of testing did not reproduce other filesystem race conditions.

This notably does not work around an edge case where the parent path of the XML could be created as a file instead of a folder or link. That vanishingly rare case should cause file creation to fail on the next line, with a fairly obvious exception message.
nicoddemus pushed a commit that referenced this issue Jan 6, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
Fixes #10604 which could intermittently display unexpected behavior between checking if the path exists and requesting creation. This was fairly prevalent when pytest was being invoked in parallel by another test runner (CTest) and trying to create the same parent-folder for multiple XMLs. A modest amount of testing did not reproduce other filesystem race conditions.

This notably does not work around an edge case where the parent path of the XML could be created as a file instead of a folder or link. That vanishingly rare case should cause file creation to fail on the next line, with a fairly obvious exception message.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue easy issue that is friendly to new contributor plugin: junitxml related to the junitxml builtin plugin type: bug problem that needs to be addressed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants