From ab196aeff21bedd7acd0e38d74e77989255f1d3f Mon Sep 17 00:00:00 2001 From: Alberto Bertogli Date: Fri, 6 Nov 2015 01:40:56 +0000 Subject: [PATCH] testing: only call flag.Parse if it has not been called before Calling flag.Parse twice can be problematic if other goroutines called flag.Parsed in between: the race detector complains due to the write after read from a different goroutine. This can happen if TestMain calls flag.Parse and launches goroutines that call flag.Parsed, for example if it initializes a server which checks flags. This patch makes testing.M.Run only parse the flags if they have not been parsed already. Change-Id: Id9f8c31c5f90614e3f34c63d1a32cf7e9055d68e Reviewed-on: https://go-review.googlesource.com/16739 Reviewed-by: Russ Cox --- src/testing/testing.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/testing/testing.go b/src/testing/testing.go index 1dcc35ebc0..6237da9abd 100644 --- a/src/testing/testing.go +++ b/src/testing/testing.go @@ -485,7 +485,11 @@ func MainStart(matchString func(pat, str string) (bool, error), tests []Internal // Run runs the tests. It returns an exit code to pass to os.Exit. func (m *M) Run() int { - flag.Parse() + // TestMain may have already called flag.Parse. + if !flag.Parsed() { + flag.Parse() + } + parseCpuList() before() -- 2.48.1