Browse Source

mvcc: don't cancel watcher if stream is already closed

Close() already cancels all the watchers but doesn't bother to clear out
the bookkeeping maps so Cancel() may try to cancel twice.

Fixes #5533
Anthony Romano 9 years ago
parent
commit
f57b4eb46d
1 changed files with 7 additions and 2 deletions
  1. 7 2
      mvcc/watcher.go

+ 7 - 2
mvcc/watcher.go

@@ -117,13 +117,18 @@ func (ws *watchStream) Chan() <-chan WatchResponse {
 }
 }
 
 
 func (ws *watchStream) Cancel(id WatchID) error {
 func (ws *watchStream) Cancel(id WatchID) error {
+	ws.mu.Lock()
 	cancel, ok := ws.cancels[id]
 	cancel, ok := ws.cancels[id]
+	ok = ok && !ws.closed
+	if ok {
+		delete(ws.cancels, id)
+		delete(ws.watchers, id)
+	}
+	ws.mu.Unlock()
 	if !ok {
 	if !ok {
 		return ErrWatcherNotExist
 		return ErrWatcherNotExist
 	}
 	}
 	cancel()
 	cancel()
-	delete(ws.cancels, id)
-	delete(ws.watchers, id)
 	return nil
 	return nil
 }
 }