Does the Go race detector catch all data race bugs?

What is a data race?

A data race occurs when two goroutines access the same variable concurrently and at least one of the accesses is a write.

Data races exist in go

It is possible to write incorrectly synchronized code and compile it. The compiler regards it as valid and doesn’t emit any warnings or errors for data race bugs.

The race detector doesn’t catch bugs at compile time

$ go build -race mycmd

The race detector reported a warning but I don’t think my code actually has a bug. Can I ignore it?

No.

The race detector reported a warning in a non-critical part of my code. Can I ignore it?

No.

The gopher aviator thinks it’s okay to suddenly be at the same position as the tree, but actually it’s not.

I ran my program, and the race detector didn’t detect anything. Does this mean that my code has no data race bugs?

No.

$ go run -race racy3.go 
[1 0 1]
$ go run -race racy3.go 
==================
WARNING: DATA RACE
Read at 0x00c4200b8000 by goroutine 7:
main.main.func2()
/home/deleplace/racy3.go:30 +0x66
Previous write at 0x00c4200b8000 by goroutine 6:
main.main.func1()
/home/deleplace/racy3.go:24 +0x87
Goroutine 7 (running) created at:
main.main()
/home/deleplace/racy3.go:28 +0x1d0
Goroutine 6 (finished) created at:
main.main()
/home/deleplace/racy3.go:22 +0x192
==================
[0 0 2]
Found 1 data race(s)

If a data race condition occurs during an execution of my program, will the race detector catch it?

Yes, with very high probability.

Conclusion

  • The race detector is easy to use. I strongly recommend it.
  • It can’t tell you all of the bugs lurking in your code.
  • But it shouts a WARNING for every data race that occurs.
  • Almost all of the races: very few false negatives!
  • Only true races: no false positives!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Val Deleplace

Val Deleplace

451 Followers

Engineer on cloudy things @Google. Opinions my own. Twitter @val_deleplace