diff --git a/agent/local/state.go b/agent/local/state.go index 228c879dc9..b44fcad88e 100644 --- a/agent/local/state.go +++ b/agent/local/state.go @@ -1091,6 +1091,12 @@ func (l *State) deleteService(key structs.ServiceID) error { switch { case err == nil || strings.Contains(err.Error(), "Unknown service"): delete(l.services, key) + // service deregister also deletes associated checks + for _, c := range l.checks { + if c.Deleted && c.Check.ServiceID == key.ID { + l.pruneCheck(c.Check.CompoundCheckID()) + } + } l.logger.Printf("[INFO] agent: Deregistered service %q", key.ID) return nil @@ -1125,11 +1131,7 @@ func (l *State) deleteCheck(key structs.CheckID) error { err := l.Delegate.RPC("Catalog.Deregister", &req, &out) switch { case err == nil || strings.Contains(err.Error(), "Unknown check"): - c := l.checks[key] - if c != nil && c.DeferCheck != nil { - c.DeferCheck.Stop() - } - delete(l.checks, key) + l.pruneCheck(key) l.logger.Printf("[INFO] agent: Deregistered check %q", key.String()) return nil @@ -1147,6 +1149,14 @@ func (l *State) deleteCheck(key structs.CheckID) error { } } +func (l *State) pruneCheck(id structs.CheckID) { + c := l.checks[id] + if c != nil && c.DeferCheck != nil { + c.DeferCheck.Stop() + } + delete(l.checks, id) +} + // syncService is used to sync a service to the server func (l *State) syncService(key structs.ServiceID) error { // If the service has associated checks that are out of sync,