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

    https://github.com/apache/flink/pull/3219#discussion_r99576850
  
    --- 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 only happens when multiple threads are involved; running the test with 1 
thread works like charm.
    
    This whole thing is just strange. I've made some debugging and what 
happened is that multiple threads delete the same file, and verify the deletion 
using `Files.exists(filename)`. All of them pass. A few seconds later we hit 
the `DirectoryNotEmptyException`, check the contents again and what do you 
know, the file *which we just verified to be deleted*  still exists.


---
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