Skip to content

fix: Ensure response body is always closed in async revalidation. #47

Open
sendya wants to merge 1 commit intomainfrom
fix/revalidate-leak-resp-bodyclose
Open

fix: Ensure response body is always closed in async revalidation. #47
sendya wants to merge 1 commit intomainfrom
fix/revalidate-leak-resp-bodyclose

Conversation

@sendya
Copy link
Member

@sendya sendya commented Mar 10, 2026

This pull request makes a small but important change to the asyncRevalidate method in caching_revalidate.go to ensure proper resource cleanup and error handling.

  • Resource cleanup improvement: The defer closeBody(resp) statement is now placed immediately after the upstream request, ensuring that the response body is closed even if the request fails and resp is nil. This prevents potential resource leaks and makes the error handling safer.

Copilot AI review requested due to automatic review settings March 10, 2026 07:36
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Ensures background (“fuzzy”) async revalidation always closes the upstream HTTP response body, improving resource cleanup and preventing potential leaks when upstream requests return a non-nil response alongside an error.

Changes:

  • Move defer closeBody(resp) to immediately after c.doProxy(...) in asyncRevalidate so it runs on both success and error paths.
  • Minor whitespace/formatting cleanup in related functions.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


// Perform the upstream request
resp, err := c.doProxy(bgReq, false)
defer closeBody(resp) // always check resp nil
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

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

The inline comment on the new defer closeBody(resp) is misleading: closeBody already guards nil responses, and the real reason to defer before the error check is that doProxy can return a non-nil resp alongside a non-nil err (so the body must still be closed). Consider updating/removing the comment to reflect that behavior to avoid confusion for future readers.

Suggested change
defer closeBody(resp) // always check resp nil
// Defer closing the response body even if doProxy returns a non-nil resp alongside a non-nil err.
defer closeBody(resp)

Copilot uses AI. Check for mistakes.
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