Skip to content

Commit

Permalink
Mitigate directory creation race condition (#10607)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Kadino committed Jan 6, 2023
1 parent 6bf7f55 commit 3ad4344
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/10607.bugfix.rst
@@ -0,0 +1 @@
Fix a race condition when creating junitxml reports, which could occur when multiple instances of pytest execute in parallel.
4 changes: 2 additions & 2 deletions src/_pytest/junitxml.py
Expand Up @@ -645,8 +645,8 @@ def pytest_sessionstart(self) -> None:

def pytest_sessionfinish(self) -> None:
dirname = os.path.dirname(os.path.abspath(self.logfile))
if not os.path.isdir(dirname):
os.makedirs(dirname)
# exist_ok avoids filesystem race conditions between checking path existence and requesting creation
os.makedirs(dirname, exist_ok=True)

with open(self.logfile, "w", encoding="utf-8") as logfile:
suite_stop_time = timing.time()
Expand Down

0 comments on commit 3ad4344

Please sign in to comment.