From 20d3f83f27946f9654c48f096b2f671f7d5e3662 Mon Sep 17 00:00:00 2001 From: Matthias Sohn Date: Wed, 24 Aug 2016 14:08:00 +0200 Subject: [PATCH] Fix HttpClientConnection leaking temporary buffer files 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 --- .../http/apache/HttpClientConnection.java | 30 ++++++++++++------- .../http/apache/TemporaryBufferEntity.java | 15 +++++++++- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/org.eclipse.jgit.http.apache/src/org/eclipse/jgit/transport/http/apache/HttpClientConnection.java b/org.eclipse.jgit.http.apache/src/org/eclipse/jgit/transport/http/apache/HttpClientConnection.java index a0eeef89fc..2d9d17a890 100644 --- a/org.eclipse.jgit.http.apache/src/org/eclipse/jgit/transport/http/apache/HttpClientConnection.java +++ b/org.eclipse.jgit.http.apache/src/org/eclipse/jgit/transport/http/apache/HttpClientConnection.java @@ -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> getHeaderFields() { diff --git a/org.eclipse.jgit.http.apache/src/org/eclipse/jgit/transport/http/apache/TemporaryBufferEntity.java b/org.eclipse.jgit.http.apache/src/org/eclipse/jgit/transport/http/apache/TemporaryBufferEntity.java index 1ff168e237..377e5ca572 100644 --- a/org.eclipse.jgit.http.apache/src/org/eclipse/jgit/transport/http/apache/TemporaryBufferEntity.java +++ b/org.eclipse.jgit.http.apache/src/org/eclipse/jgit/transport/http/apache/TemporaryBufferEntity.java @@ -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(); + } + } } -- 2.39.5