Skip to content

runtime/race: TestRaceRange is not always detected as a race #10589

@rsc

Description

@rsc

It seems to me that TestRaceRange should always be detected as a race. The code is:

func TestRaceRange(t *testing.T) {
    const N = 2
    var a [N]int
    var x, y int
    done := make(chan bool, N)
    for i, v := range a {
        go func(i int) {
            // we don't want a write-vs-write race
            // so there is no array b here
            if i == 0 {
                x = v
            } else {
                y = v
            }
            done <- true
        }(i)
    }
    for i := 0; i < N; i++ {
        <-done
    }
}

The actual execution is

v = a[0]
go func() { x = v }()
v = a[1]
go func() { y = v }()

and the race is between the x = v and the v = a[1].

When Austin tweaked the goroutine scheduler behavior, the race detector stopped detecting this race. Adding a Gosched brought the detection of the race back, but it seems like it should be detected even without a Gosched.

@aclements

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions