-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
rmtree
sometimes fails due to "directory not empty" on Linux
#128076
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
Hello! I have the same problem. Raises an error about an non-empty folder, but really the folder is empty. |
I am also having the same problem in Python 3.12. |
I think there are some flaky issues due to the OS itself (@barneygale maybe you have some idea?) I can suggest using |
Thanks for the suggestion. What I currently have is: try:
shutil.rmtree(self.build_dir)
except OSError as e:
if e.strerror == "Directory not empty":
# Not sure why this happens, but trying again seems to fix it usually?
shutil.rmtree(self.build_dir)
else:
raise which seems to reduce the frequency of the problem but not eliminate it entirely. Maybe I'll add a sleep for a few milliseconds before trying again, and do three tries. I bet that will be good enough, but I'll have to see. |
So maybe: def retrying_rmtree(d):
for _ in range(3):
try:
return shutil.rmtree(d)
except OSError as e:
if e.strerror == "Directory not empty":
# wait a bit and try again up to 3 tries
time.sleep(0.01)
else:
raise
raise RuntimeError(f"shutil.rmtree('{d}') failed with ENOTEMPTY three times") |
It's indeed a possibility to have a sleep/retry but I eventually think there's no real issue with @barneygale WDYT? closing this one as |
rmtree
fails due to "directory not empty" on Linuxrmtree
sometimes fails due to "directory not empty" on Linux
Considering the workaround with |
Uh oh!
There was an error while loading. Please reload this page.
Bug report
Bug description:
I have a
temp
directory in which I have one zip file per day of a month (along the pattern./agrisens_YYYYMMDD.zip
) and the extracted folders (many files along the pattern./agrisens_YYYYMMDD/foo.bar
). The zip files are listed in adownloaded_files
variable. My code to clear thetemp
directory (without deleting it entirely) is like this:Repeatedly, this loop failed half-way through:
Interestingly, when looking at the file system, the folder is actually empty!
So it's quite likely that some race condition happened so that the folder was not empty on the attempt to delete it, but shortly after.
Note also that the folder of the day before the criticised folder didn't get removed either. And neither the zip file of the day in question, despite the call to
os.remove
being before the call toshutil.rmtree
in the loop.The same issue also happened with the previous month:
In both cases, the fail happened quite exactly in the middle of the month -- might be coincidental, might be noteworthy.
Has this been discussed elsewhere?
I looked through open issues with "rmtree" and saw two that might be related:
Other notes
The file system being written to is a network storage that is mounted into a virtual machine, from where it is mounted into the Docker container where my actual code runs, so not the closest setup -> a good environment for race conditions to happen. To my knowledge, no Windows is involved anywhere in the chain.
Version
CPython versions tested on:
3.10
Operating systems tested on:
Linux
The text was updated successfully, but these errors were encountered: