Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

etcd/client: ensure response body is always closed #620

Merged
merged 1 commit into from
Jul 8, 2014

Conversation

jonboulle
Copy link
Contributor

No description provided.

@jonboulle
Copy link
Contributor Author

@unihorn how about this?

@yichengq
Copy link
Contributor

yichengq commented Jul 5, 2014

I don't think it fixes the bug because resp could still be unassigned when exit the function.
What about close the resp.Body in the goroutine that runs RoundTrip?

@jonboulle
Copy link
Contributor Author

Ah, now I see what you mean.

@jonboulle
Copy link
Contributor Author

@unihorn take a look now?

respchan := make(chan struct {
r *http.Response
b []byte
e error
Copy link
Contributor

Choose a reason for hiding this comment

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

If we're going to communicate an error through this struct, maybe we can drop the other channel (errchan chan error)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call, updated.

@yichengq
Copy link
Contributor

yichengq commented Jul 7, 2014

@jonboulle This approach is good to me. :)

case err = <-errchan:
return nil, nil, err
case res := <-respchan:
return res.r, res.b, res.e
Copy link
Contributor

Choose a reason for hiding this comment

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

How about we change this line to:

resp, body, err = res.r, res.b, res.e

Then we put a bare return at the bottom of the function

@jonboulle
Copy link
Contributor Author

@bcwaldon updated with tests and complete coverage for requestHTTP. With the caveat that it's simply not possible to test that resp.Body is closed when it's masked; need to eyeball the coverage for that :/

@@ -22,6 +22,13 @@ type Client interface {
Wait(Action, <-chan bool) (*Result, error)
}

// transport wraps http.Transport to provide an interface which can
// be substituted for testing
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe making a note about needing this because http.RoundTripper interface does not require the CancelRequest method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hah, I had that originally. Will add.

@bcwaldon
Copy link
Contributor

bcwaldon commented Jul 8, 2014

@jonboulle lgtm - thanks for slogging through this

@jonboulle
Copy link
Contributor Author

@unihorn any final thoughts?

@bcwaldon
Copy link
Contributor

bcwaldon commented Jul 8, 2014

@jonboulle Some squashing would be appropriate before merge.

@jonboulle
Copy link
Contributor Author

Sure thing, guy.
On Jul 7, 2014 5:28 PM, "Brian Waldon" notifications@github.com wrote:

@jonboulle https://github.com/jonboulle Some squashing would be
appropriate before merge.


Reply to this email directly or view it on GitHub
#620 (comment).

@jonboulle jonboulle closed this Jul 8, 2014
@jonboulle jonboulle reopened this Jul 8, 2014
There was a race condition when cancelling a request since the actual
RoundTrip is performed in a goroutine, and the cancel is done
asynchronously. This reworks the goroutine to ensure that the Body is
closed whenever a non-nil response is retrieved from the RoundTrip. It
also introduces a transport interface to support testing of requestHTTP
and adds complete coverage of the function.
jonboulle added a commit that referenced this pull request Jul 8, 2014
etcd/client: ensure response body is always closed
@jonboulle jonboulle merged commit 8d26d50 into coreos:master Jul 8, 2014
@bcwaldon bcwaldon added this to the v0.5.2 milestone Jul 8, 2014
@bcwaldon bcwaldon mentioned this pull request Jul 8, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants