Browse Source

lib/watchaggregator: Prevent race on config update (#4938)

Simon Frei 7 years ago
parent
commit
07bf24a3b4
1 changed files with 5 additions and 1 deletions
  1. 5 1
      lib/watchaggregator/aggregator.go

+ 5 - 1
lib/watchaggregator/aggregator.go

@@ -98,6 +98,9 @@ func (dir *eventDir) eventType() fs.EventType {
 }
 
 type aggregator struct {
+	// folderID never changes and is accessed in CommitConfiguration, which
+	// asynchronously updates folderCfg -> can't use folderCfg.ID (racy)
+	folderID        string
 	folderCfg       config.FolderConfiguration
 	folderCfgUpdate chan config.FolderConfiguration
 	// Time after which an event is scheduled for scanning when no modifications occur.
@@ -112,6 +115,7 @@ type aggregator struct {
 
 func newAggregator(folderCfg config.FolderConfiguration, ctx context.Context) *aggregator {
 	a := &aggregator{
+		folderID:              folderCfg.ID,
 		folderCfgUpdate:       make(chan config.FolderConfiguration),
 		notifyTimerNeedsReset: false,
 		notifyTimerResetChan:  make(chan time.Duration),
@@ -390,7 +394,7 @@ func (a *aggregator) VerifyConfiguration(from, to config.Configuration) error {
 
 func (a *aggregator) CommitConfiguration(from, to config.Configuration) bool {
 	for _, folderCfg := range to.Folders {
-		if folderCfg.ID == a.folderCfg.ID {
+		if folderCfg.ID == a.folderID {
 			select {
 			case a.folderCfgUpdate <- folderCfg:
 			case <-a.ctx.Done():