Github user zentol commented on a diff in the pull request:

    https://github.com/apache/flink/pull/3219#discussion_r98486443
  
    --- Diff: flink-core/src/main/java/org/apache/flink/util/FileUtils.java ---
    @@ -148,14 +158,49 @@ public static void deleteDirectory(File directory) 
throws IOException {
                                return;
                        }
     
    -                   // delete the directory. this fails if the directory is 
not empty, meaning
    -                   // if new files got concurrently created. we want to 
fail then.
    -                   try {
    -                           Files.delete(directory.toPath());
    -                   }
    -                   catch (NoSuchFileException ignored) {
    -                           // if someone else deleted this concurrently, 
we don't mind
    -                           // the result is the same for us, after all
    +                   java.nio.file.Path directoryPath = directory.toPath();
    +                   if (OperatingSystem.isWindows()) {
    +                           // delete the directory. this fails if the 
directory is not empty, meaning
    +                           // if new files got concurrently created. we 
want to fail then.
    +                           try {
    +                                   Files.delete(directoryPath);
    +                           } catch (NoSuchFileException ignored) {
    +                                   // if someone else deleted this 
concurrently, we don't mind
    +                                   // the result is the same for us, after 
all
    +                           } catch (AccessDeniedException e) {
    +                                   // This may occur on Windows if another 
process is currently
    +                                   // deleting the file, since the file 
must be opened in order
    +                                   // to delete it. We double check here 
to make sure the file
    +                                   // was actually deleted by another 
process. Note that this
    +                                   // isn't a perfect solution, but it's 
better than nothing.
    +                                   if (Files.exists(directoryPath)) {
    +                                           throw e;
    +                                   }
    +                           } catch (DirectoryNotEmptyException e) {
    +                                   // This may occur on Windows for some 
reason even for empty
    +                                   // directories. Apparently there's a 
timing/visibility
    +                                   // issue when concurrently deleting the 
contents of a directory 
    +                                   // and afterwards deleting the 
directory itself.
    +                                   try {
    +                                           Thread.sleep(50);
    --- End diff --
    
    It is intended that the thread hitting this exception never deletes the 
directory. The assumption is that another thread has a correct view over the 
directory and will actually delete it if possible.
    
    We however must still check whether the directory is empty after *some* 
time so that we actually fail if a new file was created concurrently, to 
preserve existing behavior.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to