OSDN Git Service

Fix concurrent modification exception in KeepaliveTracker
authorjunyulai <junyulai@google.com>
Mon, 13 May 2019 06:19:00 +0000 (14:19 +0800)
committerjunyulai <junyulai@google.com>
Mon, 13 May 2019 10:44:39 +0000 (18:44 +0800)
In aosp/951200, the clean up function delete the item in the
hash map that holds the record while iterating it, where the
list used to iterate the records is backed by the hash map,
so changes to the map are reflected in the list and caused
the concurrent modification exception.

Bug: 132341736
Test: 1. atest com.android.server.ConnectivityServiceTest \
         #testNattSocketKeepalives --generate-new-metrics 300
      2. atest FrameworksNetTests --generate-new-metrics 10

(Clean cherry-pick of aosp/959599)

Change-Id: I9cdfe6f6d11c5400c856cc30a33ff4a44ba9d811
Merged-In: I0481a469ee23231e5f0ab738a06b5e09f6cdb680

services/core/java/com/android/server/connectivity/KeepaliveTracker.java

index 7fb97f2..0edab4f 100644 (file)
@@ -458,9 +458,10 @@ public class KeepaliveTracker {
     }
 
     public void handleStopAllKeepalives(NetworkAgentInfo nai, int reason) {
-        HashMap <Integer, KeepaliveInfo> networkKeepalives = mKeepalives.get(nai);
+        final HashMap<Integer, KeepaliveInfo> networkKeepalives = mKeepalives.get(nai);
         if (networkKeepalives != null) {
-            for (KeepaliveInfo ki : networkKeepalives.values()) {
+            final ArrayList<KeepaliveInfo> kalist = new ArrayList(networkKeepalives.values());
+            for (KeepaliveInfo ki : kalist) {
                 ki.stop(reason);
                 // Clean up keepalives since the network agent is disconnected and unable to pass
                 // back asynchronous result of stop().