Skip to content

Commit

Permalink
fix data race (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
imroc committed Aug 26, 2022
1 parent fe49f38 commit 5a3fb09
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1223,8 +1223,11 @@ func (c *Client) roundTrip(r *Request) (resp *Response, err error) {
if r.trace == nil && r.client.trace {
r.trace = &clientTrace{}
}

ctx := r.ctx

if r.trace != nil {
r.ctx = r.trace.createContext(r.Context())
ctx = r.trace.createContext(r.Context())
}

// setup url and host
Expand Down Expand Up @@ -1260,7 +1263,6 @@ func (c *Client) roundTrip(r *Request) (resp *Response, err error) {
for _, cookie := range r.Cookies {
req.AddCookie(cookie)
}
ctx := r.ctx
if r.isSaveResponse && r.downloadCallback != nil {
var wrap wrapResponseBodyFunc = func(rc io.ReadCloser) io.ReadCloser {
return &callbackReader{
Expand All @@ -1275,10 +1277,7 @@ func (c *Client) roundTrip(r *Request) (resp *Response, err error) {
interval: r.downloadCallbackInterval,
}
}
if ctx == nil {
ctx = context.Background()
}
ctx = context.WithValue(ctx, wrapResponseBodyKey, wrap)
ctx = context.WithValue(r.Context(), wrapResponseBodyKey, wrap)
}
if ctx != nil {
req = req.WithContext(ctx)
Expand Down Expand Up @@ -1371,7 +1370,9 @@ func (c *Client) do(r *Request) (resp *Response, err error) {
if r.dumpBuffer != nil {
r.dumpBuffer.Reset()
}
r.trace = nil
if r.trace != nil {
r.trace = &clientTrace{}
}
resp.body = nil
resp.result = nil
resp.error = nil
Expand Down

0 comments on commit 5a3fb09

Please sign in to comment.