fix: Ensure response body is always closed in async revalidation. #47
fix: Ensure response body is always closed in async revalidation. #47
Conversation
There was a problem hiding this comment.
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 afterc.doProxy(...)inasyncRevalidateso 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 |
There was a problem hiding this comment.
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.
| 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) |
This pull request makes a small but important change to the
asyncRevalidatemethod incaching_revalidate.goto ensure proper resource cleanup and error handling.defer closeBody(resp)statement is now placed immediately after the upstream request, ensuring that the response body is closed even if the request fails andrespis nil. This prevents potential resource leaks and makes the error handling safer.