Skip to content

Conversation

@cih9088
Copy link

@cih9088 cih9088 commented Jan 20, 2026

Rationale

From https://pkg.go.dev/net/http#RoundTripper

In particular, RoundTrip must return err == nil if it obtained a response, regardless of the response's HTTP status code. A non-nil err should be reserved for failure to obtain a response

So if err != nil, resp is nil which causes nil pointer dereference on line 94 of the following snippet.

if (err != nil || isStaleErrorAllowed(resp.StatusCode)) && req.Method == http.MethodGet {
ccResp = ParseCCResponseDirectives(resp.Header)
ccRespOnce = true
if r.siep.CanStaleOnError(ctx.Freshness, ccResp) {
// RFC 9111 §4.2.4 Serving Stale Responses
// RFC 9111 §4.3.3 Handling Validation Responses (5xx errors)
SetAgeHeader(ctx.Stored.Data, r.clock, ctx.Freshness.Age)
CacheStatusStale.ApplyTo(ctx.Stored.Data.Header)
r.l.LogCacheStaleIfError(req, ctx.URLKey, ctx.ToMisc(ccResp))
return ctx.Stored.Data, nil
}
}

Changed

  • return err immediately on upstream roundtripper error
  • HandleValidationResponse does not accept error anymore.

Note that on cache miss and background revalidation returns error immediately.

On cache miss,

httpcache/roundtripper.go

Lines 271 to 274 in 60e8c9f

resp, start, end, err := r.roundTripTimed(req)
if err != nil {
return nil, err
}

On background revalidation,

httpcache/roundtripper.go

Lines 424 to 428 in 60e8c9f

resp, start, end, err := r.roundTripTimed(req)
if err != nil {
errc <- err
return
}

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.

1 participant