Fix data race (#5029)

Fix #4357
This commit is contained in:
dawxy 2019-01-09 00:43:14 +08:00 committed by Matt Keeler
parent 067027230b
commit 238d430275
2 changed files with 13 additions and 1 deletions

View File

@ -335,7 +335,7 @@ func agentServiceWatch(params map[string]interface{}) (WatcherFunc, error) {
func makeQueryOptionsWithContext(p *Plan, stale bool) consulapi.QueryOptions {
ctx, cancel := context.WithCancel(context.Background())
p.cancelFunc = cancel
p.setCancelFunc(cancel)
opts := consulapi.QueryOptions{AllowStale: stale}
switch param := p.lastParamVal.(type) {
case WaitIndexVal:

View File

@ -1,6 +1,7 @@
package watch
import (
"context"
"fmt"
"log"
"os"
@ -148,6 +149,17 @@ func (p *Plan) shouldStop() bool {
}
}
func (p *Plan) setCancelFunc(cancel context.CancelFunc) {
p.stopLock.Lock()
defer p.stopLock.Unlock()
if p.shouldStop() {
// The watch is stopped and execute the new cancel func to stop watchFactory
cancel()
return
}
p.cancelFunc = cancel
}
func (p *Plan) IsStopped() bool {
p.stopLock.Lock()
defer p.stopLock.Unlock()