From a6f85a0e3eabb07f79959455485d3a31c8526d53 Mon Sep 17 00:00:00 2001 From: Kadino Date: Fri, 6 Jan 2023 04:12:24 -0800 Subject: [PATCH] [7.2.x] Mitigate directory creation race condition --- changelog/10607.bugfix.rst | 1 + src/_pytest/junitxml.py | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) create mode 100644 changelog/10607.bugfix.rst diff --git a/changelog/10607.bugfix.rst b/changelog/10607.bugfix.rst new file mode 100644 index 00000000000..e89504695c2 --- /dev/null +++ b/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. diff --git a/src/_pytest/junitxml.py b/src/_pytest/junitxml.py index 7a5170f328b..9242d46d9df 100644 --- a/src/_pytest/junitxml.py +++ b/src/_pytest/junitxml.py @@ -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()