Skip to content

Commit ab196ae

Browse files
albertitorsc
authored andcommitted
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 <[email protected]>
1 parent a9ca213 commit ab196ae

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/testing/testing.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,11 @@ func MainStart(matchString func(pat, str string) (bool, error), tests []Internal
485485

486486
// Run runs the tests. It returns an exit code to pass to os.Exit.
487487
func (m *M) Run() int {
488-
flag.Parse()
488+
// TestMain may have already called flag.Parse.
489+
if !flag.Parsed() {
490+
flag.Parse()
491+
}
492+
489493
parseCpuList()
490494

491495
before()

0 commit comments

Comments
 (0)