Does runtime.RaceDisable not work in non-std library ?

217 views
Skip to first unread message

nea...@gmail.com

unread,
Aug 22, 2018, 6:25:24 AM8/22/18
to golang-nuts
Hi,

When I copy the sync.Pool's source code in a repo, and using `//go:linkname` link those runtime functions manually.
When I run `go test -race`, it reports `DATA RACE`.
But the sync.Pool with the same test case does not report  `DATA RACE`.

Does runtime.RaceDisable not work in non-std library ?

Ian Lance Taylor

unread,
Aug 22, 2018, 10:34:35 AM8/22/18
to nea...@gmail.com, golang-nuts
It should work. I think you'll have to show us your code.

Ian

nea...@gmail.com

unread,
Aug 23, 2018, 12:35:44 AM8/23/18
to golang-nuts
Thanks for your attention.
When I run `go test -v -race -count=10 .`  `TestSyncAPI` reports DATA RACE, `TestSyncAPI2` not reports. 

在 2018年8月22日星期三 UTC+8下午10:34:35,Ian Lance Taylor写道:
Message has been deleted

Ian Lance Taylor

unread,
Aug 23, 2018, 4:36:06 PM8/23/18
to buaa...@gmail.com, golang-nuts
On Thu, Aug 23, 2018 at 12:43 AM, <buaa...@gmail.com> wrote:
> package main
>
> import "runtime"
>
> var a int
>
> func calc() {
> for i := 0; i < 10; i++ {
> go func() {
> for {
> runtime.RaceDisable()
> a++
> runtime.RaceEnable()
> }
> }()
>
> }
> }
>
> func main() {
> calc()
> }
>
> go run -race a.go

Thanks for the example. As the docs for runtime.RaceDisable say, it
only applies to synchronization events, like mutex locks. It doesn't
apply to memory accesses.

Ian


> 在 2018年8月22日星期三 UTC+8下午10:34:35,Ian Lance Taylor写道:
>>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

nea...@gmail.com

unread,
Aug 23, 2018, 11:46:10 PM8/23/18
to golang-nuts
Hi lan,

The sync.Pool has memory accesses in per-p private storage. Why it not reports `DATA RACE`, but it reports when I copy those code outside stdlib?
I'm confused about this, or there is something wrong in https://gist.github.com/lrita/efa8c4ae555b4b7cceee29b4ed819652 
Thanks.

在 2018年8月24日星期五 UTC+8上午4:36:06,Ian Lance Taylor写道:

Dave Cheney

unread,
Aug 24, 2018, 12:41:17 AM8/24/18
to golang-nuts
Hi,

Can you please do two things to help with this error report.

1. Please include the entire data race report -- we need this to match up the line with the source code you've provided in the gist
2. Please double check that you are not copying a your sync.Pool type by value, this can happen if you have a type declared on sync.Pool, not *sync.Pool, or it could happen if you do something like this

var x sync.Pool
y := x

Thanks

Dave

nea...@gmail.com

unread,
Aug 24, 2018, 3:29:07 AM8/24/18
to golang-nuts
Hi Dave,

1. the report:

> go version
go version go1.10 darwin/amd64

> go test -v -race -count=5 .
=== RUN   TestSyncAPI
==================
WARNING: DATA RACE
Read at 0x00c4200ca080 by goroutine 8:
  _/Users/yanqing11/test/go/sync1.(*Pool).Put()
      /Users/yanqing11/test/go/sync1/pool.go:108 +0xe8
  _/Users/yanqing11/test/go/sync1.TestSyncAPI.func2()
      /Users/yanqing11/test/go/sync1/pool_test.go:18 +0x50

Previous write at 0x00c4200ca080 by goroutine 7:
  _/Users/yanqing11/test/go/sync1.(*Pool).Put()
      /Users/yanqing11/test/go/sync1/pool.go:109 +0x2ad
  _/Users/yanqing11/test/go/sync1.TestSyncAPI.func1()
      /Users/yanqing11/test/go/sync1/pool_test.go:14 +0x5e

Goroutine 8 (running) created at:
  _/Users/yanqing11/test/go/sync1.TestSyncAPI()
      /Users/yanqing11/test/go/sync1/pool_test.go:17 +0x129
  testing.tRunner()
      /Users/yanqing11/dev/go/src/testing/testing.go:777 +0x16d

Goroutine 7 (finished) created at:
  _/Users/yanqing11/test/go/sync1.TestSyncAPI()
      /Users/yanqing11/test/go/sync1/pool_test.go:12 +0xfd
  testing.tRunner()
      /Users/yanqing11/dev/go/src/testing/testing.go:777 +0x16d
==================
==================
WARNING: DATA RACE
Write at 0x00c4200ca090 by goroutine 8:
  _/Users/yanqing11/test/go/sync1.(*Pool).Put()
      /Users/yanqing11/test/go/sync1/pool.go:115 +0x206
  _/Users/yanqing11/test/go/sync1.TestSyncAPI.func2()
      /Users/yanqing11/test/go/sync1/pool_test.go:18 +0x50

Previous read at 0x00c4200ca090 by goroutine 7:
  _/Users/yanqing11/test/go/sync1.(*Pool).Get()
      /Users/yanqing11/test/go/sync1/pool.go:141 +0x1ee
  _/Users/yanqing11/test/go/sync1.TestSyncAPI.func1()
      /Users/yanqing11/test/go/sync1/pool_test.go:13 +0x38

Goroutine 8 (running) created at:
  _/Users/yanqing11/test/go/sync1.TestSyncAPI()
      /Users/yanqing11/test/go/sync1/pool_test.go:17 +0x129
  testing.tRunner()
      /Users/yanqing11/dev/go/src/testing/testing.go:777 +0x16d

Goroutine 7 (finished) created at:
  _/Users/yanqing11/test/go/sync1.TestSyncAPI()
      /Users/yanqing11/test/go/sync1/pool_test.go:12 +0xfd
  testing.tRunner()
      /Users/yanqing11/dev/go/src/testing/testing.go:777 +0x16d
==================
==================
WARNING: DATA RACE
Read at 0x00c4200ce000 by goroutine 9:
  _/Users/yanqing11/test/go/sync1.(*Pool).getSlow()
      /Users/yanqing11/test/go/sync1/pool.go:175 +0x17a
  _/Users/yanqing11/test/go/sync1.(*Pool).Get()
      /Users/yanqing11/test/go/sync1/pool.go:148 +0x243
  _/Users/yanqing11/test/go/sync1.TestSyncAPI.func3()
      /Users/yanqing11/test/go/sync1/pool_test.go:23 +0x38

Previous write at 0x00c4200ce000 by goroutine 8:
  _/Users/yanqing11/test/go/sync1.(*Pool).Put()
      /Users/yanqing11/test/go/sync1/pool.go:115 +0x1c6
  _/Users/yanqing11/test/go/sync1.TestSyncAPI.func2()
      /Users/yanqing11/test/go/sync1/pool_test.go:18 +0x50

Goroutine 9 (running) created at:
  _/Users/yanqing11/test/go/sync1.TestSyncAPI()
      /Users/yanqing11/test/go/sync1/pool_test.go:22 +0x155
  testing.tRunner()
      /Users/yanqing11/dev/go/src/testing/testing.go:777 +0x16d

Goroutine 8 (finished) created at:
  _/Users/yanqing11/test/go/sync1.TestSyncAPI()
      /Users/yanqing11/test/go/sync1/pool_test.go:17 +0x129
  testing.tRunner()
      /Users/yanqing11/dev/go/src/testing/testing.go:777 +0x16d
==================
--- FAIL: TestSyncAPI (0.00s)
testing.go:730: race detected during execution of test
=== RUN   TestSyncAPI2
--- PASS: TestSyncAPI2 (0.00s)
=== RUN   TestSyncAPI
--- PASS: TestSyncAPI (0.00s)
=== RUN   TestSyncAPI2
--- PASS: TestSyncAPI2 (0.00s)
=== RUN   TestSyncAPI
--- PASS: TestSyncAPI (0.00s)
=== RUN   TestSyncAPI2
--- PASS: TestSyncAPI2 (0.00s)
=== RUN   TestSyncAPI
==================
WARNING: DATA RACE
Read at 0x00c42010a380 by goroutine 36:
  _/Users/yanqing11/test/go/sync1.(*Pool).Get()
      /Users/yanqing11/test/go/sync1/pool.go:136 +0x5c
  _/Users/yanqing11/test/go/sync1.TestSyncAPI.func3()
      /Users/yanqing11/test/go/sync1/pool_test.go:23 +0x38

Previous write at 0x00c42010a380 by goroutine 34:
  _/Users/yanqing11/test/go/sync1.(*Pool).Put()
      /Users/yanqing11/test/go/sync1/pool.go:109 +0x2ad
  _/Users/yanqing11/test/go/sync1.TestSyncAPI.func1()
      /Users/yanqing11/test/go/sync1/pool_test.go:14 +0x5e

Goroutine 36 (running) created at:
  _/Users/yanqing11/test/go/sync1.TestSyncAPI()
      /Users/yanqing11/test/go/sync1/pool_test.go:22 +0x155
  testing.tRunner()
      /Users/yanqing11/dev/go/src/testing/testing.go:777 +0x16d

Goroutine 34 (finished) created at:
  _/Users/yanqing11/test/go/sync1.TestSyncAPI()
      /Users/yanqing11/test/go/sync1/pool_test.go:12 +0xfd
  testing.tRunner()
      /Users/yanqing11/dev/go/src/testing/testing.go:777 +0x16d
==================
--- FAIL: TestSyncAPI (0.00s)
testing.go:730: race detected during execution of test
=== RUN   TestSyncAPI2
--- PASS: TestSyncAPI2 (0.00s)
=== RUN   TestSyncAPI
--- PASS: TestSyncAPI (0.00s)
=== RUN   TestSyncAPI2
--- PASS: TestSyncAPI2 (0.00s)
FAIL
FAIL _/Users/yanqing11/test/go/sync1 0.025s

2. the test case using a pointer of Pool, the test case code is also in the gist. https://gist.github.com/lrita/efa8c4ae555b4b7cceee29b4ed819652#file-pool_test-go 

Thanks for your reply.

在 2018年8月24日星期五 UTC+8下午12:41:17,Dave Cheney写道:

Ian Lance Taylor

unread,
Aug 24, 2018, 1:32:12 PM8/24/18
to nea...@gmail.com, golang-nuts
On Fri, Aug 24, 2018 at 12:28 AM, <nea...@gmail.com> wrote:
>
>> go test -v -race -count=5 .
> === RUN TestSyncAPI
> ==================
> WARNING: DATA RACE
> Read at 0x00c4200ca080 by goroutine 8:
> _/Users/yanqing11/test/go/sync1.(*Pool).Put()
> /Users/yanqing11/test/go/sync1/pool.go:108 +0xe8
> _/Users/yanqing11/test/go/sync1.TestSyncAPI.func2()
> /Users/yanqing11/test/go/sync1/pool_test.go:18 +0x50
>
> Previous write at 0x00c4200ca080 by goroutine 7:
> _/Users/yanqing11/test/go/sync1.(*Pool).Put()
> /Users/yanqing11/test/go/sync1/pool.go:109 +0x2ad
> _/Users/yanqing11/test/go/sync1.TestSyncAPI.func1()
> /Users/yanqing11/test/go/sync1/pool_test.go:14 +0x5e

These race reports are data races on memory accesses, and as such are
not affect by runtime.RaceDisable.

I don't know what the problem is here but I don't think it's related
to RaceDisable.

Ian

nea...@gmail.com

unread,
Aug 24, 2018, 10:04:17 PM8/24/18
to golang-nuts
Thanks a lot.

在 2018年8月25日星期六 UTC+8上午1:32:12,Ian Lance Taylor写道:

nea...@gmail.com

unread,
Nov 15, 2018, 1:29:41 AM11/15/18
to golang-nuts
According to https://github.com/golang/go/blob/7a6ccece877232fc58872c85fdf0718629b83d91/src/cmd/compile/internal/gc/racewalk.go#L45-L47 , the compile does not insert data race detecting code in package "sync".

在 2018年8月22日星期三 UTC+8下午6:25:24,nea...@gmail.com写道:
Reply all
Reply to author
Forward
0 new messages