Skip to content

Commit 54c2bdc

Browse files
committed
Fixed overlapping file lock exception
- Updated SynchronizedFileStream class - Updated CachedMapsFile#load() method
1 parent b0f6330 commit 54c2bdc

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/main/java/io/josemmo/bukkit/plugin/storage/CachedMapsFile.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ public int getDelay() {
114114
private void load() {
115115
// Try to load maps from disk
116116
if (exists() && getLastModified() > imageFile.getLastModified()) {
117+
LOGGER.fine("Found warm cache file \"" + path + "\"");
117118
try {
118119
loadFromDisk();
119120
return;
@@ -125,6 +126,7 @@ private void load() {
125126
}
126127

127128
// Generate maps from image file
129+
LOGGER.fine("Missed cache file \"" + path + "\"");
128130
try {
129131
generateFromImage();
130132
tryToWriteToDisk();

src/main/java/io/josemmo/bukkit/plugin/storage/SynchronizedFileStream.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import org.jetbrains.annotations.NotNull;
55
import java.io.IOException;
66
import java.io.RandomAccessFile;
7+
import java.nio.channels.FileChannel;
8+
import java.nio.channels.FileLock;
9+
import java.nio.channels.OverlappingFileLockException;
710
import java.nio.file.Path;
811

912
/**
@@ -17,10 +20,29 @@ public class SynchronizedFileStream extends RandomAccessFile {
1720
* @param readOnly Whether to open stream in read-only mode
1821
* @throws IOException if failed to acquire lock
1922
*/
20-
@Blocking
2123
public SynchronizedFileStream(@NotNull Path path, boolean readOnly) throws IOException {
2224
super(path.toFile(), readOnly ? "r" : "rw");
23-
//noinspection ResultOfMethodCallIgnored
24-
getChannel().lock(0L, Long.MAX_VALUE, readOnly);
25+
waitForLock(readOnly);
26+
}
27+
28+
/**
29+
* Wait for lock to be released
30+
* @param readOnly Whether to acquire read-only (shared) lock
31+
*/
32+
@Blocking
33+
private void waitForLock(boolean readOnly) throws IOException {
34+
FileChannel channel = getChannel();
35+
FileLock lock = null;
36+
while (lock == null) {
37+
try {
38+
lock = channel.lock(0L, Long.MAX_VALUE, readOnly);
39+
} catch (OverlappingFileLockException e) {
40+
try {
41+
Thread.sleep(20);
42+
} catch (InterruptedException __) {
43+
throw e;
44+
}
45+
}
46+
}
2547
}
2648
}

0 commit comments

Comments
 (0)