Skip to content
Permalink
Browse files
8252883: AccessDeniedException caused by delayed file deletion on Win…
…dows

Reviewed-by: dfuchs
  • Loading branch information
Evan Whelan authored and dfuch committed Feb 25, 2021
1 parent f79c626 commit ebdc80ead9b2ffd5f0d1e16ca6b8bab94b5ae6f3
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -36,6 +36,7 @@
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import java.nio.channels.OverlappingFileLockException;
import java.nio.file.AccessDeniedException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
@@ -511,6 +512,22 @@ private void openFiles() throws IOException {
channel = FileChannel.open(lockFilePath,
CREATE_NEW, WRITE);
fileCreated = true;
} catch (AccessDeniedException ade) {
// This can be either a temporary, or a more permanent issue.
// The lock file might be still pending deletion from a previous run
// (temporary), or the parent directory might not be accessible,
// not writable, etc..
// If we can write to the current directory, and this is a regular file,
// let's try again.
if (Files.isRegularFile(lockFilePath, LinkOption.NOFOLLOW_LINKS)
&& isParentWritable(lockFilePath)) {
// Try again. If it doesn't work, then this will
// eventually ensure that we increment "unique" and
// use another file name.
continue;
} else {
throw ade; // no need to retry
}
} catch (FileAlreadyExistsException ix) {
// This may be a zombie file left over by a previous
// execution. Reuse it - but only if we can actually
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.LogRecord;

/**
* @test
* @bug 8252883
* @summary tests the handling of AccessDeniedException due to delay in Windows deletion.
* @modules java.logging/java.util.logging:open
* @run main/othervm FileHandlerAccessTest 20
* @author evwhelan
*/

public class FileHandlerAccessTest {
public static void main(String[] args) {
var count = Integer.parseInt(args[0]);
System.out.println("Testing with " + count + " threads");
for (var i = 0; i < count; i++) {
new Thread(FileHandlerAccessTest::access).start();
}
}

private static void access() {
try {
var handler = new FileHandler("sample%g.log", 1048576, 2, true);
handler.publish(new LogRecord(Level.SEVERE, "TEST"));
handler.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

0 comments on commit ebdc80e

Please sign in to comment.