[tor-commits] [metrics-lib/master] Prevent blocking after an exception.

karsten at torproject.org karsten at torproject.org
Sat Jul 11 06:27:30 UTC 2015


commit ca0cf734c5dfc1b40bd23014bd4c13cf089ab871
Author: leeroy <ter.one.leeboi at hush.com>
Date:   Fri Jul 10 20:26:56 2015 -0400

    Prevent blocking after an exception.
---
 .../descriptor/impl/DescriptorCollectorImpl.java           |   12 ++++++++++--
 .../torproject/descriptor/impl/DirectoryDownloader.java    |    7 +++++--
 2 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/src/org/torproject/descriptor/impl/DescriptorCollectorImpl.java b/src/org/torproject/descriptor/impl/DescriptorCollectorImpl.java
index e5f2f9c..5373b9d 100644
--- a/src/org/torproject/descriptor/impl/DescriptorCollectorImpl.java
+++ b/src/org/torproject/descriptor/impl/DescriptorCollectorImpl.java
@@ -112,9 +112,10 @@ public class DescriptorCollectorImpl implements DescriptorCollector {
 
   String fetchRemoteDirectory(String url) {
     StringBuilder sb = new StringBuilder();
+    HttpURLConnection huc = null;
     try {
       URL u = new URL(url);
-      HttpURLConnection huc = (HttpURLConnection) u.openConnection();
+      huc = (HttpURLConnection) u.openConnection();
       huc.setRequestMethod("GET");
       huc.connect();
       int responseCode = huc.getResponseCode();
@@ -129,6 +130,9 @@ public class DescriptorCollectorImpl implements DescriptorCollector {
       }
     } catch (IOException e) {
       e.printStackTrace();
+      if (huc != null) {
+        huc.disconnect();
+      }
       return "";
     }
     return sb.toString();
@@ -184,6 +188,7 @@ public class DescriptorCollectorImpl implements DescriptorCollector {
 
   void fetchRemoteFile(String url, File destinationFile,
       long lastModifiedMillis) {
+    HttpURLConnection huc = null;
     try {
       File destinationDirectory = destinationFile.getParentFile();
       destinationDirectory.mkdirs();
@@ -191,7 +196,7 @@ public class DescriptorCollectorImpl implements DescriptorCollector {
           + destinationFile.getName());
       FileOutputStream fos = new FileOutputStream(tempDestinationFile);
       URL u = new URL(url);
-      HttpURLConnection huc = (HttpURLConnection) u.openConnection();
+      huc = (HttpURLConnection) u.openConnection();
       huc.setRequestMethod("GET");
       if (!url.endsWith(".xz")) {
         huc.addRequestProperty("Accept-Encoding", "gzip");
@@ -219,6 +224,9 @@ public class DescriptorCollectorImpl implements DescriptorCollector {
       }
     } catch (IOException e) {
       e.printStackTrace();
+      if (huc != null) {
+        huc.disconnect();
+      }
     }
   }
 
diff --git a/src/org/torproject/descriptor/impl/DirectoryDownloader.java b/src/org/torproject/descriptor/impl/DirectoryDownloader.java
index f10fc83..4266217 100644
--- a/src/org/torproject/descriptor/impl/DirectoryDownloader.java
+++ b/src/org/torproject/descriptor/impl/DirectoryDownloader.java
@@ -58,10 +58,10 @@ public class DirectoryDownloader implements Runnable {
         String url = "http://" + this.ipPort
             + request.getRequestedResource();
         request.setRequestStart(System.currentTimeMillis());
+        HttpURLConnection huc = null;
         try {
           URL u = new URL(url);
-          HttpURLConnection huc =
-              (HttpURLConnection) u.openConnection();
+          huc = (HttpURLConnection) u.openConnection();
           huc.setConnectTimeout((int) this.connectTimeout);
           huc.setReadTimeout((int) this.readTimeout);
           huc.setRequestMethod("GET");
@@ -86,6 +86,9 @@ public class DirectoryDownloader implements Runnable {
           }
         } catch (Exception e) {
           request.setException(e);
+          if (huc != null) {
+            huc.disconnect();
+          }
           /* Stop downloading from this directory if there are any
            * problems, e.g., refused connections. */
           keepRunning = false;



More information about the tor-commits mailing list