From 3dab0249b4a55c8afcbd703535101dadac6fac35 Mon Sep 17 00:00:00 2001 From: Tim Gross Date: Thu, 8 Sep 2022 10:41:15 -0400 Subject: [PATCH] test: fix concurrent map access in `TestStatsFetcher` (#14496) The map of in-flight RPCs gets cleared by a goroutine in the test without first locking it to make sure that it's not being accessed concurrently by the stats fetcher itself. This can cause a panic in tests. --- nomad/stats_fetcher_test.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/nomad/stats_fetcher_test.go b/nomad/stats_fetcher_test.go index 442ff4477..74a5d497e 100644 --- a/nomad/stats_fetcher_test.go +++ b/nomad/stats_fetcher_test.go @@ -71,7 +71,11 @@ func TestStatsFetcher(t *testing.T) { // from it. func() { s1.statsFetcher.inflight[raft.ServerID(s3.config.NodeID)] = struct{}{} - defer delete(s1.statsFetcher.inflight, raft.ServerID(s3.config.NodeID)) + defer func() { + s1.statsFetcher.inflightLock.Lock() + delete(s1.statsFetcher.inflight, raft.ServerID(s3.config.NodeID)) + s1.statsFetcher.inflightLock.Unlock() + }() ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel()