diff --git a/staticlibs/device/com/android/net/module/util/SingleWriterBpfMap.java b/staticlibs/device/com/android/net/module/util/SingleWriterBpfMap.java index cd6bfec294..a638cc4151 100644 --- a/staticlibs/device/com/android/net/module/util/SingleWriterBpfMap.java +++ b/staticlibs/device/com/android/net/module/util/SingleWriterBpfMap.java @@ -19,6 +19,7 @@ import android.os.Build; import android.system.ErrnoException; import android.util.Pair; +import androidx.annotation.GuardedBy; import androidx.annotation.NonNull; import androidx.annotation.RequiresApi; @@ -60,6 +61,7 @@ import java.util.NoSuchElementException; public class SingleWriterBpfMap extends BpfMap { // HashMap instead of ArrayMap because it performs better on larger maps, and many maps used in // our code can contain hundreds of items. + @GuardedBy("this") private final HashMap mCache = new HashMap<>(); // This should only ever be called (hence private) once for a given 'path'. @@ -72,10 +74,12 @@ public class SingleWriterBpfMap extends BpfM super(path, BPF_F_RDWR_EXCLUSIVE, key, value); // Populate cache with the current map contents. - K currentKey = super.getFirstKey(); - while (currentKey != null) { - mCache.put(currentKey, super.getValue(currentKey)); - currentKey = super.getNextKey(currentKey); + synchronized (this) { + K currentKey = super.getFirstKey(); + while (currentKey != null) { + mCache.put(currentKey, super.getValue(currentKey)); + currentKey = super.getNextKey(currentKey); + } } }