Skip to content

Commit

Permalink
[client] Fix race condition in Metadata.Get (#9595)
Browse files Browse the repository at this point in the history
**Description:** 
Fixes a bug where `Get` was not thread safe.

**Link to tracking Issue:** <Issue number if applicable>
Closes
#9594

**Testing:** <Describe what testing was performed and which tests were
added.>
Tested locally
  • Loading branch information
TylerHelmuth committed Feb 21, 2024
1 parent 4430ca9 commit ae90430
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
25 changes: 25 additions & 0 deletions .chloggen/make-metadata-thread-safe.yaml
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: client

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Make `Metadata.Get` thread safe

# One or more tracking issues or pull requests related to the change
issues: [9595]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
26 changes: 11 additions & 15 deletions client/client.go
Expand Up @@ -141,28 +141,24 @@ func FromContext(ctx context.Context) Info {

// NewMetadata creates a new Metadata object to use in Info. md is used as-is.
func NewMetadata(md map[string][]string) Metadata {
c := make(map[string][]string, len(md))
for k, v := range md {
c[strings.ToLower(k)] = v
}
return Metadata{
data: md,
data: c,
}
}

// Get gets the value of the key from metadata, returning a copy.
func (m Metadata) Get(key string) []string {
vals := m.data[key]
if len(m.data) == 0 {
return nil
}

vals := m.data[strings.ToLower(key)]
if len(vals) == 0 {
// we didn't find the key, but perhaps it just has different cases?
for k, v := range m.data {
if strings.EqualFold(key, k) {
vals = v
// we optimize for the next lookup
m.data[key] = v
}
}

// if it's still not found, it's really not here
if len(vals) == 0 {
return nil
}
return nil
}

ret := make([]string, len(vals))
Expand Down
10 changes: 8 additions & 2 deletions client/client_test.go
Expand Up @@ -77,10 +77,11 @@ func TestFromContext(t *testing.T) {
}

func TestMetadata(t *testing.T) {
source := map[string][]string{"test-key": {"test-val"}}
source := map[string][]string{"test-key": {"test-val"}, "TEST-KEY-2": {"test-val"}}
md := NewMetadata(source)
assert.Equal(t, []string{"test-val"}, md.Get("test-key"))
assert.Equal(t, []string{"test-val"}, md.Get("test-KEY")) // case insensitive lookup
assert.Equal(t, []string{"test-val"}, md.Get("test-KEY")) // case insensitive lookup
assert.Equal(t, []string{"test-val"}, md.Get("test-key-2")) // case insensitive lookup

// test if copy. In regular use, source cannot change
val := md.Get("test-key")
Expand All @@ -89,3 +90,8 @@ func TestMetadata(t *testing.T) {

assert.Empty(t, md.Get("non-existent-key"))
}

func TestUninstantiatedMetadata(t *testing.T) {
i := Info{}
assert.Empty(t, i.Metadata.Get("test"))
}

0 comments on commit ae90430

Please sign in to comment.