go: runtime/race: race detector misses race condition in case of fmt.Println

What version of Go are you using (go version)?

go version go1.11 linux/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/dd/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/dd/go:/home/dd/data/Documents/dev"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go-1.11/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go-1.11/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="0"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -fmessage-length=0 -fdebug-prefix-map=/tmp/user/1000/go-build740506104=/tmp/go-build -gno-record-gcc-switches"

What did you do?

cat /home/dd/.GoLand2018.2/config/scratches/scratch_5.go

package main

import "fmt"

var a int

func f() {
	fmt.Println(a) // if comment this, data race is found
	a = 1
}

func main() {
	go f()
	fmt.Println(a)
}

go run -race /home/dd/.GoLand2018.2/config/scratches/scratch_5.go

What did you expect to see?

0
0
==================
WARNING: DATA RACE
Write at 0x0000005e3640 by goroutine 6:
  main.f()
      /home/dd/.GoLand2018.2/config/scratches/scratch_5.go:9 +0x69

Previous read at 0x0000005e3640 by main goroutine:
  main.main()
      /home/dd/.GoLand2018.2/config/scratches/scratch_5.go:14 +0x5e

Goroutine 6 (running) created at:
  main.main()
      /home/dd/.GoLand2018.2/config/scratches/scratch_5.go:13 +0x46
==================
Found 1 data race(s)

What did you see instead?

0
0

I think this is related with: https://github.com/golang/go/issues/12664 cc @dvyukov

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Comments: 24 (23 by maintainers)

Most upvoted comments

@mirtchovski : your last example doesn’t have a race. The channel operations establish a happens-before relationship between the operations in f and the final write. Swapping the last two statements removes the happens-before relationship.