-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Close response body in error case and close first one #341
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
Conversation
Incidentally, I think there may be one more subtle leak, but I want someone else to verify. On this line we initially get an *http.Response, which has a body that must be closed. I think the body for that very first response gets closed if and only if it comes back with a 201 or 202 status code. If it doesn't, we replace I actually caught this thanks to Sourcegraph's code browser; when I clicked the Am I missing something or is that also a bug? |
Actually, I'm not sure if my initial PR was entirely correct. There are some places where handleHTTPError is called after a |
acme/client.go
Outdated
|
||
break | ||
default: | ||
defer resp.Body.Close() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not put this defer directly after the post, i.e. line 612? Then the Close
in the other switch can be removed as well.
But yeah, def. a leak here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also make a new const of maxBodySize, and cap the number of polls to a maximum of 1000.
@xenolf @miekg I've pushed a commit that ensures the body for both responses is closed. I had to pull the select out into its own function in order for it to be reasonably readable. I also took Tested and it works :) Please review ASAP. (Will go forward including this in new Caddy releases either way, though.) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @mholt thanks for catching that. The latest revision of the changes is looking good.
acme/client.go
Outdated
} | ||
|
||
// maxBodySize is the maximum size of body that we will read. | ||
const maxBodySize = 1024 * 1024 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we move that up to the top? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure! Will do in a little bit. (Edit: Done!)
I noticed that the response body is not closed in the error case of waiting for a certificate to be issued: https://sourcegraph.com/github.com/xenolf/lego@ce8fb060cb8361a9ff8b5fb7c2347fa907b6fcac/-/blob/acme/client.go#L673-674
AFAIK the response body needs to be closed even if there isn't one or it is not read. We close it above if the response code is 201 or 202, but not 200 or anything else. I think this will fix that problem.