Skip to content

Commit 9caad1f

Browse files
[3.12] gh-116401: Fix blocking os.fwalk() and shutil.rmtree() on opening a named pipe (GH-116421) (GH-116716)
(cherry picked from commit aa7bcf2)
1 parent 9ef84b4 commit 9caad1f

File tree

6 files changed

+113
-8
lines changed

6 files changed

+113
-8
lines changed

Lib/os.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ def fwalk(top=".", topdown=True, onerror=None, *, follow_symlinks=False, dir_fd=
473473
# lstat()/open()/fstat() trick.
474474
if not follow_symlinks:
475475
orig_st = stat(top, follow_symlinks=False, dir_fd=dir_fd)
476-
topfd = open(top, O_RDONLY, dir_fd=dir_fd)
476+
topfd = open(top, O_RDONLY | O_NONBLOCK, dir_fd=dir_fd)
477477
try:
478478
if (follow_symlinks or (st.S_ISDIR(orig_st.st_mode) and
479479
path.samestat(orig_st, stat(topfd)))):
@@ -522,7 +522,7 @@ def _fwalk(topfd, toppath, isbytes, topdown, onerror, follow_symlinks):
522522
assert entries is not None
523523
name, entry = name
524524
orig_st = entry.stat(follow_symlinks=False)
525-
dirfd = open(name, O_RDONLY, dir_fd=topfd)
525+
dirfd = open(name, O_RDONLY | O_NONBLOCK, dir_fd=topfd)
526526
except OSError as err:
527527
if onerror is not None:
528528
onerror(err)

Lib/shutil.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ def _rmtree_safe_fd(topfd, path, onexc):
676676
continue
677677
if is_dir:
678678
try:
679-
dirfd = os.open(entry.name, os.O_RDONLY, dir_fd=topfd)
679+
dirfd = os.open(entry.name, os.O_RDONLY | os.O_NONBLOCK, dir_fd=topfd)
680680
dirfd_closed = False
681681
except OSError as err:
682682
onexc(os.open, fullname, err)
@@ -775,7 +775,7 @@ def onexc(*args):
775775
onexc(os.lstat, path, err)
776776
return
777777
try:
778-
fd = os.open(path, os.O_RDONLY, dir_fd=dir_fd)
778+
fd = os.open(path, os.O_RDONLY | os.O_NONBLOCK, dir_fd=dir_fd)
779779
fd_closed = False
780780
except Exception as err:
781781
onexc(os.open, path, err)

Lib/test/test_glob.py

+12
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,18 @@ def test_glob_non_directory(self):
343343
eq(self.rglob('nonexistent', '*'), [])
344344
eq(self.rglob('nonexistent', '**'), [])
345345

346+
@unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()')
347+
@unittest.skipIf(sys.platform == "vxworks",
348+
"fifo requires special path on VxWorks")
349+
def test_glob_named_pipe(self):
350+
path = os.path.join(self.tempdir, 'mypipe')
351+
os.mkfifo(path)
352+
self.assertEqual(self.rglob('mypipe'), [path])
353+
self.assertEqual(self.rglob('mypipe*'), [path])
354+
self.assertEqual(self.rglob('mypipe', ''), [])
355+
self.assertEqual(self.rglob('mypipe', 'sub'), [])
356+
self.assertEqual(self.rglob('mypipe', '*'), [])
357+
346358
def test_glob_many_open_files(self):
347359
depth = 30
348360
base = os.path.join(self.tempdir, 'deep')

Lib/test/test_os.py

+78-4
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,7 @@ def test_ror_operator(self):
12901290

12911291
class WalkTests(unittest.TestCase):
12921292
"""Tests for os.walk()."""
1293+
is_fwalk = False
12931294

12941295
# Wrapper to hide minor differences between os.walk and os.fwalk
12951296
# to tests both functions with the same code base
@@ -1324,14 +1325,14 @@ def setUp(self):
13241325
self.sub11_path = join(self.sub1_path, "SUB11")
13251326
sub2_path = join(self.walk_path, "SUB2")
13261327
sub21_path = join(sub2_path, "SUB21")
1327-
tmp1_path = join(self.walk_path, "tmp1")
1328+
self.tmp1_path = join(self.walk_path, "tmp1")
13281329
tmp2_path = join(self.sub1_path, "tmp2")
13291330
tmp3_path = join(sub2_path, "tmp3")
13301331
tmp5_path = join(sub21_path, "tmp3")
13311332
self.link_path = join(sub2_path, "link")
13321333
t2_path = join(os_helper.TESTFN, "TEST2")
13331334
tmp4_path = join(os_helper.TESTFN, "TEST2", "tmp4")
1334-
broken_link_path = join(sub2_path, "broken_link")
1335+
self.broken_link_path = join(sub2_path, "broken_link")
13351336
broken_link2_path = join(sub2_path, "broken_link2")
13361337
broken_link3_path = join(sub2_path, "broken_link3")
13371338

@@ -1341,13 +1342,13 @@ def setUp(self):
13411342
os.makedirs(sub21_path)
13421343
os.makedirs(t2_path)
13431344

1344-
for path in tmp1_path, tmp2_path, tmp3_path, tmp4_path, tmp5_path:
1345+
for path in self.tmp1_path, tmp2_path, tmp3_path, tmp4_path, tmp5_path:
13451346
with open(path, "x", encoding='utf-8') as f:
13461347
f.write("I'm " + path + " and proud of it. Blame test_os.\n")
13471348

13481349
if os_helper.can_symlink():
13491350
os.symlink(os.path.abspath(t2_path), self.link_path)
1350-
os.symlink('broken', broken_link_path, True)
1351+
os.symlink('broken', self.broken_link_path, True)
13511352
os.symlink(join('tmp3', 'broken'), broken_link2_path, True)
13521353
os.symlink(join('SUB21', 'tmp5'), broken_link3_path, True)
13531354
self.sub2_tree = (sub2_path, ["SUB21", "link"],
@@ -1443,6 +1444,11 @@ def test_walk_symlink(self):
14431444
else:
14441445
self.fail("Didn't follow symlink with followlinks=True")
14451446

1447+
walk_it = self.walk(self.broken_link_path, follow_symlinks=True)
1448+
if self.is_fwalk:
1449+
self.assertRaises(FileNotFoundError, next, walk_it)
1450+
self.assertRaises(StopIteration, next, walk_it)
1451+
14461452
def test_walk_bad_dir(self):
14471453
# Walk top-down.
14481454
errors = []
@@ -1464,6 +1470,73 @@ def test_walk_bad_dir(self):
14641470
finally:
14651471
os.rename(path1new, path1)
14661472

1473+
def test_walk_bad_dir2(self):
1474+
walk_it = self.walk('nonexisting')
1475+
if self.is_fwalk:
1476+
self.assertRaises(FileNotFoundError, next, walk_it)
1477+
self.assertRaises(StopIteration, next, walk_it)
1478+
1479+
walk_it = self.walk('nonexisting', follow_symlinks=True)
1480+
if self.is_fwalk:
1481+
self.assertRaises(FileNotFoundError, next, walk_it)
1482+
self.assertRaises(StopIteration, next, walk_it)
1483+
1484+
walk_it = self.walk(self.tmp1_path)
1485+
self.assertRaises(StopIteration, next, walk_it)
1486+
1487+
walk_it = self.walk(self.tmp1_path, follow_symlinks=True)
1488+
if self.is_fwalk:
1489+
self.assertRaises(NotADirectoryError, next, walk_it)
1490+
self.assertRaises(StopIteration, next, walk_it)
1491+
1492+
@unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()')
1493+
@unittest.skipIf(sys.platform == "vxworks",
1494+
"fifo requires special path on VxWorks")
1495+
def test_walk_named_pipe(self):
1496+
path = os_helper.TESTFN + '-pipe'
1497+
os.mkfifo(path)
1498+
self.addCleanup(os.unlink, path)
1499+
1500+
walk_it = self.walk(path)
1501+
self.assertRaises(StopIteration, next, walk_it)
1502+
1503+
walk_it = self.walk(path, follow_symlinks=True)
1504+
if self.is_fwalk:
1505+
self.assertRaises(NotADirectoryError, next, walk_it)
1506+
self.assertRaises(StopIteration, next, walk_it)
1507+
1508+
@unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()')
1509+
@unittest.skipIf(sys.platform == "vxworks",
1510+
"fifo requires special path on VxWorks")
1511+
def test_walk_named_pipe2(self):
1512+
path = os_helper.TESTFN + '-dir'
1513+
os.mkdir(path)
1514+
self.addCleanup(shutil.rmtree, path)
1515+
os.mkfifo(os.path.join(path, 'mypipe'))
1516+
1517+
errors = []
1518+
walk_it = self.walk(path, onerror=errors.append)
1519+
next(walk_it)
1520+
self.assertRaises(StopIteration, next, walk_it)
1521+
self.assertEqual(errors, [])
1522+
1523+
errors = []
1524+
walk_it = self.walk(path, onerror=errors.append)
1525+
root, dirs, files = next(walk_it)
1526+
self.assertEqual(root, path)
1527+
self.assertEqual(dirs, [])
1528+
self.assertEqual(files, ['mypipe'])
1529+
dirs.extend(files)
1530+
files.clear()
1531+
if self.is_fwalk:
1532+
self.assertRaises(NotADirectoryError, next, walk_it)
1533+
self.assertRaises(StopIteration, next, walk_it)
1534+
if self.is_fwalk:
1535+
self.assertEqual(errors, [])
1536+
else:
1537+
self.assertEqual(len(errors), 1, errors)
1538+
self.assertIsInstance(errors[0], NotADirectoryError)
1539+
14671540
def test_walk_many_open_files(self):
14681541
depth = 30
14691542
base = os.path.join(os_helper.TESTFN, 'deep')
@@ -1529,6 +1602,7 @@ def test_walk_above_recursion_limit(self):
15291602
@unittest.skipUnless(hasattr(os, 'fwalk'), "Test needs os.fwalk()")
15301603
class FwalkTests(WalkTests):
15311604
"""Tests for os.fwalk()."""
1605+
is_fwalk = True
15321606

15331607
def walk(self, top, **kwargs):
15341608
for root, dirs, files, root_fd in self.fwalk(top, **kwargs):

Lib/test/test_shutil.py

+17
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,23 @@ def test_rmtree_on_junction(self):
669669
finally:
670670
shutil.rmtree(TESTFN, ignore_errors=True)
671671

672+
@unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()')
673+
@unittest.skipIf(sys.platform == "vxworks",
674+
"fifo requires special path on VxWorks")
675+
def test_rmtree_on_named_pipe(self):
676+
os.mkfifo(TESTFN)
677+
try:
678+
with self.assertRaises(NotADirectoryError):
679+
shutil.rmtree(TESTFN)
680+
self.assertTrue(os.path.exists(TESTFN))
681+
finally:
682+
os.unlink(TESTFN)
683+
684+
os.mkdir(TESTFN)
685+
os.mkfifo(os.path.join(TESTFN, 'mypipe'))
686+
shutil.rmtree(TESTFN)
687+
self.assertFalse(os.path.exists(TESTFN))
688+
672689

673690
class TestCopyTree(BaseTest, unittest.TestCase):
674691

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix blocking :func:`os.fwalk` and :func:`shutil.rmtree` on opening named
2+
pipe.

0 commit comments

Comments
 (0)