Use rename to bypass windows lockfile issue

This commit is contained in:
dotasek 2024-09-19 12:22:29 -04:00
parent cce4d108ce
commit c81c631ae8
3 changed files with 16 additions and 9 deletions

View File

@ -261,11 +261,15 @@ public class FilesystemPackageCacheManagerLocks {
try { try {
result = function.get(); result = function.get();
} finally { } finally {
lockFile.renameTo(File.createTempFile(lockFile.getName(), ".lock-renamed"));
fileLock.release();
channel.close();
if (!lockFile.delete()) { if (!lockFile.delete()) {
lockFile.deleteOnExit(); lockFile.deleteOnExit();
} }
fileLock.release();
channel.close();
lock.writeLock().unlock(); lock.writeLock().unlock();
cacheLock.getLock().writeLock().unlock(); cacheLock.getLock().writeLock().unlock();

View File

@ -93,24 +93,27 @@ public class LockfileTestProcessUtility {
*/ */
private static void lockWaitAndDelete(String path, String lockFileName, int seconds) throws InterruptedException, IOException { private static void lockWaitAndDelete(String path, String lockFileName, int seconds) throws InterruptedException, IOException {
File file = Paths.get(path,lockFileName).toFile(); File lockFile = Paths.get(path,lockFileName).toFile();
try (FileChannel channel = new RandomAccessFile(file.getAbsolutePath(), "rw").getChannel()) { try (FileChannel channel = new RandomAccessFile(lockFile.getAbsolutePath(), "rw").getChannel()) {
FileLock fileLock = channel.tryLock(0, Long.MAX_VALUE, false); FileLock fileLock = channel.tryLock(0, Long.MAX_VALUE, false);
if (fileLock != null) { if (fileLock != null) {
final ByteBuffer buff = ByteBuffer.wrap("Hello world".getBytes(StandardCharsets.UTF_8)); final ByteBuffer buff = ByteBuffer.wrap("Hello world".getBytes(StandardCharsets.UTF_8));
channel.write(buff); channel.write(buff);
System.out.println("File "+lockFileName+" is locked. Waiting for " + seconds + " seconds to release. "); System.out.println("File "+lockFileName+" is locked. Waiting for " + seconds + " seconds to release. ");
Thread.sleep(seconds * 1000L); Thread.sleep(seconds * 1000L);
file.delete();
lockFile.renameTo(File.createTempFile(lockFile.getName(), ".lock-renamed"));
fileLock.release(); fileLock.release();
channel.close();
System.out.println(System.currentTimeMillis()); System.out.println(System.currentTimeMillis());
System.out.println("File "+lockFileName+" is released."); System.out.println("File "+lockFileName+" is released.");
channel.close(); lockFile.delete();
}}finally { }}finally {
if (file.exists()) { if (lockFile.exists()) {
file.delete(); lockFile.delete();
} }
} }
} }

View File

@ -70,7 +70,7 @@ public class LockfileTestUtility {
// the file system immediately. It is unlikely to appear in production environments, but should it occur, it will // the file system immediately. It is unlikely to appear in production environments, but should it occur, it will
// result in a lock file being erroneously reported as not having an owning process, and will cause a package to // result in a lock file being erroneously reported as not having an owning process, and will cause a package to
// fail to be loaded from that cache until the lock is cleaned up by cache initialization. // fail to be loaded from that cache until the lock is cleaned up by cache initialization.
Thread.sleep(100); //Thread.sleep(100);
} }