]> source.dussan.org Git - jgit.git/commitdiff
Fix HttpClientConnection leaking temporary buffer files 22/79622/1
authorMatthias Sohn <matthias.sohn@sap.com>
Wed, 24 Aug 2016 12:08:00 +0000 (14:08 +0200)
committerMatthias Sohn <matthias.sohn@sap.com>
Wed, 24 Aug 2016 12:08:00 +0000 (14:08 +0200)
HttpClientConnection uses a TemporaryBufferEntity which uses
TemporaryBuffer.LocalFile to buffer an HttpEntity. It was leaking
temporary files if the buffered entities were larger than 1MB since it
failed to destroy the TemporaryBuffer.LocalFile.

Bug: 500079
Change-Id: Ib963e04efc252bdd0420a5c69b1a19181e9e6169
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
org.eclipse.jgit.http.apache/src/org/eclipse/jgit/transport/http/apache/HttpClientConnection.java
org.eclipse.jgit.http.apache/src/org/eclipse/jgit/transport/http/apache/TemporaryBufferEntity.java

index a0eeef89fc7263d1b5cf34afdca42a56210f92d5..2d9d17a890281eaeb0e4ed810cbe78306a501da2 100644 (file)
@@ -226,17 +226,25 @@ public class HttpClientConnection implements HttpConnection {
        }
 
        private void execute() throws IOException, ClientProtocolException {
-               if (resp == null)
-                       if (entity != null) {
-                               if (req instanceof HttpEntityEnclosingRequest) {
-                                       HttpEntityEnclosingRequest eReq = (HttpEntityEnclosingRequest) req;
-                                       eReq.setEntity(entity);
-                               }
-                               resp = getClient().execute(req);
-                               entity.getBuffer().close();
-                               entity = null;
-                       } else
-                               resp = getClient().execute(req);
+               if (resp != null) {
+                       return;
+               }
+
+               if (entity == null) {
+                       resp = getClient().execute(req);
+                       return;
+               }
+
+               try {
+                       if (req instanceof HttpEntityEnclosingRequest) {
+                               HttpEntityEnclosingRequest eReq = (HttpEntityEnclosingRequest) req;
+                               eReq.setEntity(entity);
+                       }
+                       resp = getClient().execute(req);
+               } finally {
+                       entity.close();
+                       entity = null;
+               }
        }
 
        public Map<String, List<String>> getHeaderFields() {
index 1ff168e2377bcbb1e53f2983069c62e65959a91d..377e5ca572de7952c22f681e634c63b1437a06d1 100644 (file)
@@ -55,7 +55,8 @@ import org.eclipse.jgit.util.TemporaryBuffer;
  *
  * @since 3.3
  */
-public class TemporaryBufferEntity extends AbstractHttpEntity {
+public class TemporaryBufferEntity extends AbstractHttpEntity
+               implements AutoCloseable {
        private TemporaryBuffer buffer;
 
        private Integer contentLength;
@@ -106,4 +107,16 @@ public class TemporaryBufferEntity extends AbstractHttpEntity {
        public void setContentLength(int contentLength) {
                this.contentLength = new Integer(contentLength);
        }
+
+       /**
+        * Close destroys the associated buffer used to buffer the entity
+        *
+        * @since 4.5
+        */
+       @Override
+       public void close() {
+               if (buffer != null) {
+                       buffer.destroy();
+               }
+       }
 }