Skip to content

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

@Escapist25

Description

@Escapist25

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeWaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions