[3.12] GH-89727: Fix FD leak on os.fwalk() generator finalization. (GH-119766) (#119768)

GH-89727: Fix FD leak on `os.fwalk()` generator finalization. (GH-119766)

Follow-up to 3c890b50. Ensure we `os.close()` open file descriptors when
the `os.fwalk()` generator is finalized.
(cherry picked from commit a5fef800d3)

Co-authored-by: Barney Gale <barney.gale@gmail.com>
This commit is contained in:
Miss Islington (bot) 2024-05-30 06:30:37 +02:00 committed by GitHub
parent aae371bda4
commit d4a146d567
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 30 additions and 2 deletions

View file

@ -478,8 +478,15 @@ if {open, stat} <= supports_dir_fd and {scandir, stat} <= supports_fd:
top = fspath(top) top = fspath(top)
stack = [(_fwalk_walk, (True, dir_fd, top, top, None))] stack = [(_fwalk_walk, (True, dir_fd, top, top, None))]
isbytes = isinstance(top, bytes) isbytes = isinstance(top, bytes)
try:
while stack: while stack:
yield from _fwalk(stack, isbytes, topdown, onerror, follow_symlinks) yield from _fwalk(stack, isbytes, topdown, onerror, follow_symlinks)
finally:
# Close any file descriptors still on the stack.
while stack:
action, value = stack.pop()
if action == _fwalk_close:
close(value)
# Each item in the _fwalk() stack is a pair (action, args). # Each item in the _fwalk() stack is a pair (action, args).
_fwalk_walk = 0 # args: (isroot, dirfd, toppath, topname, entry) _fwalk_walk = 0 # args: (isroot, dirfd, toppath, topname, entry)

View file

@ -1671,6 +1671,27 @@ class FwalkTests(WalkTests):
self.addCleanup(os.close, newfd) self.addCleanup(os.close, newfd)
self.assertEqual(newfd, minfd) self.assertEqual(newfd, minfd)
@unittest.skipIf(
support.is_emscripten, "Cannot dup stdout on Emscripten"
)
@unittest.skipIf(
support.is_android, "dup return value is unpredictable on Android"
)
def test_fd_finalization(self):
# Check that close()ing the fwalk() generator closes FDs
def getfd():
fd = os.dup(1)
os.close(fd)
return fd
for topdown in (False, True):
old_fd = getfd()
it = self.fwalk(os_helper.TESTFN, topdown=topdown)
self.assertEqual(getfd(), old_fd)
next(it)
self.assertGreater(getfd(), old_fd)
it.close()
self.assertEqual(getfd(), old_fd)
# fwalk() keeps file descriptors open # fwalk() keeps file descriptors open
test_walk_many_open_files = None test_walk_many_open_files = None