提交 73a8054f 编写于 作者: S Sam Judd

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 #2352
上级 7f625977
......@@ -100,6 +100,8 @@ public class HttpUrlFetcher implements DataFetcher<InputStream> {
// 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<InputStream> {
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<InputStream> {
if (urlConnection != null) {
urlConnection.disconnect();
}
urlConnection = null;
}
@Override
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册