Skip to content

Commit

Permalink
Fix data race accessing Request.URL (#1472)
Browse files Browse the repository at this point in the history
If an HTTP client is instrumented with apmhttp,
then the http.Request.URL is recorded for including
in span context. The caller of the HTTP client request
may mutate the URL after the response is ready, so we
must clone the URL while recording it to prevent data
races.
  • Loading branch information
axw authored Jun 21, 2023
1 parent d41a211 commit a402c4b
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion spancontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,15 @@ func (c *SpanContext) SetHTTPRequest(req *http.Request) {
if req.URL == nil {
return
}
c.http.URL = req.URL

// Clone the URL, since callers of http.RoundTrip may mutate the
// Request after the response body is closed.
clonedURL := *req.URL
if req.URL.User != nil {
clonedURLUser := *req.URL.User
clonedURL.User = &clonedURLUser
}
c.http.URL = &clonedURL
c.model.HTTP = &c.http

addr, port := apmhttputil.DestinationAddr(req)
Expand Down

0 comments on commit a402c4b

Please sign in to comment.