Skip to content

Evaluate cache strategy before enforcing failure response code#3401

Merged
colinrtwhite merged 1 commit intocoil-kt:mainfrom
leesgit:fix/3391-evaluate-cache-strategy-before-failure-code
Apr 24, 2026
Merged

Evaluate cache strategy before enforcing failure response code#3401
colinrtwhite merged 1 commit intocoil-kt:mainfrom
leesgit:fix/3391-evaluate-cache-strategy-before-failure-code

Conversation

@leesgit
Copy link
Copy Markdown
Contributor

@leesgit leesgit commented Apr 24, 2026

Summary

Resolves #3391.

NetworkFetcher cached non-2xx responses listed in CACHEABLE_STATUS_CODES (300, 301, 404, 405, 410, 414, 501) to disk, but then unconditionally threw HttpException on the next read before CacheStrategy.read() was invoked. As a result, cache-aware strategies such as CacheControlCacheStrategy could never evaluate whether the cached failure had expired — a cached 404 would throw forever, regardless of Cache-Control headers.

Changes

  • Moved throwIfFailureResponseCode(cacheResponse) until after cacheStrategy.read(...) returns a response.
  • The check now runs only when the strategy opts to reuse the cached response. When the strategy returns a fresh NetworkRequest instead (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 unblocks CacheControlCacheStrategy from honoring expiration on cached failures.

Testing

  • ./gradlew :coil-network-core:jvmTest --tests coil3.network.NetworkFetcherTest — all 5 tests pass.
  • Existing cached404ResponseThrows still passes (default strategy preserves current behavior).
  • New cached404IsRefreshedWhenCacheStrategyRequestsNewRequest verifies that when a strategy returns ReadResult(request), the cached 404 no longer short-circuits with HttpException and the network request is issued.
  • ./gradlew :coil-network-core:spotlessCheck — passes.

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
@colinrtwhite
Copy link
Copy Markdown
Member

Thanks this looks like the proper fix.

@colinrtwhite colinrtwhite merged commit 6a20523 into coil-kt:main Apr 24, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Non-200 (or 304) cached response never expired

2 participants