fix: Prevent concurrent map writes to c.UnusedFields (#11311)

This commit is contained in:
Sebastian Spaink 2022-06-16 07:04:45 -05:00 committed by GitHub
parent 412be64088
commit 478edd36c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 4 deletions

View File

@ -16,6 +16,7 @@ import (
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
"github.com/coreos/go-semver/semver" "github.com/coreos/go-semver/semver"
@ -65,9 +66,10 @@ var (
// will be logging to, as well as all the plugins that the user has // will be logging to, as well as all the plugins that the user has
// specified // specified
type Config struct { type Config struct {
toml *toml.Config toml *toml.Config
errs []error // config load errors. errs []error // config load errors.
UnusedFields map[string]bool UnusedFields map[string]bool
unusedFieldsMutex *sync.Mutex
Tags map[string]string Tags map[string]string
InputFilters []string InputFilters []string
@ -91,7 +93,8 @@ type Config struct {
// once the configuration is parsed. // once the configuration is parsed.
func NewConfig() *Config { func NewConfig() *Config {
c := &Config{ c := &Config{
UnusedFields: map[string]bool{}, UnusedFields: map[string]bool{},
unusedFieldsMutex: &sync.Mutex{},
// Agent defaults: // Agent defaults:
Agent: &AgentConfig{ Agent: &AgentConfig{
@ -1846,7 +1849,9 @@ func (c *Config) missingTomlField(_ reflect.Type, key string) error {
// ignore fields that are common to all plugins. // ignore fields that are common to all plugins.
default: default:
c.unusedFieldsMutex.Lock()
c.UnusedFields[key] = true c.UnusedFields[key] = true
c.unusedFieldsMutex.Unlock()
} }
return nil return nil
} }