Merge pull request #1872 from xiang90/fix_watcher_race

store: fix race in watcher_hub
This commit is contained in:
Xiang Li 2014-12-05 12:12:07 -08:00
commit 6bfa5d409e

View File

@ -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