-
-
Notifications
You must be signed in to change notification settings - Fork 32k
os.unlink behaviour is error prone on windows #109608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Someone could contribute an improvement to The problem doesn't arise until you do something to reuse the filename. We shouldn't assume that someone is only deleting a file so that they can recreate it.
If the app itself has the file open, blocking until all handles are closed would deadlock the app. This would be as bad (or worse) as the "safely remove disk" command. |
@zooba I don't 100% agree about the "devastating" part for speed, but I do accept it would take longer, simply because right now it doesn't wait for the file to actually go away. As for waiting forever because the file was open, yes, I can see that being a change in behaviour, however, if the file was open by anything other than a scanner opening the file with
raises (On linux etc this wouldn't error) |
Perhaps it might be better for shutil.rmtree to rename the folder before deletion |
I've hit this issue with unlink on Windows before. Can we at least update the docs to note why Windows may seem different? (Add a note that unlink schedules the delete on Windows but doesn't wait for it to be done). |
@csm10495 That sounds a good idea. Also, updating shutil to DWIM seems less disruptive than altering unlink |
Found another earlier issue https://bugs.python.org/issue22024 I'm currently working on a patch to shutil that hopefully covers these cases in a friendly way |
Uh oh!
There was an error while loading. Please reload this page.
Bug report
Bug description:
There are a great many bugs, mostly relating to use of
shutil.rmtree()
,os.unlink()
oros.remove()
on windows where people find they have to retry errors when deleting or renaming files, or when creating fresh files with the same name of files they have deleted using these calls.To pick one at random #51692 outlines the classic issue and also notes the cause.
On windows
DeleteFileW()
will return success when a file has been "schedulled" for deletion. Since almost everyone who writes code that callsos.unlink()
expects the file to be gone once that function returns this is the cause of many windows-specific errors in code that otherwise works fine on all other platforms.The Windows API docs for
DeleteFile()
at https://learn.microsoft.com/en-gb/windows/win32/api/fileapi/nf-fileapi-deletefilew?redirectedfrom=MSDN state:Many comments out there wrongly attribute the resulting errors to antivirus scanners delaying the removal of the file, but to me it seems like cpython should, on windows handle this, it is simply a longer delay on windows if antivirus is busy with a file we wanted to remove.
It therefore seems reasonable to consider making
os.unlink()
/os.remove()
on windows block until the file has actually been removed ifDeleteFileW()
has not returned an error.Has this been considered before? In my admittedly quick search of the current and past issues I've not seen this proposed.
Many thanks.
Ian
CPython versions tested on:
3.8, 3.9, 3.10, 3.11, 3.12, CPython main branch
Operating systems tested on:
Windows
The text was updated successfully, but these errors were encountered: