Skip to content

Commit d321bd8

Browse files
dkorunicmjuraga
authored andcommitted
BUG: Fix data race between configuration Save() and Load()
ClusterSync Monitor in monitorBootstrapKey()->issueJoinRequest() will issue a Save() that races (unsafe read) with configuration Load() (unsafe write). Patch also moves mutex to Configuration and removes unneeded global saveMutex.
1 parent 939902c commit d321bd8

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

configuration/configuration.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,11 @@ type Configuration struct {
149149
Status AtomicString `yaml:"status,omitempty"`
150150
Cmdline AtomicString `yaml:"-"`
151151
MapSync *MapSync `yaml:"-"`
152+
mutex sync.Mutex
152153
}
153154

154155
var (
155156
cfgInitOnce sync.Once
156-
saveMutex sync.Mutex
157157
)
158158

159159
// Get returns pointer to configuration
@@ -195,6 +195,9 @@ func (c *Configuration) UnSubscribeAll() {
195195
}
196196

197197
func (c *Configuration) Load() error {
198+
c.mutex.Lock()
199+
defer c.mutex.Unlock()
200+
198201
var err error
199202
if c.HAProxy.DataplaneConfig == "" {
200203
c.storage = &StorageDummy{}
@@ -259,8 +262,9 @@ func (c *Configuration) LoadRuntimeVars(swaggerJSON json.RawMessage, host string
259262
}
260263

261264
func (c *Configuration) Save() error {
262-
saveMutex.Lock()
263-
defer saveMutex.Unlock()
265+
c.mutex.Lock()
266+
defer c.mutex.Unlock()
267+
264268
copyConfigurationToStorage(c)
265269
if len(c.ServiceDiscovery.Consuls) == 0 {
266270
cfg := c.storage.Get()

0 commit comments

Comments
 (0)