Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a9f7e3ea authored by Dmitri Plotnikov's avatar Dmitri Plotnikov
Browse files

Close channel that underlies a file lock

Bug: 385590072
Flag: EXEMPT bugfix
Test: atest PowerStatsTests
Change-Id: Ib9e99c35c1a624ec96cdfcda1dd7751236c5b894
parent 0fd6b40d
Loading
Loading
Loading
Loading
+9 −3
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.Channel;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.charset.StandardCharsets;
@@ -242,8 +243,10 @@ public class PowerStatsStore {

        // Lock the directory from access by other JVMs
        try {
            if (!mLockFile.exists()) {
                mLockFile.getParentFile().mkdirs();
                mLockFile.createNewFile();
            }
            mJvmLock = FileChannel.open(mLockFile.toPath(), StandardOpenOption.WRITE).lock();
        } catch (IOException e) {
            Slog.e(TAG, "Cannot lock snapshot directory", e);
@@ -252,10 +255,13 @@ public class PowerStatsStore {

    private void unlockStoreDirectory() {
        try {
            mJvmLock.close();
            Channel channel = mJvmLock.acquiredBy();
            mJvmLock.release();
            channel.close();
        } catch (IOException e) {
            Slog.e(TAG, "Cannot unlock snapshot directory", e);
        } finally {
            mJvmLock = null;
            mFileLock.unlock();
        }
    }