From c81c631ae8c5c479e366b365326714bc85a95333 Mon Sep 17 00:00:00 2001 From: dotasek Date: Thu, 19 Sep 2024 12:22:29 -0400 Subject: [PATCH] Use rename to bypass windows lockfile issue --- .../npm/FilesystemPackageCacheManagerLocks.java | 8 ++++++-- .../utilities/npm/LockfileTestProcessUtility.java | 15 +++++++++------ .../fhir/utilities/npm/LockfileTestUtility.java | 2 +- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/FilesystemPackageCacheManagerLocks.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/FilesystemPackageCacheManagerLocks.java index ad9d54380..def6e8cb7 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/FilesystemPackageCacheManagerLocks.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/FilesystemPackageCacheManagerLocks.java @@ -261,11 +261,15 @@ public class FilesystemPackageCacheManagerLocks { try { result = function.get(); } finally { + + lockFile.renameTo(File.createTempFile(lockFile.getName(), ".lock-renamed")); + + fileLock.release(); + channel.close(); + if (!lockFile.delete()) { lockFile.deleteOnExit(); } - fileLock.release(); - channel.close(); lock.writeLock().unlock(); cacheLock.getLock().writeLock().unlock(); diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/npm/LockfileTestProcessUtility.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/npm/LockfileTestProcessUtility.java index 76792c5fb..63e10bfd9 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/npm/LockfileTestProcessUtility.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/npm/LockfileTestProcessUtility.java @@ -93,24 +93,27 @@ public class LockfileTestProcessUtility { */ 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); if (fileLock != null) { final ByteBuffer buff = ByteBuffer.wrap("Hello world".getBytes(StandardCharsets.UTF_8)); channel.write(buff); System.out.println("File "+lockFileName+" is locked. Waiting for " + seconds + " seconds to release. "); Thread.sleep(seconds * 1000L); - file.delete(); + + lockFile.renameTo(File.createTempFile(lockFile.getName(), ".lock-renamed")); + fileLock.release(); + channel.close(); System.out.println(System.currentTimeMillis()); System.out.println("File "+lockFileName+" is released."); - channel.close(); + lockFile.delete(); }}finally { - if (file.exists()) { - file.delete(); + if (lockFile.exists()) { + lockFile.delete(); } } } diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/npm/LockfileTestUtility.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/npm/LockfileTestUtility.java index e7badf2f0..5309d5e19 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/npm/LockfileTestUtility.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/npm/LockfileTestUtility.java @@ -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 // 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. - Thread.sleep(100); + //Thread.sleep(100); }