Skip to content

Commit d7abeb7

Browse files
authored
[3.5] bpo-29694: race condition in pathlib mkdir with flags parents=True (GH-1089). (GH-1127)
(cherry picked from commit 22a594a)
1 parent 118cf91 commit d7abeb7

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

Lib/pathlib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,8 +1217,8 @@ def mkdir(self, mode=0o777, parents=False, exist_ok=False):
12171217
except FileNotFoundError:
12181218
if not parents or self.parent == self:
12191219
raise
1220-
self.parent.mkdir(parents=True)
1221-
self._accessor.mkdir(self, mode)
1220+
self.parent.mkdir(parents=True, exist_ok=True)
1221+
self.mkdir(mode, parents=False, exist_ok=exist_ok)
12221222
except OSError:
12231223
# Cannot rely on checking for EEXIST, since the operating system
12241224
# could give priority to other errors like EACCES or EROFS

Lib/test/test_pathlib.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import stat
99
import tempfile
1010
import unittest
11+
from unittest import mock
1112

1213
from test import support
1314
TESTFN = support.TESTFN
@@ -1767,6 +1768,35 @@ def test_mkdir_no_parents_file(self):
17671768
p.mkdir(exist_ok=True)
17681769
self.assertEqual(cm.exception.errno, errno.EEXIST)
17691770

1771+
def test_mkdir_concurrent_parent_creation(self):
1772+
for pattern_num in range(32):
1773+
p = self.cls(BASE, 'dirCPC%d' % pattern_num)
1774+
self.assertFalse(p.exists())
1775+
1776+
def my_mkdir(path, mode=0o777):
1777+
path = str(path)
1778+
# Emulate another process that would create the directory
1779+
# just before we try to create it ourselves. We do it
1780+
# in all possible pattern combinations, assuming that this
1781+
# function is called at most 5 times (dirCPC/dir1/dir2,
1782+
# dirCPC/dir1, dirCPC, dirCPC/dir1, dirCPC/dir1/dir2).
1783+
if pattern.pop():
1784+
os.mkdir(path, mode) # from another process
1785+
concurrently_created.add(path)
1786+
os.mkdir(path, mode) # our real call
1787+
1788+
pattern = [bool(pattern_num & (1 << n)) for n in range(5)]
1789+
concurrently_created = set()
1790+
p12 = p / 'dir1' / 'dir2'
1791+
try:
1792+
with mock.patch("pathlib._normal_accessor.mkdir", my_mkdir):
1793+
p12.mkdir(parents=True, exist_ok=False)
1794+
except FileExistsError:
1795+
self.assertIn(str(p12), concurrently_created)
1796+
else:
1797+
self.assertNotIn(str(p12), concurrently_created)
1798+
self.assertTrue(p.exists())
1799+
17701800
@with_symlinks
17711801
def test_symlink_to(self):
17721802
P = self.cls(BASE)

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ Extension Modules
4848

4949
Library
5050
-------
51+
52+
- bpo-29694: Fixed race condition in pathlib mkdir with flags
53+
parents=True. Patch by Armin Rigo.
54+
5155
- bpo-29692: Fixed arbitrary unchaining of RuntimeError exceptions in
5256
contextlib.contextmanager.
5357
Patch by Siddharth Velankar.

0 commit comments

Comments
 (0)