Skip to content

Running Tests with coverage and "Go tool arguments:" '-race' gives erroneous race conditions #2037

Closed
@SonOfBytes

Description

@SonOfBytes

Coverage uses inserted global variable in the code to track coverage. When this is combined with the race checks it can lead to erroneous and very confusing race conditions.

The following site presents some simple code when run with the "-race" flag added and in coverage mode will give the false erroneous race conditions.

http://herman.asia/running-the-go-race-detector-with-cover

Package:

package coverrace

func add100() int {
    total := 0
    c := make(chan int, 1)
    for i := 0; i < 100; i++ {
        go func(chan int) {
            c <- 1
        }(c)
    }
    for u := 0; u < 100; u++ {
        total += <-c
    }
    return total
}

Test:

package coverrace

import "testing"

func TestCoverRace(t *testing.T) {
    got := add100()
    if got != 100 {
        t.Errorf("got %d, want %d", got, 100)
    }
}

Reproducing command generated by PyCharm:

go test -v -race ./... -coverprofile=/someUserDir/coverrace$All_in__coverrace_.coverage -covermode=count

This has been fixed in go > 1.3+ by adding support for the '-covermode=atomic' flag. This ensures that the access to the coverage global variable is protected by the sync package to avoid race detection.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions