diff --git a/agent/agent_endpoint.go b/agent/agent_endpoint.go index 96c312a2a..7eead01c5 100644 --- a/agent/agent_endpoint.go +++ b/agent/agent_endpoint.go @@ -170,10 +170,11 @@ func (s *HTTPServer) AgentChecks(resp http.ResponseWriter, req *http.Request) (i } // Use empty list instead of nil - // checks needs to be a deep copy for this not be racy - for _, c := range checks { + for id, c := range checks { if c.ServiceTags == nil { - c.ServiceTags = make([]string, 0) + clone := *c + clone.ServiceTags = make([]string, 0) + checks[id] = &clone } } diff --git a/agent/catalog_endpoint.go b/agent/catalog_endpoint.go index d1e6bfdd4..2eae35a2a 100644 --- a/agent/catalog_endpoint.go +++ b/agent/catalog_endpoint.go @@ -201,9 +201,11 @@ func (s *HTTPServer) CatalogServiceNodes(resp http.ResponseWriter, req *http.Req if out.ServiceNodes == nil { out.ServiceNodes = make(structs.ServiceNodes, 0) } - for _, s := range out.ServiceNodes { + for i, s := range out.ServiceNodes { if s.ServiceTags == nil { - s.ServiceTags = make([]string, 0) + clone := *s + clone.ServiceTags = make([]string, 0) + out.ServiceNodes[i] = &clone } } metrics.IncrCounterWithLabels([]string{"client", "api", "success", "catalog_service_nodes"}, 1, diff --git a/agent/health_endpoint.go b/agent/health_endpoint.go index 59ea6c02f..e3cf4539a 100644 --- a/agent/health_endpoint.go +++ b/agent/health_endpoint.go @@ -42,9 +42,11 @@ func (s *HTTPServer) HealthChecksInState(resp http.ResponseWriter, req *http.Req if out.HealthChecks == nil { out.HealthChecks = make(structs.HealthChecks, 0) } - for _, c := range out.HealthChecks { + for i, c := range out.HealthChecks { if c.ServiceTags == nil { - c.ServiceTags = make([]string, 0) + clone := *c + clone.ServiceTags = make([]string, 0) + out.HealthChecks[i] = &clone } } return out.HealthChecks, nil @@ -80,9 +82,11 @@ func (s *HTTPServer) HealthNodeChecks(resp http.ResponseWriter, req *http.Reques if out.HealthChecks == nil { out.HealthChecks = make(structs.HealthChecks, 0) } - for _, c := range out.HealthChecks { + for i, c := range out.HealthChecks { if c.ServiceTags == nil { - c.ServiceTags = make([]string, 0) + clone := *c + clone.ServiceTags = make([]string, 0) + out.HealthChecks[i] = &clone } } return out.HealthChecks, nil @@ -120,9 +124,11 @@ func (s *HTTPServer) HealthServiceChecks(resp http.ResponseWriter, req *http.Req if out.HealthChecks == nil { out.HealthChecks = make(structs.HealthChecks, 0) } - for _, c := range out.HealthChecks { + for i, c := range out.HealthChecks { if c.ServiceTags == nil { - c.ServiceTags = make([]string, 0) + clone := *c + clone.ServiceTags = make([]string, 0) + out.HealthChecks[i] = &clone } } return out.HealthChecks, nil @@ -194,19 +200,20 @@ func (s *HTTPServer) HealthServiceNodes(resp http.ResponseWriter, req *http.Requ out.Nodes = make(structs.CheckServiceNodes, 0) } for i := range out.Nodes { - // TODO (slackpad) It's lame that this isn't a slice of pointers - // but it's not a well-scoped change to fix this. We should - // change this at the next opportunity. if out.Nodes[i].Checks == nil { out.Nodes[i].Checks = make(structs.HealthChecks, 0) } - for _, c := range out.Nodes[i].Checks { + for j, c := range out.Nodes[i].Checks { if c.ServiceTags == nil { - c.ServiceTags = make([]string, 0) + clone := *c + clone.ServiceTags = make([]string, 0) + out.Nodes[i].Checks[j] = &clone } } if out.Nodes[i].Service != nil && out.Nodes[i].Service.Tags == nil { - out.Nodes[i].Service.Tags = make([]string, 0) + clone := *out.Nodes[i].Service + clone.Tags = make([]string, 0) + out.Nodes[i].Service = &clone } } return out.Nodes, nil