This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eryksun
Recipients Gabriele Tornetta, christian.heimes, eryksun
Date 2021-01-01.13:52:53
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1609509173.66.0.453854599803.issue42796@roundup.psfhosted.org>
In-reply-to
Content
> Windows doesn't let you remove files and directories that are used 
> by a process. 

Windows does allow deleting open files/directories, but read/execute, write/append, and delete/rename access have to be explicitly shared when opening a file or directory. WinAPI SetCurrentDirectoryW (called by os.chdir) does not share delete access when opening a directory as the new working directory, and the handle is kept open to use as the NTAPI RootDirectory handle when opening relative paths. So the directory can only be deleted after either the working directory changes or the process exits.

See bpo-35144 for the source of the recursion error. An onerror handler was added that retries rmtree() after attempting to reset permissions. I proposed a workaround in the linked issue, but I don't think it's enough. Maybe there should be a separate onerror function for Windows.

The two common PermissionError cases to handle in Windows are readonly files and sharing violations. If the readonly attribute is set, we can remove it and retry the unlink() or rmdir() call. I don't think there's no need for a recursive call since rmtree() only removes empty directories. 

For a sharing violation (winerror 32), all that can be done is to set an atexit function that retries deleting the directory. If rmtree() still fails, emit a resource warning and give up.
History
Date User Action Args
2021-01-01 13:52:53eryksunsetrecipients: + eryksun, christian.heimes, Gabriele Tornetta
2021-01-01 13:52:53eryksunsetmessageid: <1609509173.66.0.453854599803.issue42796@roundup.psfhosted.org>
2021-01-01 13:52:53eryksunlinkissue42796 messages
2021-01-01 13:52:53eryksuncreate