Repository navigation
bpo-29694: race condition in pathlib mkdir with flags parents=True #1089
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| import stat | ||
| import tempfile | ||
| import unittest | ||
| from unittest import mock | ||
|
|
||
| from test import support | ||
| android_not_root = support.android_not_root | ||
|
|
@@ -1801,6 +1802,36 @@ def test_mkdir_no_parents_file(self): | |
| p.mkdir(exist_ok=True) | ||
| self.assertEqual(cm.exception.errno, errno.EEXIST) | ||
|
|
||
| def test_mkdir_concurrent_parent_creation(self): | ||
| for pattern_num in range(32): | ||
| p = self.cls(BASE, 'dirCPC%d' % pattern_num) | ||
| self.assertFalse(p.exists()) | ||
|
|
||
| def my_mkdir(path, mode=0o777): | ||
| path = str(path) | ||
| # Emulate another process that would create the directory | ||
| # just before we try to create it ourselves. We do it | ||
| # in all possible pattern combinations, assuming that this | ||
| # function is called at most 5 times (dirCPC/dir1/dir2, | ||
| # dirCPC/dir1, dirCPC, dirCPC/dir1, cirCPC/dir1/dir2). | ||
| if pattern.pop(): | ||
| os.mkdir(path, mode) # from another process | ||
| concurrently_created.add(path) | ||
| os.mkdir(path, mode) # our real call | ||
| org_mkdir = pathlib._normal_accessor.mkdir | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems this is not used. |
||
|
|
||
| pattern = [bool(pattern_num & (1 << n)) for n in range(5)] | ||
| concurrently_created = set() | ||
| p12 = p / 'dir1' / 'dir2' | ||
| try: | ||
| with mock.patch("pathlib._normal_accessor.mkdir", my_mkdir): | ||
| p12.mkdir(parents=True, exist_ok=False) | ||
| got_exception = False | ||
| except FileExistsError: | ||
| got_exception = True | ||
| self.assertEqual(str(p12) in concurrently_created, got_exception) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| self.assertTrue(p.exists()) | ||
|
|
||
| @support.skip_unless_symlink | ||
| def test_symlink_to(self): | ||
| P = self.cls(BASE) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -306,6 +306,9 @@ Extension Modules | |
|
|
||
| Library | ||
| ------- | ||
|
|
||
| - bpo-29694: race condition in pathlib mkdir with flags parents=True | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add "Fixed" at the start, add a period at the end. |
||
|
|
||
| - bpo-29692: Fixed arbitrary unchaining of RuntimeError exceptions in | ||
| contextlib.contextmanager. | ||
| Patch by Siddharth Velankar. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cirCPC looks like a typo here.