mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
[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 to3c890b50
. Ensure we `os.close()` open file descriptors when the `os.fwalk()` generator is finalized. (cherry picked from commita5fef800d3
) Co-authored-by: Barney Gale <barney.gale@gmail.com>
This commit is contained in:
parent
aae371bda4
commit
d4a146d567
2 changed files with 30 additions and 2 deletions
11
Lib/os.py
11
Lib/os.py
|
@ -478,8 +478,15 @@ if {open, stat} <= supports_dir_fd and {scandir, stat} <= supports_fd:
|
|||
top = fspath(top)
|
||||
stack = [(_fwalk_walk, (True, dir_fd, top, top, None))]
|
||||
isbytes = isinstance(top, bytes)
|
||||
while stack:
|
||||
yield from _fwalk(stack, isbytes, topdown, onerror, follow_symlinks)
|
||||
try:
|
||||
while stack:
|
||||
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).
|
||||
_fwalk_walk = 0 # args: (isroot, dirfd, toppath, topname, entry)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue