mirror of
https://github.com/xpipe-io/xpipe.git
synced 2024-09-28 23:31:43 +12:00
Handle data lock race condition
This commit is contained in:
parent
8e1665ba27
commit
7d03e22af3
1 changed files with 8 additions and 1 deletions
|
@ -5,6 +5,7 @@ import io.xpipe.app.issue.ErrorEvent;
|
|||
import java.io.RandomAccessFile;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.channels.FileLock;
|
||||
import java.nio.file.FileAlreadyExistsException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
|
@ -22,7 +23,13 @@ public class AppDataLock {
|
|||
var file = getLockFile().toFile();
|
||||
Files.createDirectories(file.toPath().getParent());
|
||||
if (!Files.exists(file.toPath())) {
|
||||
try {
|
||||
// It is possible that another instance creates the lock at almost the same time
|
||||
// If it already exists, we lost the race
|
||||
Files.createFile(file.toPath());
|
||||
} catch (FileAlreadyExistsException f) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
channel = new RandomAccessFile(file, "rw").getChannel();
|
||||
lock = channel.tryLock();
|
||||
|
|
Loading…
Reference in a new issue