Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: close response Body when the returned error is nil #146

Merged
merged 2 commits into from
Apr 25, 2022

Conversation

jlikeme
Copy link
Contributor

@jlikeme jlikeme commented Apr 24, 2022

Issue: #145

According to this official http client documentation, If the returned error is nil, the Response will contain a non-nil Body which the user is expected to close.. The Response Body must be closed when the returned error is nil. And we don't need check whether the Response Body is nil.

If your build fails due to your commit message not passing the build checks, please review the guidelines here: https://github.com/edgexfoundry/go-mod-secrets/blob/main/.github/Contributing.md

PR Checklist

Please check if your PR fulfills the following requirements:

  • I am not introducing a breaking change (if you are, flag in conventional commit message with BREAKING CHANGE: describing the break)
  • I am not introducing a new dependency (add notes below if you are)
  • I have added unit tests for the new feature or bug fix (if not, why?)
  • I have fully tested (add details below) this the new feature or bug fix (if not, why?)
  • I have opened a PR for the related docs change (if not, why?)

Testing Instructions

New Dependency Instructions (If applicable)

if resp.Body != nil {
_ = resp.Body.Close()
}
_ = resp.Body.Close()
Copy link
Collaborator

@bnevis-i bnevis-i Apr 24, 2022

Choose a reason for hiding this comment

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

Why was the resp.Body nil check removed here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

According to this official http client documentation, If the returned error is nil, the Response will contain a non-nil Body

Copy link
Collaborator

Choose a reason for hiding this comment

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

If the returned error is nil, the Response will contain a non-nil Body which the user is expected to close. If the Body is not both read to EOF and closed, the Client's underlying RoundTripper (typically Transport) may not be able to re-use a persistent TCP connection to the server for a subsequent "keep-alive" request.

On error, any Response can be ignored. A non-nil Response with a non-nil error only occurs when CheckRedirect fails, and even then the returned Response.Body is already closed.

According to the above, on success, we need to close the Body explicitly. On error, the body is already closed.

As long as the double close doesn't result in a panic(), I am ok with this.

@@ -370,15 +370,14 @@ func (c *Client) getAllKeys(subPath string) (map[string]string, error) {
if err != nil {
return nil, err
}
defer func() {
_ = resp.Body.Close()
Copy link
Collaborator

Choose a reason for hiding this comment

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

This code is not checking that resp.Body is nil or not before calling Close

bnevis-i
bnevis-i previously approved these changes Apr 24, 2022
@bnevis-i
Copy link
Collaborator

bnevis-i commented Apr 24, 2022

@gao270615179 You need to sign your commit before it can be merged. Please sign the commit (commit --amend -s and maybe -S) and force-push.

@codecov-commenter
Copy link

Codecov Report

Merging #146 (d2972b5) into main (8f104b7) will increase coverage by 0.22%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #146      +/-   ##
==========================================
+ Coverage   77.66%   77.88%   +0.22%     
==========================================
  Files          17       17              
  Lines         900      900              
==========================================
+ Hits          699      701       +2     
+ Misses        143      142       -1     
+ Partials       58       57       -1     
Impacted Files Coverage Δ
internal/pkg/vault/secrets.go 74.45% <100.00%> (+0.86%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8f104b7...d2972b5. Read the comment docs.

@jlikeme
Copy link
Contributor Author

jlikeme commented Apr 25, 2022

@bnevis-i

You need to sign your commit before it can be merged.

I have signed the commit and fixed secrets_test InMemoryMockCaller response.

@bnevis-i bnevis-i merged commit 3ba02e4 into edgexfoundry:main Apr 25, 2022
@lenny-goodell lenny-goodell linked an issue Apr 25, 2022 that may be closed by this pull request
bnevis-i added a commit that referenced this pull request Jun 1, 2022
* fix: close response Body when the returned error is nil

Signed-off-by: gao270615179 <[email protected]>

* fix: add InMemoryMockCaller response body

Signed-off-by: gao270615179 <[email protected]>

Co-authored-by: gao270615179 <[email protected]>
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.

resp.Body is not closed after return 404
3 participants