Skip to content

fix: return error immediately on roundtrip error#29

Merged
bartventer merged 4 commits intobartventer:masterfrom
cih9088:fix/handle-error-on-validation
Mar 12, 2026
Merged

fix: return error immediately on roundtrip error#29
bartventer merged 4 commits intobartventer:masterfrom
cih9088:fix/handle-error-on-validation

Conversation

@cih9088
Copy link
Contributor

@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
}

@bartventer bartventer force-pushed the fix/handle-error-on-validation branch from 4cd53ec to 1135d10 Compare March 12, 2026 16:58
Copy link
Owner

@bartventer bartventer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks.

@bartventer bartventer merged commit 1b1dd0f into bartventer:master Mar 12, 2026
4 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.

2 participants