aboutsummaryrefslogtreecommitdiffstats
path: root/android
diff options
context:
space:
mode:
authorNeil Fuller <nfuller@google.com>2014-11-19 18:50:41 +0000
committerNeil Fuller <nfuller@google.com>2014-11-19 19:53:14 +0000
commitff345b6c0ffcc691e4c3c594f8a222cf81bb325c (patch)
tree5727a68a6af573b09427597acdef9aacf57bd21d /android
parent84ae29f837f73df820510dbd0ea57f38485c93c7 (diff)
downloadplatform_external_okhttp-ff345b6c0ffcc691e4c3c594f8a222cf81bb325c.tar.gz
platform_external_okhttp-ff345b6c0ffcc691e4c3c594f8a222cf81bb325c.tar.bz2
platform_external_okhttp-ff345b6c0ffcc691e4c3c594f8a222cf81bb325c.zip
Fix for a socket leak in OkHttp on Android
When the preferred Android network changes from cell -> wifi or wifi -> cell the HTTP connection pool in use is abandoned to avoid reuse of connections on the old network. This was added in commit 8bced3e. The design for the connection pool was such that continuous use of the connection pool was required to clean up idle / expired connections. If a connection pool becomes idle (as when it is dereferenced on a network change) it is possible for some connections to remain in the pool indefinitely. After the preferred network change, because the old connection pool was no longer referenced the pool would be garbage collected and Android's "Strict Mode" would complain about sockets not being closed. The only existing way to avoid this was to call "evictAll()", which would have had issues when a large number of connections were returned to the pool after evictAll() was called. It also wouldn't work for SPDY connections which are shared but not reference counted, which makes knowing whether it is safe to close them difficult. SPDY is not enabled on Android by default and so that may have been safe to ignore. This fix tries to keep the existing cleaning behavior intact to avoid introducing new bugs or new thread behavior that might impact battery life. It adds a new mode to the pool for "draining", which handles cleaning up any existing entries in the pool and any added after the pool has been placed into draining mode. The drainModeRunnable introduced serves two purposes: 1) While scheduled / executing, it pins the connection pool in memory to avoid it being garbage collected. 2) It continues to close connections (safely) until the pool is empty. If a connection is then added back to the pool the drainModeRunnable is restarted. Bug: 18369687 Change-Id: I7e9bdd0edba84784f667105f52ef80655c1b06ff
Diffstat (limited to 'android')
-rw-r--r--android/main/java/com/squareup/okhttp/ConfigAwareConnectionPool.java4
1 files changed, 4 insertions, 0 deletions
diff --git a/android/main/java/com/squareup/okhttp/ConfigAwareConnectionPool.java b/android/main/java/com/squareup/okhttp/ConfigAwareConnectionPool.java
index 36c3101..e64eec4 100644
--- a/android/main/java/com/squareup/okhttp/ConfigAwareConnectionPool.java
+++ b/android/main/java/com/squareup/okhttp/ConfigAwareConnectionPool.java
@@ -86,7 +86,11 @@ public class ConfigAwareConnectionPool {
// If the network config has changed then existing pooled connections should not be
// re-used. By setting connectionPool to null it ensures that the next time
// getConnectionPool() is called a new pool will be created.
+ ConnectionPool oldConnectionPool = connectionPool;
connectionPool = null;
+ if (oldConnectionPool != null) {
+ oldConnectionPool.enterDrainMode();
+ }
}
}
});