-
Notifications
You must be signed in to change notification settings - Fork 419
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
internal/datastreams: fix memory leak #2266
Conversation
BenchmarksBenchmark execution time: 2023-11-06 19:58:19 Comparing candidate commit ef405b7 in PR branch Found 0 performance improvements and 0 performance regressions! Performance is the same for 41 metrics, 0 unstable metrics. |
This PR is stale because it has been open 20 days with no activity. Remove stale label or comment or this will be closed in 10 days. |
Any chance on this getting merged? Also, is data-streams-go out of alpha now that it has been moved into the dd-trace-go package? It isn't clear to me that this feature is prod ready. |
internal/datastreams/transport.go
Outdated
msg := make([]byte, 1000) | ||
n, _ := resp.Body.Read(msg) | ||
resp.Body.Close() | ||
if code := resp.StatusCode; code >= 400 { | ||
// error, check the body for context information and | ||
// return a nice error. |
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.
msg := make([]byte, 1000) | |
n, _ := resp.Body.Read(msg) | |
resp.Body.Close() | |
if code := resp.StatusCode; code >= 400 { | |
// error, check the body for context information and | |
// return a nice error. | |
defer resp.Body.Close() | |
if code := resp.StatusCode; code >= 400 { | |
// error, check the body for context information and | |
// return a nice error. | |
msg := make([]byte, 1000) | |
n, _ := resp.Body.Read(msg) |
If there's a chance you're going to need to throw away n
, then don't read it until you know you need to. I suggest just deferring the close and then reading the body if and when you really need it
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.
So, ideally I would like to do that, but it looks like we need to read the bytes for the structure to be re-used:
// Body which the user is expected to close. If the Body is not both
// read to EOF and closed, the Client's underlying RoundTripper
// (typically Transport) may not be able to re-use a persistent TCP
// connection to the server for a subsequent "keep-alive" request.
From here:
https://github.com/golang/go/blob/master/src/net/http/client.go#L562
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.
This doesn't guarantee we read to EOF, just that we read <=1000 bytes.
Let's just use the standard io.Copy
and io.Discard
like we do in the agent: https://github.com/DataDog/datadog-agent/blob/main/pkg/trace/api/api.go#L467
ideally with a defer along with the Close.
Then we can only allocate a buffer for the message and read into it if we actually need it.
internal/datastreams/transport.go
Outdated
msg := make([]byte, 1000) | ||
n, _ := resp.Body.Read(msg) | ||
resp.Body.Close() | ||
if code := resp.StatusCode; code >= 400 { | ||
// error, check the body for context information and | ||
// return a nice error. |
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.
This doesn't guarantee we read to EOF, just that we read <=1000 bytes.
Let's just use the standard io.Copy
and io.Discard
like we do in the agent: https://github.com/DataDog/datadog-agent/blob/main/pkg/trace/api/api.go#L467
ideally with a defer along with the Close.
Then we can only allocate a buffer for the message and read into it if we actually need it.
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.
Looks good.
What does this PR do?
Context here: https://stackoverflow.com/questions/18598780/is-resp-body-close-necessary-if-we-dont-read-anything-from-the-body/18601625#18601625
Motivation
Memory leak.
Reviewer's Checklist
For Datadog employees:
@DataDog/security-design-and-guidance
.Unsure? Have a question? Request a review!