Skip to content

Commit 73a8054

Browse files
committed
Always set stream when connecting HttpUrlConnection
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 bumptech#2352
1 parent 7f62597 commit 73a8054

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

library/src/main/java/com/bumptech/glide/load/data/HttpUrlFetcher.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ private InputStream loadDataWithRedirects(URL url, int redirects, URL lastUrl,
100100

101101
// Connect explicitly to avoid errors in decoders if connection fails.
102102
urlConnection.connect();
103+
// Set the stream so that it's closed in cleanup to avoid resource leaks. See #2352.
104+
stream = urlConnection.getInputStream();
103105
if (isCancelled) {
104106
return null;
105107
}
@@ -114,9 +116,7 @@ private InputStream loadDataWithRedirects(URL url, int redirects, URL lastUrl,
114116
URL redirectUrl = new URL(url, redirectUrlString);
115117
// Closing the stream specifically is required to avoid leaking ResponseBodys in addition
116118
// to disconnecting the url connection below. See #2352.
117-
urlConnection.getInputStream().close();
118-
urlConnection.disconnect();
119-
urlConnection = null;
119+
cleanup();
120120
return loadDataWithRedirects(redirectUrl, redirects + 1, url, headers);
121121
} else if (statusCode == -1) {
122122
throw new HttpException(statusCode);
@@ -151,6 +151,7 @@ public void cleanup() {
151151
if (urlConnection != null) {
152152
urlConnection.disconnect();
153153
}
154+
urlConnection = null;
154155
}
155156

156157
@Override

0 commit comments

Comments
 (0)