-
Notifications
You must be signed in to change notification settings - Fork 13
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
Improve handling of errors from body.Close() #526
Conversation
PR Summary
These changes ensure a higher degree of reliability and seamless operation of our application. Review of individual files for understanding the changes in detail is encouraged. |
59633df
to
9e7ae55
Compare
e2e/tests/proxy_test.go
Outdated
"http://wronghost/", | ||
"http://differenthost", |
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.
Strange host names
internal/martian/proxy.go
Outdated
@@ -728,7 +729,7 @@ func (p *Proxy) handle(ctx *Context, conn net.Conn, brw *bufio.ReadWriter) error | |||
} | |||
return errClose | |||
} | |||
defer req.Body.Close() | |||
defer closeBody(req.Context(), req.Body, &retErr) |
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.
Creative but wrong.
Use defer func...
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 wrong?
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.
Maybe it's not wrong but passing ptr to the return value is a form of hack.
Other thing is that we should return body close error only if there are no other errors.
IMO there should be defer req.Body.Close()
and a manual body close by the end the way you handle files for instance.
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.
Got it.
internal/martian/handler.go
Outdated
@@ -164,7 +164,7 @@ func (p proxyHandler) handleConnectRequest(ctx *Context, rw http.ResponseWriter, | |||
res = p.errorResponse(req, cerr) | |||
p.warning(res.Header, cerr) | |||
} | |||
defer res.Body.Close() | |||
defer mustCloseBody(res.Request.Context(), res.Body) |
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.
I believe handler does not need that because the server closes the body close and manages the connections i.e. it's out of our control.
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.
Of course!
9e7ae55
to
f977720
Compare
e2e/tests/proxy_test.go
Outdated
"http://wronghost2", | ||
} { | ||
res := c.GET(h, func(req *http.Request) { | ||
var body bytes.Buffer |
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.
What if you try to use io.Pipe for a body or a limit reader of 10MB reading zeros?
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.
Added io.Pipe with zeros to be read.
e2e/tests/proxy_test.go
Outdated
"http://wronghost1", | ||
"http://wronghost2", |
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.
Host does not matter, you could as well have a plain loop.
In order to reuse HTTP connection the body must be drained.
f977720
to
48000e7
Compare
Drop the last commit it does not belong here. Also it only logs in one place. |
Otherwise lgtm. |
48000e7
to
6080252
Compare
No description provided.