Skip to content

Commit ebdc80e

Browse files
Evan Whelandfuch
authored andcommitted
8252883: AccessDeniedException caused by delayed file deletion on Windows
Reviewed-by: dfuchs
1 parent f79c626 commit ebdc80e

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

src/java.logging/share/classes/java/util/logging/FileHandler.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -36,6 +36,7 @@
3636
import java.io.OutputStream;
3737
import java.nio.channels.FileChannel;
3838
import java.nio.channels.OverlappingFileLockException;
39+
import java.nio.file.AccessDeniedException;
3940
import java.nio.file.FileAlreadyExistsException;
4041
import java.nio.file.Files;
4142
import java.nio.file.LinkOption;
@@ -511,6 +512,22 @@ private void openFiles() throws IOException {
511512
channel = FileChannel.open(lockFilePath,
512513
CREATE_NEW, WRITE);
513514
fileCreated = true;
515+
} catch (AccessDeniedException ade) {
516+
// This can be either a temporary, or a more permanent issue.
517+
// The lock file might be still pending deletion from a previous run
518+
// (temporary), or the parent directory might not be accessible,
519+
// not writable, etc..
520+
// If we can write to the current directory, and this is a regular file,
521+
// let's try again.
522+
if (Files.isRegularFile(lockFilePath, LinkOption.NOFOLLOW_LINKS)
523+
&& isParentWritable(lockFilePath)) {
524+
// Try again. If it doesn't work, then this will
525+
// eventually ensure that we increment "unique" and
526+
// use another file name.
527+
continue;
528+
} else {
529+
throw ade; // no need to retry
530+
}
514531
} catch (FileAlreadyExistsException ix) {
515532
// This may be a zombie file left over by a previous
516533
// execution. Reuse it - but only if we can actually
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import java.util.logging.FileHandler;
25+
import java.util.logging.Level;
26+
import java.util.logging.LogRecord;
27+
28+
/**
29+
* @test
30+
* @bug 8252883
31+
* @summary tests the handling of AccessDeniedException due to delay in Windows deletion.
32+
* @modules java.logging/java.util.logging:open
33+
* @run main/othervm FileHandlerAccessTest 20
34+
* @author evwhelan
35+
*/
36+
37+
public class FileHandlerAccessTest {
38+
public static void main(String[] args) {
39+
var count = Integer.parseInt(args[0]);
40+
System.out.println("Testing with " + count + " threads");
41+
for (var i = 0; i < count; i++) {
42+
new Thread(FileHandlerAccessTest::access).start();
43+
}
44+
}
45+
46+
private static void access() {
47+
try {
48+
var handler = new FileHandler("sample%g.log", 1048576, 2, true);
49+
handler.publish(new LogRecord(Level.SEVERE, "TEST"));
50+
handler.close();
51+
} catch (Exception e) {
52+
throw new RuntimeException(e);
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)