Ignore errors when deleting files and folders on Windows #3289
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This, in combination #3288, removes all known Windows test errors due to failed deletes (note: this PR has no overlap with #3288 and the closed #3239 ).
The change in this PR affects primarily Windows - by catching
OSError
when deletion fails.In couple of cases, it also affects Linux in the sense that it checks whether a folder is present before attempting to
shutil.rmtree
it, and catches errors when attempting to cleanup aTemporaryDirectory
. However, in the specific contexts where it happens, this change is harmless.One potential issue is not solved in this PR, but it is not present in our codebase at the moment, and we should avoid introducing it anyway, so there's nothing to worry about. Still, just for reference in case this comes up:
TemporaryDirectory
, in its finalizer, deletes the temporary folder without catchingOSError
(the finalizer is called from gc and fromatexit
). Ifcleanup()
is called explicitly, the finalizer is detached, and that's what we do (and should continue to do) in our codebase (of course, I putcleanup()
call inside try/except). However, if aTemporaryDirectory
is accidentally allowed to go out of scope or survive till interpreter exit without acleanup()
call, the finalizer will kick in, and on Windows might raise an exception (if called fromatexit
) or print stuff tostderr
(if called from gc). If we did want to fix this, we could simply wrapTemporaryDirectory
in our own class that has its own finalizer that callscleanup()
of the wrappedTemporaryDirectory
instance. The finalizer semantics would ensure that our own finalizer is called first (since gc can't touch the wrapped object while there are references to it, andatexit
will call our finalizer first as it was created later). As I said though, this is unnecessary since we always explicitly callcleanup()
.