[Python-checkins] bpo-39327: Close file descriptors as soon as possible in shutil.rmtree (GH-31384)
serhiy-storchaka
webhook-mailer at python.org
Sun Feb 20 11:02:25 EST 2022
https://github.com/python/cpython/commit/b77158b4da449ec5b8f682816a79d004fd65ed07
commit: b77158b4da449ec5b8f682816a79d004fd65ed07
branch: main
author: Lital Natan <litaln at gmail.com>
committer: serhiy-storchaka <storchaka at gmail.com>
date: 2022-02-20T18:02:10+02:00
summary:
bpo-39327: Close file descriptors as soon as possible in shutil.rmtree (GH-31384)
It fixes the "Text File Busy" OSError when using 'rmtree' on a
windows-managed filesystem in via the VirtualBox shared folder
(and possible other scenarios like a windows-managed network file
system).
files:
A Misc/NEWS.d/next/Library/2022-02-17-13-10-50.bpo-39327.ytIT7Z.rst
M Lib/shutil.py
M Misc/ACKS
diff --git a/Lib/shutil.py b/Lib/shutil.py
index 949e024853c1d..eb768f9e03b7d 100644
--- a/Lib/shutil.py
+++ b/Lib/shutil.py
@@ -648,6 +648,7 @@ def _rmtree_safe_fd(topfd, path, onerror):
if is_dir:
try:
dirfd = os.open(entry.name, os.O_RDONLY, dir_fd=topfd)
+ dirfd_closed = False
except OSError:
onerror(os.open, fullname, sys.exc_info())
else:
@@ -655,6 +656,8 @@ def _rmtree_safe_fd(topfd, path, onerror):
if os.path.samestat(orig_st, os.fstat(dirfd)):
_rmtree_safe_fd(dirfd, fullname, onerror)
try:
+ os.close(dirfd)
+ dirfd_closed = True
os.rmdir(entry.name, dir_fd=topfd)
except OSError:
onerror(os.rmdir, fullname, sys.exc_info())
@@ -668,7 +671,8 @@ def _rmtree_safe_fd(topfd, path, onerror):
except OSError:
onerror(os.path.islink, fullname, sys.exc_info())
finally:
- os.close(dirfd)
+ if not dirfd_closed:
+ os.close(dirfd)
else:
try:
os.unlink(entry.name, dir_fd=topfd)
@@ -711,6 +715,7 @@ def onerror(*args):
return
try:
fd = os.open(path, os.O_RDONLY)
+ fd_closed = False
except Exception:
onerror(os.open, path, sys.exc_info())
return
@@ -718,6 +723,8 @@ def onerror(*args):
if os.path.samestat(orig_st, os.fstat(fd)):
_rmtree_safe_fd(fd, path, onerror)
try:
+ os.close(fd)
+ fd_closed = True
os.rmdir(path)
except OSError:
onerror(os.rmdir, path, sys.exc_info())
@@ -728,7 +735,8 @@ def onerror(*args):
except OSError:
onerror(os.path.islink, path, sys.exc_info())
finally:
- os.close(fd)
+ if not fd_closed:
+ os.close(fd)
else:
try:
if _rmtree_islink(path):
diff --git a/Misc/ACKS b/Misc/ACKS
index dceb2b628eb20..bab04b4613646 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -429,6 +429,7 @@ Caleb Deveraux
Catherine Devlin
Scott Dial
Alon Diamant
+Lital Natan
Toby Dickenson
Mark Dickinson
Jack Diederich
diff --git a/Misc/NEWS.d/next/Library/2022-02-17-13-10-50.bpo-39327.ytIT7Z.rst b/Misc/NEWS.d/next/Library/2022-02-17-13-10-50.bpo-39327.ytIT7Z.rst
new file mode 100644
index 0000000000000..fc6e8250922ff
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-02-17-13-10-50.bpo-39327.ytIT7Z.rst
@@ -0,0 +1,2 @@
+:func:`shutil.rmtree` can now work with VirtualBox shared folders when
+running from the guest operating-system.
More information about the Python-checkins
mailing list