Browse Source

Merge pull request #1872 from xiang90/fix_watcher_race

store: fix race in watcher_hub
Xiang Li 11 years ago
parent
commit
6bfa5d409e
1 changed files with 3 additions and 4 deletions
  1. 3 4
      store/watcher_hub.go

+ 3 - 4
store/watcher_hub.go

@@ -33,7 +33,7 @@ import (
 // event happens between the end of the first watch command and the start
 // of the second command.
 type watcherHub struct {
-	mutex        sync.Mutex // protect the hash map
+	mutex        sync.Mutex
 	watchers     map[string]*list.List
 	count        int64 // current number of watchers.
 	EventHistory *EventHistory
@@ -71,6 +71,8 @@ func (wh *watcherHub) watch(key string, recursive, stream bool, index, storeInde
 		hub:        wh,
 	}
 
+	wh.mutex.Lock()
+	defer wh.mutex.Unlock()
 	// If the event exists in the known history, append the EtcdIndex and return immediately
 	if event != nil {
 		event.EtcdIndex = storeIndex
@@ -78,9 +80,6 @@ func (wh *watcherHub) watch(key string, recursive, stream bool, index, storeInde
 		return w, nil
 	}
 
-	wh.mutex.Lock()
-	defer wh.mutex.Unlock()
-
 	l, ok := wh.watchers[key]
 
 	var elem *list.Element