-
Notifications
You must be signed in to change notification settings - Fork 34
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
Metrics concurrency bug #318
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bmoylan
reviewed
Aug 16, 2023
metrics/context.go
Outdated
@@ -52,8 +52,11 @@ func AddTags(ctx context.Context, tags ...Tag) context.Context { | |||
if !ok || container == nil { | |||
container = &tagsContainer{} | |||
} | |||
newTags := make(Tags, len(container.Tags), len(container.Tags)+len(tags)) |
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.
Let's use copy for both input slices:
newTags := make(Tags, len(container.Tags)+len(tags))
copy(newTags, container.Tags)
copy(newTags[len(container.Tags):], tags)
bmoylan
approved these changes
Aug 16, 2023
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.
Thanks for the root-causing and contribution!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There is a race condition under certain situations when using the FromContext function call and then updating metrics.
Each call to FromContext, when a tagsContainer is present, will return a childRegistry with the tags field set to the same location in memory.
In the case where these tag slices have a capacity > length and concurrent calls to append are made on the slice (Such as two processes calling Gauge at the same time), the tags slice will be populated with the first caller's new tags, and then overwritten by the second caller. If this happens before the tags are copied in registerMetric, the first caller will have the name of the first caller and the tags of the second caller.
In this test case, since we add tags to the context one by one for a total of 3. The tags slice will have a length of 3 and a capacity of 4. Because of that we see the behavior described above.
Gauge is called with prefix1 and tag {"name": prefix1}. The append call does not create a new array since the memory is allocated to fit this 1 tag. Then, before the tags are copied, Gauge is called with prefix2 and tag {"name":prefix2}. The amend in this case overwrites the {"name":prefix1} since this is done on the same slice, and then by the time the prefix1 tags are being copied the contents contain {"name":prefix2} and not {"name":prefix1}.
This change is![Reviewable](https://camo.githubusercontent.com/23b05f5fb48215c989e92cc44cf6512512d083132bd3daf689867c8d9d386888/68747470733a2f2f72657669657761626c652e696f2f7265766965775f627574746f6e2e737667)