fix: prevent data race when mutating tags (#11200)

This commit is contained in:
Jon Ayers 2023-12-14 02:56:59 -06:00 committed by GitHub
parent eb81fcf1e1
commit 82f7b0cef4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 10 deletions

View File

@ -16,21 +16,25 @@ const (
// own their own operations.
// Otherwise, the "owner" tag is always empty.
func MutateTags(userID uuid.UUID, tags map[string]string) map[string]string {
if tags == nil {
tags = map[string]string{}
// We copy the tags here to avoid overwriting the provided map. This can
// cause data races when using dbmem.
cp := map[string]string{}
for k, v := range tags {
cp[k] = v
}
_, ok := tags[TagScope]
_, ok := cp[TagScope]
if !ok {
tags[TagScope] = ScopeOrganization
delete(tags, TagOwner)
cp[TagScope] = ScopeOrganization
delete(cp, TagOwner)
}
switch tags[TagScope] {
switch cp[TagScope] {
case ScopeUser:
tags[TagOwner] = userID.String()
cp[TagOwner] = userID.String()
case ScopeOrganization:
delete(tags, TagOwner)
delete(cp, TagOwner)
default:
tags[TagScope] = ScopeOrganization
cp[TagScope] = ScopeOrganization
}
return tags
return cp
}