From 03e5dc61174e057723d20437b60869527af8f28b Mon Sep 17 00:00:00 2001 From: "Denis.Zharkov" Date: Mon, 7 Jun 2021 13:51:05 +0300 Subject: [PATCH] Force unmapping MappedByteBuffer Otherwise, on Windows daemon might hold mapped regions for some time (until those objects are collection) and during the time those file become locked (it's impossible to modify or remove them) Reflection/Unsafe of course is not a cool thing to use, but JDK still (already for 18 years) doesn't have public API for this See https://bugs.java.com/bugdatabase/view_bug.do?bug_id=4724038 And https://stackoverflow.com/questions/2972986/how-to-unmap-a-file-from-memory-mapped-using-filechannel-in-java --- .../cli/jvm/compiler/jarfs/FastJarHandler.kt | 48 ++++++++++++++++++- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/jarfs/FastJarHandler.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/jarfs/FastJarHandler.kt index 766754defbc..df183eda624 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/jarfs/FastJarHandler.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/jarfs/FastJarHandler.kt @@ -14,6 +14,7 @@ import java.io.File import java.io.FileNotFoundException import java.io.IOException import java.io.RandomAccessFile +import java.nio.ByteBuffer import java.nio.MappedByteBuffer import java.nio.channels.FileChannel @@ -26,8 +27,12 @@ class FastJarHandler(val fileSystem: FastJarFileSystem, path: String) : ZipHandl init { RandomAccessFile(file, "r").use { randomAccessFile -> val mappedByteBuffer = randomAccessFile.channel.map(FileChannel.MapMode.READ_ONLY, 0, randomAccessFile.length()) - ourEntryMap = mappedByteBuffer.parseCentralDirectory().associateBy { it.relativePath } - cachedManifest = ourEntryMap[MANIFEST_PATH]?.let(mappedByteBuffer::contentsToByteArray) + try { + ourEntryMap = mappedByteBuffer.parseCentralDirectory().associateBy { it.relativePath } + cachedManifest = ourEntryMap[MANIFEST_PATH]?.let(mappedByteBuffer::contentsToByteArray) + } finally { + mappedByteBuffer.unmapBuffer() + } } val entries: MutableMap = HashMap() @@ -167,9 +172,48 @@ private val cachedOpenFileHandles: FileAccessorCache