Cleanup fileChannel if file locking fails

This commit is contained in:
AsamK 2022-05-18 15:27:02 +02:00
parent 53f47d42fc
commit 7ac6c9a170

View file

@ -884,6 +884,7 @@ public class SignalAccount implements Closeable {
private static Pair<FileChannel, FileLock> openFileChannel(File fileName, boolean waitForLock) throws IOException { private static Pair<FileChannel, FileLock> openFileChannel(File fileName, boolean waitForLock) throws IOException {
var fileChannel = new RandomAccessFile(fileName, "rw").getChannel(); var fileChannel = new RandomAccessFile(fileName, "rw").getChannel();
try {
var lock = fileChannel.tryLock(); var lock = fileChannel.tryLock();
if (lock == null) { if (lock == null) {
if (!waitForLock) { if (!waitForLock) {
@ -894,7 +895,14 @@ public class SignalAccount implements Closeable {
lock = fileChannel.lock(); lock = fileChannel.lock();
logger.info("Config file lock acquired."); logger.info("Config file lock acquired.");
} }
return new Pair<>(fileChannel, lock); final var result = new Pair<>(fileChannel, lock);
fileChannel = null;
return result;
} finally {
if (fileChannel != null) {
fileChannel.close();
}
}
} }
public void addPreKeys(ServiceIdType serviceIdType, List<PreKeyRecord> records) { public void addPreKeys(ServiceIdType serviceIdType, List<PreKeyRecord> records) {