From 18855e17e796fc18e2f5c8b100cb6ad807c56ac6 Mon Sep 17 00:00:00 2001 From: Lital Natan Date: Thu, 17 Feb 2022 12:41:18 +0200 Subject: [PATCH 1/2] Fixed issue 39327: Avoid "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) it will no longer give the error above and the rmtree succeeds --- Lib/shutil.py | 12 ++++++++++-- Misc/ACKS | 1 + .../Library/2022-02-17-13-10-50.bpo-39327.ytIT7Z.rst | 2 ++ 3 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2022-02-17-13-10-50.bpo-39327.ytIT7Z.rst diff --git a/Lib/shutil.py b/Lib/shutil.py index 949e024853c1d2..eb768f9e03b7d7 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 dceb2b628eb20c..bab04b4613646a 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 00000000000000..bb3bbc8ea2fc95 --- /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 From 16d84cfce5c1c72cfa9a8626899727759fee5388 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 17 Feb 2022 14:28:20 +0200 Subject: [PATCH 2/2] Update Misc/NEWS.d/next/Library/2022-02-17-13-10-50.bpo-39327.ytIT7Z.rst --- .../next/Library/2022-02-17-13-10-50.bpo-39327.ytIT7Z.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index bb3bbc8ea2fc95..fc6e8250922ffe 100644 --- 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 @@ -1,2 +1,2 @@ :func:`shutil.rmtree` can now work with VirtualBox shared folders when -running from the guest operating-system +running from the guest operating-system.