summaryrefslogtreecommitdiffstats
path: root/core/java
diff options
context:
space:
mode:
authorNick Kralevich <nnk@google.com>2010-12-13 14:32:17 -0800
committerNick Kralevich <nnk@google.com>2010-12-13 14:32:17 -0800
commit2ed20f87684a892d640a91f1961f369bbbc7d05e (patch)
tree52ece9413a6feb7be05d9d5cb31b48860a62e92f /core/java
parente3f6336bcffc250da90ec864bccfa73ad1d016b9 (diff)
downloadframeworks_base-2ed20f87684a892d640a91f1961f369bbbc7d05e.zip
frameworks_base-2ed20f87684a892d640a91f1961f369bbbc7d05e.tar.gz
frameworks_base-2ed20f87684a892d640a91f1961f369bbbc7d05e.tar.bz2
Avoid leaking sockets.
When a DNS lookup fails, we end up creating a datagram socket but never properly close it. This wastes limited valuable file descriptors and causes CTS test failures. Generally, we should ensure that sockets or files are opened in a try block, and closed in a finally block. Bug: 3276283 Change-Id: I3432f3bc061268c7367948230f6edbdfcec55892
Diffstat (limited to 'core/java')
-rw-r--r--core/java/android/net/SntpClient.java8
1 files changed, 6 insertions, 2 deletions
diff --git a/core/java/android/net/SntpClient.java b/core/java/android/net/SntpClient.java
index f607ee9..3e21e2d 100644
--- a/core/java/android/net/SntpClient.java
+++ b/core/java/android/net/SntpClient.java
@@ -72,8 +72,9 @@ public class SntpClient
* @return true if the transaction was successful.
*/
public boolean requestTime(String host, int timeout) {
+ DatagramSocket socket = null;
try {
- DatagramSocket socket = new DatagramSocket();
+ socket = new DatagramSocket();
socket.setSoTimeout(timeout);
InetAddress address = InetAddress.getByName(host);
byte[] buffer = new byte[NTP_PACKET_SIZE];
@@ -96,7 +97,6 @@ public class SntpClient
socket.receive(response);
long responseTicks = SystemClock.elapsedRealtime();
long responseTime = requestTime + (responseTicks - requestTicks);
- socket.close();
// extract the results
long originateTime = readTimeStamp(buffer, ORIGINATE_TIME_OFFSET);
@@ -123,6 +123,10 @@ public class SntpClient
} catch (Exception e) {
if (Config.LOGD) Log.d(TAG, "request time failed: " + e);
return false;
+ } finally {
+ if (socket != null) {
+ socket.close();
+ }
}
return true;