修复缓存异常时 FetchTask 无限循环的问题 (#4844)

This commit is contained in:
Glavo
2025-11-23 15:20:14 +08:00
committed by GitHub
parent 97ada51489
commit ab6e8692ad
2 changed files with 8 additions and 5 deletions

View File

@@ -112,7 +112,7 @@ public final class UpdateChecker {
try {
result = checkUpdate(channel, preview);
LOG.info("Latest version (" + channel + ", preview=" + preview + ") is " + result);
} catch (IOException e) {
} catch (Throwable e) {
LOG.warning("Failed to check for update", e);
}

View File

@@ -187,7 +187,9 @@ public abstract class FetchTask<T> extends Task<T> {
ArrayList<IOException> exceptions = null;
for (int retryTime = 0; retryTime < retry; retryTime++) {
// If loading the cache fails, the cache should not be loaded again.
boolean useCachedResult = true;
for (int retryTime = 0, retryLimit = retry; retryTime < retryLimit; retryTime++) {
if (isCancelled()) {
throw new InterruptedException();
}
@@ -204,7 +206,7 @@ public abstract class FetchTask<T> extends Task<T> {
LinkedHashMap<String, String> headers = new LinkedHashMap<>();
headers.put("accept-encoding", "gzip");
if (checkETag)
if (useCachedResult && checkETag)
headers.putAll(repository.injectConnection(uri));
do {
@@ -248,7 +250,7 @@ public abstract class FetchTask<T> extends Task<T> {
} while (true);
int responseCode = response.statusCode();
if (responseCode == HttpURLConnection.HTTP_NOT_MODIFIED) {
if (useCachedResult && responseCode == HttpURLConnection.HTTP_NOT_MODIFIED) {
// Handle cache
try {
Path cache = repository.getCachedRemoteFile(currentURI, false);
@@ -260,9 +262,10 @@ public abstract class FetchTask<T> extends Task<T> {
} catch (IOException e) {
LOG.warning("Unable to use cached file, redownload " + NetworkUtils.dropQuery(uri), e);
repository.removeRemoteEntry(currentURI);
useCachedResult = false;
// Now we must reconnect the server since 304 may result in empty content,
// if we want to redownload the file, we must reconnect the server without etag settings.
retryTime--;
retryLimit++;
continue;
}
} else if (responseCode / 100 == 4) {