Skip to content

Commit

Permalink
Clean up leaked goroutine that waits for sigterm in healthchecker
Browse files Browse the repository at this point in the history
  • Loading branch information
iain-macdonald committed Oct 11, 2024
1 parent c097300 commit 7a4d125
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
1 change: 1 addition & 0 deletions server/testutil/testenv/testenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ func GetTestEnv(t testing.TB) *real_environment.RealEnv {
}

healthChecker := healthcheck.NewHealthChecker("test")
t.Cleanup(healthChecker.Shutdown)
te := real_environment.NewRealEnv(healthChecker)
c, err := memory_cache.NewMemoryCache(1000 * 1000 * 1000 /* 1GB */)
if err != nil {
Expand Down
8 changes: 5 additions & 3 deletions server/util/healthcheck/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type serviceStatus struct {
type HealthChecker struct {
done chan bool
quit chan struct{}
sigTerm chan os.Signal
checkersMu sync.Mutex
checkers map[string]interfaces.Checker
lastStatus []*serviceStatus
Expand All @@ -60,18 +61,18 @@ func NewHealthChecker(serverType string) *HealthChecker {
serverType: serverType,
done: make(chan bool),
quit: make(chan struct{}),
sigTerm: make(chan os.Signal, 1),
shutdownFuncs: make([]interfaces.CheckerFunc, 0),
readyToServe: true,
checkersMu: sync.Mutex{},
checkers: make(map[string]interfaces.Checker, 0),
lastStatus: make([]*serviceStatus, 0),
}
sigTerm := make(chan os.Signal, 1)
go func() {
<-sigTerm
<-hc.sigTerm
hc.Shutdown()
}()
signal.Notify(sigTerm, os.Interrupt, syscall.SIGTERM)
signal.Notify(hc.sigTerm, os.Interrupt, syscall.SIGTERM)
go hc.handleShutdownFuncs()
go func() {
ticker := time.NewTicker(healthCheckPeriod)
Expand Down Expand Up @@ -169,6 +170,7 @@ func (h *HealthChecker) WaitForGracefulShutdown() {
func (h *HealthChecker) Shutdown() {
h.shutdownOnce.Do(func() {
close(h.quit)
close(h.sigTerm)
})
}

Expand Down

0 comments on commit 7a4d125

Please sign in to comment.