From 73a8054f2bc4833598426ba5b09c243b21f752f3 Mon Sep 17 00:00:00 2001 From: Sam Judd Date: Thu, 7 Sep 2017 10:01:36 -0700 Subject: [PATCH] Always set stream when connecting HttpUrlConnection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit That way we will always close the stream in cleanup(). Prior to this change, when cancel was called, stream was never set to non-null and therefore never closed in cleanup. Because disconnecting the stream doesn’t seem to close the contained ResponseBody object, failing to obtain and then close the stream results in leaked objects. Progress towards #2352 --- .../java/com/bumptech/glide/load/data/HttpUrlFetcher.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/library/src/main/java/com/bumptech/glide/load/data/HttpUrlFetcher.java b/library/src/main/java/com/bumptech/glide/load/data/HttpUrlFetcher.java index 31b7ee467..6989129cc 100644 --- a/library/src/main/java/com/bumptech/glide/load/data/HttpUrlFetcher.java +++ b/library/src/main/java/com/bumptech/glide/load/data/HttpUrlFetcher.java @@ -100,6 +100,8 @@ public class HttpUrlFetcher implements DataFetcher { // Connect explicitly to avoid errors in decoders if connection fails. urlConnection.connect(); + // Set the stream so that it's closed in cleanup to avoid resource leaks. See #2352. + stream = urlConnection.getInputStream(); if (isCancelled) { return null; } @@ -114,9 +116,7 @@ public class HttpUrlFetcher implements DataFetcher { URL redirectUrl = new URL(url, redirectUrlString); // Closing the stream specifically is required to avoid leaking ResponseBodys in addition // to disconnecting the url connection below. See #2352. - urlConnection.getInputStream().close(); - urlConnection.disconnect(); - urlConnection = null; + cleanup(); return loadDataWithRedirects(redirectUrl, redirects + 1, url, headers); } else if (statusCode == -1) { throw new HttpException(statusCode); @@ -151,6 +151,7 @@ public class HttpUrlFetcher implements DataFetcher { if (urlConnection != null) { urlConnection.disconnect(); } + urlConnection = null; } @Override -- GitLab