Skip to content

runtime/race: os.File.ReadAt defeats race detector #51618

Closed
ferrmin/go
#312
@dominikh

Description

@dominikh

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

$ go version
go version devel go1.19-45f45444b3 Sat Mar 5 21:20:16 2022 +0000 linux/amd64
go version go1.17.8 linux/amd64
go version go1.15.15 linux/amd64

Does this issue reproduce with the latest release?

Yes.

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/dominikh/.cache/go-build"
GOENV="/home/dominikh/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/dominikh/prj/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/dominikh/prj"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/dominikh/prj/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/dominikh/prj/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.17.8"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/dominikh/prj/src/honnef.co/go/bittorrent/go.mod"
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 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2412946897=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Run

package main

import "os"

func main() {
	buf := make([]byte, 1)
	f, err := os.Open("/dev/urandom")
	if err != nil {
		panic(err)
	}

	go func() {
		for {
			n, err := f.ReadAt(buf, 0)
			if err != nil || n == 0 {
				panic(err)
			}
		}
	}()

	for {
		println(buf[0])
	}
}

with GORACE=halt_on_error=1 go run -race foo.go

What did you expect to see?

I expect the race detector to flag the race.

What did you see instead?

The race detector misses the race and we get an infinite stream of random bytes.

Using Read instead of ReadAt almost immediately flags the race. The issue isn't specific to the file being opened. It happens just the same with files in /proc or actual files on a hard drive.

/cc @golang/runtime

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.RaceDetector

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions