Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions modules/caddyhttp/reverseproxy/reverseproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,13 @@ func (h *Handler) reverseProxy(rw http.ResponseWriter, req *http.Request, origRe
res.Body, _ = h.bufferedBody(res.Body, h.ResponseBuffers)
}

var bodyReleased bool
defer func() {
if !bodyReleased && res != nil && res.Body != nil {
res.Body.Close()
}
}()

// set response placeholders so they can be used in retry match
// expressions and handle_response routes; clear stale header
// placeholders from a previous attempt first so they don't
Expand Down Expand Up @@ -1139,6 +1146,7 @@ func (h *Handler) reverseProxy(rw http.ResponseWriter, req *http.Request, origRe
}
if match {
res.Body.Close()
bodyReleased = true
return retryableResponseError{
error: fmt.Errorf("upstream response matched retry_match (status %d)", res.StatusCode),
statusCode: res.StatusCode,
Expand Down Expand Up @@ -1196,6 +1204,7 @@ func (h *Handler) reverseProxy(rw http.ResponseWriter, req *http.Request, origRe
if !hrc.isFinalized {
res.Body.Close()
}
bodyReleased = true

// wrap any route error in roundtripSucceededError so caller knows that
// the roundtrip was successful and to not retry
Expand All @@ -1209,6 +1218,7 @@ func (h *Handler) reverseProxy(rw http.ResponseWriter, req *http.Request, origRe
}

// copy the response body and headers back to the upstream client
bodyReleased = true
return h.finalizeResponse(rw, req, res, repl, start, logger)
}

Expand Down