Make sure we close HTTP response body#815
Conversation
| func doRequest(httpCli *http.Client, req *http.Request, token string) ([]byte, error) { | ||
| req.Header.Set("Authorization", "Bearer "+token) | ||
| res, err := httpCli.Do(req) | ||
| defer internal.CloseIO( |
There was a problem hiding this comment.
There is a bodyclose lint which is also available for golangci-lint but I'm not willing to put in the effort (at this time) to update the the Complement codebase to utilize the latest golangci-lint and add it to CI, etc. For reference, it seems like we do use golangci-lint as part of build/scripts/build-and-lint.sh already but that's not in CI and I've never realized it's there while developing tests.
Additionally, there are many false-positives with the bodyclose. For example, we would immediately hit timakin/bodyclose#39 as our CloseIO utility is in a different package than our various usages. See also the issue list as there are many "false positive" issues.
And is not very well-suited for our use case since we don't expect downstream usages of user.MustDo(...) and user.Do(...) to handle the HTTP response as we handle it for them. Basically tracked by timakin/bodyclose#11 and timakin/bodyclose#76
anoadragon453
left a comment
There was a problem hiding this comment.
This is always something that's so tricky to get right.
Nice work with the helper function and using it everywhere.
|
Thanks for the review @anoadragon453 🦞 |
Make sure we close HTTP response body as that's what the docs say:
But it also may leak resources 🤷
Spawning from exploring this elsewhere in https://github.com/element-hq/synapse-small-hosts/pull/303
Dev notes
Error handling with
res.Body.Close():There is a
bodycloselint which is also available forgolangci-lintThere are many false-positives though, for example:
HTTP client methods that deal with response bodies:
Pull Request Checklist
Signed-off-by: Eric Eastwood erice@element.io