When running coverage in a concurrent program, use the -mode=atomic flag to
avoid data races in the counters. This unavoidably has a significant
performance hit, but it should resolve this race.

-rob


On Sun, Oct 2, 2022 at 5:10 AM Shulhan <m.shul...@gmail.com> wrote:

> Hi gophers,
>
> The latest Go tip always fail with data race when running with -race
> and -coverprofile options.
>
> Here is an example of data race output,
>
> ----
> $ CGO_ENABLED=1 go test -failfast -race -count=1 -coverprofile=cover.out
> ./...
> ...
> ==================
> WARNING: DATA RACE
> Read at 0x000001e5f04c by main goroutine:
>
> internal/coverage/encodecounter.(*CoverageDataWriter).writeCounters.func2()
>       /home/ms/opt/go/src/internal/coverage/encodecounter/encode.go:261
> +0x11c
>   runtime/coverage.(*emitState).VisitFuncs()
>       /home/ms/opt/go/src/runtime/coverage/emit.go:539 +0x6bc
>   internal/coverage/encodecounter.(*CoverageDataWriter).writeCounters()
>       /home/ms/opt/go/src/internal/coverage/encodecounter/encode.go:268
> +0x16f
>   internal/coverage/encodecounter.(*CoverageDataWriter).AppendSegment()
>       /home/ms/opt/go/src/internal/coverage/encodecounter/encode.go:175
> +0x8ea
>   internal/coverage/encodecounter.(*CoverageDataWriter).Write()
>       /home/ms/opt/go/src/internal/coverage/encodecounter/encode.go:71
> +0x97
>   runtime/coverage.(*emitState).emitCounterDataFile()
>       /home/ms/opt/go/src/runtime/coverage/emit.go:573 +0x91
>   runtime/coverage.emitCounterDataToDirectory()
>       /home/ms/opt/go/src/runtime/coverage/emit.go:322 +0x310
>   runtime/coverage.processCoverTestDir()
>       /home/ms/opt/go/src/runtime/coverage/testsupport.go:39 +0x1c4
>   main.coverTearDown()
>       _testmain.go:179 +0x159
>   testing.coverReport2()
>       /home/ms/opt/go/src/testing/newcover.go:37 +0xcb
>   testing.coverReport()
>       /home/ms/opt/go/src/testing/cover.go:83 +0xc74
>   testing.(*M).writeProfiles()
>       /home/ms/opt/go/src/testing/testing.go:2053 +0xc6f
>   testing.(*M).after.func1()
>       /home/ms/opt/go/src/testing/testing.go:1987 +0x30
>   sync.(*Once).doSlow()
>       /home/ms/opt/go/src/sync/once.go:74 +0x101
>   sync.(*Once).Do()
>       /home/ms/opt/go/src/sync/once.go:65 +0x46
>   testing.(*M).after()
>       /home/ms/opt/go/src/testing/testing.go:1986 +0x55
>   testing.(*M).Run.func4()
>       /home/ms/opt/go/src/testing/testing.go:1761 +0x39
>   runtime.deferreturn()
>       /home/ms/opt/go/src/runtime/panic.go:476 +0x32
>   testing.(*M).Run()
>       /home/ms/opt/go/src/testing/testing.go:1771 +0xbb3
>   github.com/shuLhan/share/lib/dns.TestMain()
>       /home/ms/go/src/github.com/shuLhan/share/lib/dns/dns_test.go:63
> +0x5db
>   main.main()
>       _testmain.go:192 +0x33d
>
> Previous write at 0x000001e5f04c by goroutine 9:
>   sync/atomic.AddInt32()
>       /home/ms/opt/go/src/runtime/race_amd64.s:281 +0xb
>   sync/atomic.AddUint32()
>       <autogenerated>:1 +0x1a
>   github.com/shuLhan/share/lib/dns.(*Server).processRequest()
>       /home/ms/go/src/github.com/shuLhan/share/lib/dns/server.go:593
> +0xc67
>   github.com/shuLhan/share/lib/dns.(*Server).ListenAndServe.func1()
>       /home/ms/go/src/github.com/shuLhan/share/lib/dns/server.go:187 +0x39
>
> Goroutine 9 (running) created at:
>   github.com/shuLhan/share/lib/dns.(*Server).ListenAndServe()
>       /home/ms/go/src/github.com/shuLhan/share/lib/dns/server.go:187 +0xe6
>   github.com/shuLhan/share/lib/dns.TestMain.func1()
>       /home/ms/go/src/github.com/shuLhan/share/lib/dns/dns_test.go:54
> +0x44
> ==================
> ...
> ----
>
> There are many lines like that with the same pattern.
>
> In the above snippet, the lib/dns/dns_test.go:63 point this code [1],
>
>   os.Exit(m.Run())
>
> So it does not make sense if the data race is in my code.
>
> A quick bisect point to this commit [2].
>
> If someone can confirm this, I will submit an issue to GitHub.
>
> [1]
> https://github.com/shuLhan/share/blob/61720a183756bdf5a8af45e7d75116ce7ef188e0/lib/dns/dns_test.go#L63
> [2]
> https://go.googlesource.com/go/+/53773a5d0892be4489b4d5e91bbc8ae61000ada7%5E%21/
>
> --
> { "git":"git.sr.ht/~shulhan", "site":"kilabit.info" }
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/20221002011006.4ffaaaec%40inspiro.localdomain
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOXNBZTR_Y2-UMrqCYspmqRBMPzOgAmSCc6fCiqZNpppLGbLqQ%40mail.gmail.com.

Reply via email to