Merge "ConnectivityManager: no double callback remove" into nyc-mr1-dev
diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java
index fb5b3f8..995a910 100644
--- a/services/core/java/com/android/server/ConnectivityService.java
+++ b/services/core/java/com/android/server/ConnectivityService.java
@@ -2445,33 +2445,37 @@
             }
             mNetworkRequestInfoLogs.log("RELEASE " + nri);
             if (nri.request.isRequest()) {
+                boolean wasKept = false;
+                NetworkAgentInfo nai = mNetworkForRequestId.get(nri.request.requestId);
+                if (nai != null) {
+                    nai.removeRequest(nri.request.requestId);
+                    if (VDBG) {
+                        log(" Removing from current network " + nai.name() +
+                                ", leaving " + nai.numNetworkRequests() + " requests.");
+                    }
+                    if (unneeded(nai)) {
+                        if (DBG) log("no live requests for " + nai.name() + "; disconnecting");
+                        teardownUnneededNetwork(nai);
+                    } else {
+                        wasKept = true;
+                    }
+                    mNetworkForRequestId.remove(nri.request.requestId);
+                }
+
+                // TODO: remove this code once we know that the Slog.wtf is never hit.
+                //
                 // Find all networks that are satisfying this request and remove the request
                 // from their request lists.
                 // TODO - it's my understanding that for a request there is only a single
                 // network satisfying it, so this loop is wasteful
-                boolean wasKept = false;
-                for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) {
-                    if (nai.isSatisfyingRequest(nri.request.requestId)) {
-                        nai.removeRequest(nri.request.requestId);
-                        if (VDBG) {
-                            log(" Removing from current network " + nai.name() +
-                                    ", leaving " + nai.numNetworkRequests() + " requests.");
-                        }
-                        if (unneeded(nai)) {
-                            if (DBG) log("no live requests for " + nai.name() + "; disconnecting");
-                            teardownUnneededNetwork(nai);
-                        } else {
-                            // suspect there should only be one pass through here
-                            // but if any were kept do the check below
-                            wasKept |= true;
-                        }
+                for (NetworkAgentInfo otherNai : mNetworkAgentInfos.values()) {
+                    if (otherNai.isSatisfyingRequest(nri.request.requestId) && otherNai != nai) {
+                        Slog.wtf(TAG, "Request " + nri.request + " satisfied by " +
+                                otherNai.name() + ", but mNetworkAgentInfos says " +
+                                (nai != null ? nai.name() : "null"));
                     }
                 }
 
-                NetworkAgentInfo nai = mNetworkForRequestId.get(nri.request.requestId);
-                if (nai != null) {
-                    mNetworkForRequestId.remove(nri.request.requestId);
-                }
                 // Maintain the illusion.  When this request arrived, we might have pretended
                 // that a network connected to serve it, even though the network was already
                 // connected.  Now that this request has gone away, we might have to pretend