# HG changeset patch # User ewhelan # Date 1614254716 0 # Node ID 0056610eefad4cffedcc1443efdff8145d2a0135 # Parent 172c73eb036dc2583f4fcafa7e6a5b2ca8698bd5 8252883: AccessDeniedException caused by delayed file deletion on Windows Reviewed-by: dfuchs diff -r 172c73eb036d -r 0056610eefad src/share/classes/java/util/logging/FileHandler.java --- a/src/share/classes/java/util/logging/FileHandler.java Wed May 12 16:21:59 2021 +0100 +++ b/src/share/classes/java/util/logging/FileHandler.java Thu Feb 25 12:05:16 2021 +0000 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000, 2017, 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; @@ -478,6 +479,22 @@ 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 diff -r 172c73eb036d -r 0056610eefad test/java/util/logging/FileHandlerAccessTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/java/util/logging/FileHandlerAccessTest.java Thu Feb 25 12:05:16 2021 +0000 @@ -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) { + int count = Integer.parseInt(args[0]); + System.out.println("Testing with " + count + " threads"); + for (int i = 0; i < count; i++) { + new Thread(FileHandlerAccessTest::access).start(); + } + } + + private static void access() { + try { + FileHandler 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); + } + } +}