From 793cb095b03ef70e05d87e76932829ba1524556e Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Fri, 5 Dec 2014 11:55:01 -0800 Subject: [PATCH] store: fix race in watcher_hub Get the lock before modifing the global objects in the hub. --- store/watcher_hub.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/store/watcher_hub.go b/store/watcher_hub.go index 0284e66bd..89926563f 100644 --- a/store/watcher_hub.go +++ b/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