From e59c47b70a5d88012433e1fb5ed42ffeb175bce6 Mon Sep 17 00:00:00 2001
From: Benjamin Wenger <ben@elixxir.ioo>
Date: Thu, 8 Apr 2021 16:49:09 -0700
Subject: [PATCH] fixes to the health tracker stopper

---
 network/health/tracker.go | 18 +++++++-----------
 stoppable/cleanup.go      |  2 +-
 2 files changed, 8 insertions(+), 12 deletions(-)

diff --git a/network/health/tracker.go b/network/health/tracker.go
index 8e9837165..7779a86b9 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 bc998fc6d..8111abe20 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
-- 
GitLab