Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime/race: Data race in append() missed by the race detector #22762

Closed
Escapist25 opened this issue Nov 16, 2017 · 5 comments
Closed

runtime/race: Data race in append() missed by the race detector #22762

Escapist25 opened this issue Nov 16, 2017 · 5 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@Escapist25
Copy link

Escapist25 commented Nov 16, 2017

Please answer these questions before submitting your issue. Thanks!

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

go version go1.9.2 darwin/amd64

Does this issue reproduce with the latest release?

yes

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

GOARCH="amd64"
GOBIN="/Users/escapist/gocode/bin"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/escapist/gocode/"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.9.2/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.9.2/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/_l/pbv9psw11872p3zrn8dptkq40000gn/T/go-build881315384=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

I run the following sample code with -race option:
code is also available at https://play.golang.org/p/L9SMvPzfcu

package main

import (
	"time"
)

var safeSlice = []int{1}
var testS = make([]int,0,1)
func main() {
	for i := 0;i < 5;i++ {
		go func(index int) {
			for {
				s1 := append(safeSlice, index)
				if s1[1] != index {
					panic("")
				}
				s2 := append(testS, index)   // <-   data race here
				if s2[0] != index {          // <-  panic here
					panic("")
				}
			}
		}(i)
	}
	time.Sleep(time.Second*5)
}

If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.

What did you expect to see?

Append to a fixed slice with capacity larger than size is not thread-safe because every goroutine is trying to write data to same memory location in the underlying array.
I expect the data race to be detected by the race detector.

What did you see instead?

The race detector missed the data race and every goroutine could see a slice with an unexpected value.

@artyom
Copy link
Member

artyom commented Nov 16, 2017

Interestingly for me the example outputs race warnings in a similar environment (100 runs out of 100):

ex ¶ go run -race ex.go
==================
WARNING: DATA RACE
Write at 0x00c420078000 by goroutine 6:
  main.main.func1()
      /tmp/ex/ex.go:17 +0x132

Previous write at 0x00c420078000 by goroutine 5:
  main.main.func1()
      /tmp/ex/ex.go:17 +0x132

Goroutine 6 (running) created at:
  main.main()
      /tmp/ex/ex.go:11 +0x50

Goroutine 5 (running) created at:
  main.main()
      /tmp/ex/ex.go:11 +0x50
==================
==================
WARNING: DATA RACE
Write at 0x00c420078000 by goroutine 7:
  main.main.func1()
      /tmp/ex/ex.go:17 +0x132

Previous write at 0x00c420078000 by goroutine 5:
  main.main.func1()
      /tmp/ex/ex.go:17 +0x132

Goroutine 7 (running) created at:
  main.main()
      /tmp/ex/ex.go:11 +0x50

Goroutine 5 (running) created at:
  main.main()
      /tmp/ex/ex.go:11 +0x50
==================
panic: 

goroutine 19 [running]:
main.main.func1(0x1)
	/tmp/ex/ex.go:19 +0x214
created by main.main
	/tmp/ex/ex.go:11 +0x51
exit status 2

This is an official build — go version go1.9.2 darwin/amd64 from go1.9.2.darwin-amd64.tar.gz file:

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/tmp/go:/Users/artyom/go"
GORACE=""
GOROOT="/Users/artyom/Library/go"
GOTOOLDIR="/Users/artyom/Library/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/lb/3rk8rqs53czgb4v35w_342xc0000gn/T/go-build997454935=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

@ianlancetaylor
Copy link
Contributor

I also see a data race warning.

Please show us the exact commands that you are executing and exactly what you see. Thanks.

@bradfitz bradfitz changed the title Data race in append() missed by the race detector runtime/race: Data race in append() missed by the race detector Nov 16, 2017
@bradfitz bradfitz added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Nov 16, 2017
@Escapist25
Copy link
Author

I ran this with go run -race
I tried again with go build -race and saw the expected data race warning.

@ianlancetaylor
Copy link
Contributor

Odd. I see the race with both go build -race and go run -race.

Can you show us the complete output of go run -race FILE.go?

@Escapist25
Copy link
Author

go run tx.go ‹system›
panic:

goroutine 4 [running]:
main.main.func1(0x0)
/Users/escapist/gocode/src/github.com/ysxue/pipe/tx.go:19 +0x143
created by main.main
/Users/escapist/gocode/src/github.com/ysxue/pipe/tx.go:11 +0x43
panic:

goroutine 8 [running]:
main.main.func1(0x4)
/Users/escapist/gocode/src/github.com/ysxue/pipe/tx.go:19 +0x143
created by main.main
/Users/escapist/gocode/src/github.com/ysxue/pipe/tx.go:11 +0x43
exit status 2

go run -race tx.go ‹system›

WARNING: DATA RACE
Write at 0x00c420016080 by goroutine 6:
main.main.func1()
/Users/escapist/gocode/src/github.com/ysxue/pipe/tx.go:17 +0x132

Previous write at 0x00c420016080 by goroutine 5:
main.main.func1()
/Users/escapist/gocode/src/github.com/ysxue/pipe/tx.go:17 +0x132

Goroutine 6 (running) created at:
main.main()
/Users/escapist/gocode/src/github.com/ysxue/pipe/tx.go:11 +0x50

Goroutine 5 (running) created at:
main.main()
/Users/escapist/gocode/src/github.com/ysxue/pipe/tx.go:11 +0x50

panic:

goroutine 5 [running]:
main.main.func1(0x1)
/Users/escapist/gocode/src/github.com/ysxue/pipe/tx.go:19 +0x214
created by main.main
/Users/escapist/gocode/src/github.com/ysxue/pipe/tx.go:11 +0x51
exit status 2

It looks like I misplaced the -race option.
The race detector works fine.

@golang golang locked and limited conversation to collaborators Nov 20, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

5 participants