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: os.File.ReadAt defeats race detector #51618

Closed
dominikh opened this issue Mar 11, 2022 · 6 comments · Fixed by ferrmin/go#312
Closed

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

dominikh opened this issue Mar 11, 2022 · 6 comments · Fixed by ferrmin/go#312
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. RaceDetector

Comments

@dominikh
Copy link
Member

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

@dominikh dominikh added RaceDetector NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Mar 11, 2022
@dominikh
Copy link
Member Author

Quick investigation by @ericlagergren suggests that syscall.Pread (and Pwrite) are missing the required runtime calls, unlike Read and Write.

@ianlancetaylor
Copy link
Contributor

Thanks for the analysis.

@gopherbot
Copy link

Change https://go.dev/cl/391954 mentions this issue: syscall: add race annotations to Pread and Pwrite

@gopherbot
Copy link

Change https://go.dev/cl/392774 mentions this issue: syscall: add race annotations to Windows ReadFile and WriteFile

gopherbot pushed a commit that referenced this issue Mar 15, 2022
For #51618
Fixes #51673

Change-Id: Ie63408d62303293d80afed8d5cf1cb164a8abecc
Reviewed-on: https://go-review.googlesource.com/c/go/+/392774
Trust: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
@gopherbot
Copy link

Change https://go.dev/cl/392794 mentions this issue: unix: add race annotations to Pread and Pwrite

@gopherbot
Copy link

Change https://go.dev/cl/392814 mentions this issue: windows: add race annotations to Windows ReadFile and WriteFile

gopherbot pushed a commit to golang/sys that referenced this issue Mar 15, 2022
Follow CL 392774 which changed Windows ReadFile and WriteFile in package
syscall.

For golang/go#51618

Change-Id: I0f8046adb0f8145bd0775a3b5399647897cf19ce
Reviewed-on: https://go-review.googlesource.com/c/sys/+/392814
Trust: Tobias Klauser <tobias.klauser@gmail.com>
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
gopherbot pushed a commit to golang/sys that referenced this issue Mar 15, 2022
Follow CL 391954 which changed Pread/Pwrite in package syscall.

For golang/go#51618

Change-Id: Icc587c61f083886bb66ca96717f686f56b398e34
Reviewed-on: https://go-review.googlesource.com/c/sys/+/392794
Trust: Tobias Klauser <tobias.klauser@gmail.com>
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
@golang golang locked and limited conversation to collaborators Mar 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. RaceDetector
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants