diff --git a/network/health/tracker.go b/network/health/tracker.go index 8e9837165a33982f6dcf7387e7c42388b4c276bd..7779a86b96502c2d34a17690e26c79c46bcdad8b 100644 --- a/network/health/tracker.go +++ b/network/health/tracker.go @@ -103,7 +103,6 @@ func (t *Tracker) setHealth(h bool) { func (t *Tracker) Start() (stoppable.Stoppable, error) { t.mux.Lock() - defer t.mux.Unlock() if t.running { return nil, errors.New("cannot start Health tracker threads, " + "they are already running") @@ -111,20 +110,13 @@ func (t *Tracker) Start() (stoppable.Stoppable, error) { t.running = true t.isHealthy = false + t.mux.Unlock() stop := stoppable.NewSingle("Health Tracker") - stopCleanup := stoppable.NewCleanup(stop, func(duration time.Duration) error { - t.mux.Lock() - defer t.mux.Unlock() - t.isHealthy = false - t.transmit(false) - t.running = false - return nil - }) go t.start(stop.Quit()) - return stopCleanup, nil + return stop, nil } // Long-running thread used to monitor and report on network health @@ -135,7 +127,11 @@ func (t *Tracker) start(quitCh <-chan struct{}) { var heartbeat network.Heartbeat select { case <-quitCh: - // Handle thread kill + t.mux.Lock() + t.isHealthy = false + t.running = false + t.mux.Unlock() + t.transmit(false) break case heartbeat = <-t.heartbeat: if healthy(heartbeat) { diff --git a/stoppable/cleanup.go b/stoppable/cleanup.go index bc998fc6d6b011044e7e57d78d634ef7eec4c4af..8111abe206492d4e22cfaffe31195acab58b77c5 100644 --- a/stoppable/cleanup.go +++ b/stoppable/cleanup.go @@ -9,11 +9,11 @@ package stoppable import ( "github.com/pkg/errors" + jww "github.com/spf13/jwalterweatherman" "gitlab.com/xx_network/primitives/netTime" "sync" "sync/atomic" "time" - jww "github.com/spf13/jwalterweatherman" ) // Cleanup wraps any stoppable and runs a callback after to stop for cleanup