Fix missing close for canceled bundle downloads

Summary:
`Response` is `Closeable`, so we must close it even if the download is no longer relevant. Found while running with StrictMode enabled and reloading quickly multiple times.

Changelog: [Internal]

Reviewed By: NickGerleman

Differential Revision: D56629079

fbshipit-source-id: 041bf295313cbf78b7f2bb6580c50fdc2a324728
This commit is contained in:
Pieter De Baets
2024-04-29 21:10:28 -07:00
committed by Facebook GitHub Bot
parent b7de916664
commit dd51b38101
@@ -105,7 +105,6 @@ public class BundleDownloader {
final String bundleURL,
final @Nullable BundleInfo bundleInfo,
Request.Builder requestBuilder) {
final Request request =
requestBuilder.url(bundleURL).addHeader("Accept", "multipart/mixed").build();
mDownloadBundleFromURLCall = Assertions.assertNotNull(mClient.newCall(request));
@@ -129,20 +128,19 @@ public class BundleDownloader {
@Override
public void onResponse(Call call, final Response response) throws IOException {
// ignore callback if call was cancelled
if (mDownloadBundleFromURLCall == null || mDownloadBundleFromURLCall.isCanceled()) {
mDownloadBundleFromURLCall = null;
return;
}
mDownloadBundleFromURLCall = null;
final String url = response.request().url().toString();
// Make sure the result is a multipart response and parse the boundary.
String contentType = response.header("content-type");
Pattern regex = Pattern.compile("multipart/mixed;.*boundary=\"([^\"]+)\"");
Matcher match = regex.matcher(contentType);
try (Response r = response) {
// ignore callback if call was cancelled
if (mDownloadBundleFromURLCall == null || mDownloadBundleFromURLCall.isCanceled()) {
mDownloadBundleFromURLCall = null;
return;
}
mDownloadBundleFromURLCall = null;
final String url = response.request().url().toString();
// Make sure the result is a multipart response and parse the boundary.
String contentType = response.header("content-type");
Pattern regex = Pattern.compile("multipart/mixed;.*boundary=\"([^\"]+)\"");
Matcher match = regex.matcher(contentType);
if (match.find()) {
processMultipartResponse(url, r, match.group(1), outputFile, bundleInfo, callback);
} else {