Evaluate cache strategy before enforcing failure response code#3401
Merged
colinrtwhite merged 1 commit intocoil-kt:mainfrom Apr 24, 2026
Merged
Conversation
When a failure response (e.g. 404) was cached to disk via CACHEABLE_STATUS_CODES, NetworkFetcher threw HttpException before invoking CacheStrategy.read(). This prevented cache-aware strategies (e.g. CacheControlCacheStrategy) from ever seeing the cached response, so a cached failure could never expire via cache-control headers. Move the failure code check until after CacheStrategy.read() returns a response. When the strategy opts to reuse the cached response, the check still fires and preserves existing behavior for the default strategy. When the strategy returns a fresh request instead, the fetcher now proceeds to the network as expected. Resolves coil-kt#3391
Member
|
Thanks this looks like the proper fix. |
colinrtwhite
approved these changes
Apr 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Resolves #3391.
NetworkFetchercached non-2xx responses listed inCACHEABLE_STATUS_CODES(300, 301, 404, 405, 410, 414, 501) to disk, but then unconditionally threwHttpExceptionon the next read beforeCacheStrategy.read()was invoked. As a result, cache-aware strategies such asCacheControlCacheStrategycould never evaluate whether the cached failure had expired — a cached 404 would throw forever, regardless ofCache-Controlheaders.Changes
throwIfFailureResponseCode(cacheResponse)until aftercacheStrategy.read(...)returns a response.NetworkRequestinstead (e.g. because the cached entry is stale), the fetcher proceeds to the network as expected.This keeps the existing behavior for
CacheStrategy.DEFAULT(which always returns the cached response) and unblocksCacheControlCacheStrategyfrom honoring expiration on cached failures.Testing
./gradlew :coil-network-core:jvmTest --tests coil3.network.NetworkFetcherTest— all 5 tests pass.cached404ResponseThrowsstill passes (default strategy preserves current behavior).cached404IsRefreshedWhenCacheStrategyRequestsNewRequestverifies that when a strategy returnsReadResult(request), the cached 404 no longer short-circuits withHttpExceptionand the network request is issued../gradlew :coil-network-core:spotlessCheck— passes.