Merge pull request #8346 from hashicorp/dnephin/fix-race-in-agent-checks

checks: wait for goroutine to complete (fix go-test-race failures)
This commit is contained in:
Daniel Nephin 2020-07-21 11:57:57 -04:00 committed by GitHub
commit 308815b6bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 3 deletions

View File

@ -32,8 +32,7 @@ type CheckAlias struct {
stop bool
stopCh chan struct{}
stopLock sync.Mutex
stopWg sync.WaitGroup
stopWg sync.WaitGroup
structs.EnterpriseMeta
}
@ -55,6 +54,7 @@ func (c *CheckAlias) Start() {
defer c.stopLock.Unlock()
c.stop = false
c.stopCh = make(chan struct{})
c.stopWg.Add(1)
go c.run(c.stopCh)
}
@ -76,7 +76,6 @@ func (c *CheckAlias) Stop() {
// run is invoked in a goroutine until Stop() is called.
func (c *CheckAlias) run(stopCh chan struct{}) {
c.stopWg.Add(1)
defer c.stopWg.Done()
// If we have a specific node set, then use a blocking query

View File

@ -348,6 +348,7 @@ type CheckHTTP struct {
stop bool
stopCh chan struct{}
stopLock sync.Mutex
stopWg sync.WaitGroup
// Set if checks are exposed through Connect proxies
// If set, this is the target of check()
@ -399,6 +400,7 @@ func (c *CheckHTTP) Start() {
c.stop = false
c.stopCh = make(chan struct{})
c.stopWg.Add(1)
go c.run()
}
@ -410,10 +412,14 @@ func (c *CheckHTTP) Stop() {
c.stop = true
close(c.stopCh)
}
// Wait for the c.run() goroutine to complete before returning.
c.stopWg.Wait()
}
// run is invoked by a goroutine to run until Stop() is called
func (c *CheckHTTP) run() {
defer c.stopWg.Done()
// Get the randomized initial pause time
initialPauseTime := lib.RandomStagger(c.Interval)
next := time.After(initialPauseTime)