From b394bd8bba1d46add1a391b424f469e606099217 Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Mon, 28 Oct 2019 12:18:01 -0400 Subject: [PATCH] go/packages: do not return from goListDriver with goroutines still in flight This fixes a race found in https://storage.googleapis.com/go-build-log/0ae93896/linux-amd64-race_e84a66dd.log. Change-Id: I6b928e3b96378d458b48cf1f27341775a3dd0601 Reviewed-on: https://go-review.googlesource.com/c/tools/+/203897 Run-TryBot: Bryan C. Mills TryBot-Result: Gobot Gobot Reviewed-by: Michael Matloob --- go/packages/golist.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/go/packages/golist.go b/go/packages/golist.go index 7eb88c0751..dbdc791e1e 100644 --- a/go/packages/golist.go +++ b/go/packages/golist.go @@ -110,6 +110,7 @@ func goListDriver(cfg *Config, patterns ...string) (*driverResponse, error) { sizeswg.Done() }() } + defer sizeswg.Wait() // start fetching rootDirs var info goInfo @@ -128,6 +129,10 @@ func goListDriver(cfg *Config, patterns ...string) (*driverResponse, error) { return &info } + // Ensure that we don't leak goroutines: Load is synchronous, so callers will + // not expect it to access the fields of cfg after the call returns. + defer getGoInfo() + // always pass getGoInfo to golistDriver golistDriver := func(cfg *Config, patterns ...string) (*driverResponse, error) { return golistDriver(cfg, getGoInfo, patterns...) @@ -734,9 +739,9 @@ func golistDriver(cfg *Config, rootsDirs func() *goInfo, words ...string) (*driv // go list uses the following identifiers in ImportPath and Imports: // // "p" -- importable package or main (command) - // "q.test" -- q's test executable + // "q.test" -- q's test executable // "p [q.test]" -- variant of p as built for q's test executable - // "q_test [q.test]" -- q's external test package + // "q_test [q.test]" -- q's external test package // // The packages p that are built differently for a test q.test // are q itself, plus any helpers used by the external test q_test,