From 1a6bff1109366a793abd054807965762b93d5772 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Mon, 26 Apr 2021 11:57:07 -0400 Subject: [PATCH] agent: fix data race in tests caused by grpc log init grpclog.SetLoggerV2 is meant to be called only once before any gRPC requests are received, but each test that uses TestAgent will call NewBaseDeps again. Use a sync.Once to prevent the grpc logging from being re-initialized by each test. This will mean that a test can't use a fake logger to capture logs from the gRPC server. --- agent/setup.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/agent/setup.go b/agent/setup.go index 9a2a2db40..291c26862 100644 --- a/agent/setup.go +++ b/agent/setup.go @@ -66,7 +66,10 @@ func NewBaseDeps(configLoader ConfigLoader, logOut io.Writer) (BaseDeps, error) if err != nil { return d, err } - grpclog.SetLoggerV2(logging.NewGRPCLogger(cfg.Logging.LogLevel, d.Logger)) + + grpcLogInitOnce.Do(func() { + grpclog.SetLoggerV2(logging.NewGRPCLogger(cfg.Logging.LogLevel, d.Logger)) + }) for _, w := range result.Warnings { d.Logger.Warn(w) @@ -123,6 +126,10 @@ func NewBaseDeps(configLoader ConfigLoader, logOut io.Writer) (BaseDeps, error) return d, nil } +// grpcLogInitOnce because the test suite will call NewBaseDeps in many tests and +// causes data races when it is re-initialized. +var grpcLogInitOnce sync.Once + func newConnPool(config *config.RuntimeConfig, logger hclog.Logger, tls *tlsutil.Configurator) *pool.ConnPool { var rpcSrcAddr *net.TCPAddr if !ipaddr.IsAny(config.RPCBindAddr) {