Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Commit

Permalink
Fix data race (hashicorp#5029)
Browse files Browse the repository at this point in the history
  • Loading branch information
dawxy authored and mkeeler committed Jan 8, 2019
1 parent 0670272 commit 238d430
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion watch/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions watch/plan.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package watch

import (
"context"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 238d430

Please sign in to comment.