diff --git a/remote.go b/remote.go index 08f00a8d9..080acf816 100644 --- a/remote.go +++ b/remote.go @@ -243,11 +243,13 @@ func (v *Viper) watchKeyValueConfigOnChannel() error { go func(rc <-chan *RemoteResponse) { for { b := <-rc + v.viperLock.Lock() reader := bytes.NewReader(b.Value) err := v.unmarshalReader(reader, v.kvstore) if err != nil { v.logger.Error(fmt.Errorf("failed to unmarshal remote config: %w", err).Error()) } + v.viperLock.Unlock() } }(respc) return nil diff --git a/viper.go b/viper.go index 2d5158cbd..62eb269b1 100644 --- a/viper.go +++ b/viper.go @@ -152,6 +152,8 @@ type Viper struct { experimentalFinder bool experimentalBindStruct bool + + viperLock sync.RWMutex } // New returns an initialized Viper instance. @@ -713,6 +715,9 @@ func Get(key string) any { return v.Get(key) } // // Get returns an interface. For a specific value use one of the Get____ methods. func (v *Viper) Get(key string) any { + v.viperLock.RLock() + defer v.viperLock.RUnlock() + lcaseKey := strings.ToLower(key) val := v.find(lcaseKey, true) if val == nil {