From: Rhys Hiltner Date: Tue, 7 Dec 2021 21:32:24 +0000 (-0800) Subject: runtime: fix flake in TestCgoPprofThread X-Git-Tag: go1.18beta1~50 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=46db6aa1573def4ba06dbf5c38e704d85dc303b6;p=gostls13.git runtime: fix flake in TestCgoPprofThread If the test's main goroutine receives a SIGPROF while creating the C-owned thread for the test, that sample will appear in the resulting profile. The root end of that stack will show a set of Go functions. The leaf end will be the C functions returned by the SetCgoTraceback handler, which will confuse the test runner. Add a label to the main goroutine while it calls in to C, so all profile samples that triggered the SetCgoTraceback handler are either correct, or can easily be excluded from the test's analysis. (The labels will not apply to the resulting C-owned thread, which does not use goroutines.) Fixes #43174 Change-Id: Ica3100ca0f191dcf91b30b0084e8541c5a25689f Reviewed-on: https://go-review.googlesource.com/c/go/+/370135 Reviewed-by: Michael Pratt Trust: Michael Pratt Run-TryBot: Michael Pratt Trust: Michael Knyszek TryBot-Result: Gopher Robot --- diff --git a/src/runtime/crash_cgo_test.go b/src/runtime/crash_cgo_test.go index bfb260a143..058eae1c09 100644 --- a/src/runtime/crash_cgo_test.go +++ b/src/runtime/crash_cgo_test.go @@ -314,7 +314,7 @@ func testCgoPprof(t *testing.T, buildArg, runArg, top, bottom string) { defer os.Remove(fn) for try := 0; try < 2; try++ { - cmd := testenv.CleanCmdEnv(exec.Command(testenv.GoToolPath(t), "tool", "pprof", "-traces")) + cmd := testenv.CleanCmdEnv(exec.Command(testenv.GoToolPath(t), "tool", "pprof", "-tagignore=ignore", "-traces")) // Check that pprof works both with and without explicit executable on command line. if try == 0 { cmd.Args = append(cmd.Args, exe, fn) diff --git a/src/runtime/testdata/testprogcgo/threadpprof.go b/src/runtime/testdata/testprogcgo/threadpprof.go index 4bc84d16d0..e093f67e1e 100644 --- a/src/runtime/testdata/testprogcgo/threadpprof.go +++ b/src/runtime/testdata/testprogcgo/threadpprof.go @@ -64,6 +64,7 @@ void runCPUHogThread(void) { import "C" import ( + "context" "fmt" "os" "runtime" @@ -98,7 +99,14 @@ func pprofThread() { os.Exit(2) } - C.runCPUHogThread() + // This goroutine may receive a profiling signal while creating the C-owned + // thread. If it does, the SetCgoTraceback handler will make the leaf end of + // the stack look almost (but not exactly) like the stacks the test case is + // trying to find. Attach a profiler label so the test can filter out those + // confusing samples. + pprof.Do(context.Background(), pprof.Labels("ignore", "ignore"), func(ctx context.Context) { + C.runCPUHogThread() + }) time.Sleep(1*time.Second)