Skip to content

Commit e4816a3

Browse files
committed
fix: share LockFile lock map across classloaders to prevent OverlappingFileLockException (#3347)
1 parent 6a851b6 commit e4816a3

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

  • jib-core/src/main/java/com/google/cloud/tools/jib/filesystem

jib-core/src/main/java/com/google/cloud/tools/jib/filesystem/LockFile.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,19 @@
3131
/** Creates and deletes lock files. */
3232
public class LockFile implements Closeable {
3333

34-
private static final ConcurrentHashMap<Path, Lock> lockMap = new ConcurrentHashMap<>();
34+
// In Gradle composite builds with independent buildSrc directories, each included build loads
35+
// jib-core in its own classloader. A normal static field creates separate lockMap instances per
36+
// classloader, failing to prevent concurrent FileChannel.lock() calls on the same file and
37+
// causing OverlappingFileLockException (#3347). This is admittedly a hack, but there is no
38+
// clean way to share state across classloaders in the same JVM. System.getProperties() returns
39+
// the same Hashtable regardless of classloader, so it serves as a JVM-global namespace.
40+
// The map's key/value types (Path, Lock) are bootstrap-loaded, so they're safe to cast.
41+
@SuppressWarnings("unchecked")
42+
private static final ConcurrentHashMap<Path, Lock> lockMap =
43+
(ConcurrentHashMap<Path, Lock>)
44+
System.getProperties()
45+
.computeIfAbsent(
46+
"jib.lockFile.lockMap", key -> new ConcurrentHashMap<Path, Lock>());
3547

3648
private final Path lockFilePath;
3749
private final FileLock fileLock;

0 commit comments

Comments
 (0)