Skip to content

Commit 82f7b0c

Browse files
authored
fix: prevent data race when mutating tags (#11200)
1 parent eb81fcf commit 82f7b0c

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

provisionersdk/provisionertags.go

+14-10
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,25 @@ const (
1616
// own their own operations.
1717
// Otherwise, the "owner" tag is always empty.
1818
func MutateTags(userID uuid.UUID, tags map[string]string) map[string]string {
19-
if tags == nil {
20-
tags = map[string]string{}
19+
// We copy the tags here to avoid overwriting the provided map. This can
20+
// cause data races when using dbmem.
21+
cp := map[string]string{}
22+
for k, v := range tags {
23+
cp[k] = v
2124
}
22-
_, ok := tags[TagScope]
25+
26+
_, ok := cp[TagScope]
2327
if !ok {
24-
tags[TagScope] = ScopeOrganization
25-
delete(tags, TagOwner)
28+
cp[TagScope] = ScopeOrganization
29+
delete(cp, TagOwner)
2630
}
27-
switch tags[TagScope] {
31+
switch cp[TagScope] {
2832
case ScopeUser:
29-
tags[TagOwner] = userID.String()
33+
cp[TagOwner] = userID.String()
3034
case ScopeOrganization:
31-
delete(tags, TagOwner)
35+
delete(cp, TagOwner)
3236
default:
33-
tags[TagScope] = ScopeOrganization
37+
cp[TagScope] = ScopeOrganization
3438
}
35-
return tags
39+
return cp
3640
}

0 commit comments

Comments
 (0)