From bbf1a116f6cbed04715f00d63e1ae5671f67e0a3 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 23 Dec 2020 12:59:05 -0500 Subject: [PATCH] xds: Fix data race TestEnvoy.Close used e.stream.recvCh == nil to indicate the channel had already been closed, so that TestEnvoy.Close can be called multiple times. The recvCh was not protected by a lock, so setting it to nil caused a data race with any goroutine trying to read from the channel. Instead set the stream to nil. The stream is guarded by a lock, so it does not race. This change allows us to test the agent/xds package using -race. --- .circleci/config.yml | 2 +- agent/xds/testing.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index a178514d5..d095bd366 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -237,7 +237,7 @@ jobs: command: | mkdir -p $TEST_RESULTS_DIR /tmp/jsonfile pkgs="$(go list ./... | \ - grep -E -v '^github.com/hashicorp/consul/agent(/consul|/local|/xds|/routine-leak-checker)?$' | \ + grep -E -v '^github.com/hashicorp/consul/agent(/consul|/local|/routine-leak-checker)?$' | \ grep -E -v '^github.com/hashicorp/consul/command/')" gotestsum \ --jsonfile /tmp/jsonfile/go-test-race.log \ diff --git a/agent/xds/testing.go b/agent/xds/testing.go index 8a635114a..7fb9b224c 100644 --- a/agent/xds/testing.go +++ b/agent/xds/testing.go @@ -189,7 +189,7 @@ func (e *TestEnvoy) Close() error { // unblock the recv chan to simulate recv error when client disconnects if e.stream != nil && e.stream.recvCh != nil { close(e.stream.recvCh) - e.stream.recvCh = nil + e.stream = nil } if e.cancel != nil { e.cancel()