Skip to content

Commit ab6e869

Browse files
authored
修复缓存异常时 FetchTask 无限循环的问题 (#4844)
1 parent 97ada51 commit ab6e869

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

HMCL/src/main/java/org/jackhuang/hmcl/upgrade/UpdateChecker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public static void requestCheckUpdate(UpdateChannel channel, boolean preview) {
112112
try {
113113
result = checkUpdate(channel, preview);
114114
LOG.info("Latest version (" + channel + ", preview=" + preview + ") is " + result);
115-
} catch (IOException e) {
115+
} catch (Throwable e) {
116116
LOG.warning("Failed to check for update", e);
117117
}
118118

HMCLCore/src/main/java/org/jackhuang/hmcl/task/FetchTask.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@ private void downloadHttp(URI uri, boolean checkETag) throws DownloadException,
187187

188188
ArrayList<IOException> exceptions = null;
189189

190-
for (int retryTime = 0; retryTime < retry; retryTime++) {
190+
// If loading the cache fails, the cache should not be loaded again.
191+
boolean useCachedResult = true;
192+
for (int retryTime = 0, retryLimit = retry; retryTime < retryLimit; retryTime++) {
191193
if (isCancelled()) {
192194
throw new InterruptedException();
193195
}
@@ -204,7 +206,7 @@ private void downloadHttp(URI uri, boolean checkETag) throws DownloadException,
204206

205207
LinkedHashMap<String, String> headers = new LinkedHashMap<>();
206208
headers.put("accept-encoding", "gzip");
207-
if (checkETag)
209+
if (useCachedResult && checkETag)
208210
headers.putAll(repository.injectConnection(uri));
209211

210212
do {
@@ -248,7 +250,7 @@ private void downloadHttp(URI uri, boolean checkETag) throws DownloadException,
248250
} while (true);
249251

250252
int responseCode = response.statusCode();
251-
if (responseCode == HttpURLConnection.HTTP_NOT_MODIFIED) {
253+
if (useCachedResult && responseCode == HttpURLConnection.HTTP_NOT_MODIFIED) {
252254
// Handle cache
253255
try {
254256
Path cache = repository.getCachedRemoteFile(currentURI, false);
@@ -260,9 +262,10 @@ private void downloadHttp(URI uri, boolean checkETag) throws DownloadException,
260262
} catch (IOException e) {
261263
LOG.warning("Unable to use cached file, redownload " + NetworkUtils.dropQuery(uri), e);
262264
repository.removeRemoteEntry(currentURI);
265+
useCachedResult = false;
263266
// Now we must reconnect the server since 304 may result in empty content,
264267
// if we want to redownload the file, we must reconnect the server without etag settings.
265-
retryTime--;
268+
retryLimit++;
266269
continue;
267270
}
268271
} else if (responseCode / 100 == 4) {

0 commit comments

Comments
 (0)