summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClément Pit-Claudel <cpitclaudel@users.noreply.github.com>2021-09-02 20:53:51 +0000
committerGitHub <noreply@github.com>2021-09-02 13:53:51 -0700
commitf531212a2c26700737282f0b409381adf9e30dd9 (patch)
treedf826e73b95ae8a71e53ccbac589af670a1b1d65
parentc0a981148c05c0332e2fd51d426d880055959ca6 (diff)
downloadpython-coveragepy-git-f531212a2c26700737282f0b409381adf9e30dd9.tar.gz
fix: Avoid a race condition in misc.ensure_dir (#1220)
* coverage/misc.py (ensure_dir): Pass exist_ok to os.makedirs, ensuring that if two concurrent instances of coverage.py entering this function at the same time won't fail with FileExistsError. Sample backtrace: Traceback (most recent call last): File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main return _run_code(code, main_globals, None, File "/usr/lib/python3.8/runpy.py", line 87, in _run_code exec(code, run_globals) File "/home/user/.local/lib/python3.8/site-packages/coverage/__main__.py", line 8, in <module> sys.exit(main()) File "/home/user/.local/lib/python3.8/site-packages/coverage/cmdline.py", line 871, in main status = CoverageScript().command_line(argv) File "/home/user/.local/lib/python3.8/site-packages/coverage/cmdline.py", line 588, in command_line return self.do_run(options, args) File "/home/user/.local/lib/python3.8/site-packages/coverage/cmdline.py", line 743, in do_run self.coverage.start() File "/home/user/.local/lib/python3.8/site-packages/coverage/control.py", line 535, in start self._init_for_start() File "/home/user/.local/lib/python3.8/site-packages/coverage/control.py", line 474, in _init_for_start self._init_data(suffix) File "/home/user/.local/lib/python3.8/site-packages/coverage/control.py", line 512, in _init_data ensure_dir_for_file(self.config.data_file) File "/home/user/.local/lib/python3.8/site-packages/coverage/misc.py", line 165, in ensure_dir_for_file ensure_dir(os.path.dirname(path)) File "/.local/lib/python3.8/site-packages/coverage/misc.py", line 160, in ensure_dir os.makedirs(directory) File "/usr/lib/python3.8/os.py", line 223, in makedirs mkdir(name, mode)
-rw-r--r--coverage/misc.py4
1 files changed, 2 insertions, 2 deletions
diff --git a/coverage/misc.py b/coverage/misc.py
index 91ccd964..11dad23e 100644
--- a/coverage/misc.py
+++ b/coverage/misc.py
@@ -156,8 +156,8 @@ def ensure_dir(directory):
If `directory` is None or empty, do nothing.
"""
- if directory and not os.path.isdir(directory):
- os.makedirs(directory)
+ if directory:
+ os.makedirs(directory, exist_ok=True)
def ensure_dir_for_file(path):