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

cmd/vet: check for concurrent use of T.FatalX and T.SkipX #15674

Open
dsnet opened this issue May 13, 2016 · 2 comments
Open

cmd/vet: check for concurrent use of T.FatalX and T.SkipX #15674

dsnet opened this issue May 13, 2016 · 2 comments
Labels
Analysis Issues related to static analysis (vet, x/tools/go/analysis) help wanted
Milestone

Comments

@dsnet
Copy link
Member

dsnet commented May 13, 2016

The testing package is explicit about the following:

A test ends when its Test function returns or calls any of the methods FailNow, Fatal, Fatalf, SkipNow, Skip, or Skipf. Those methods, as well as the Parallel method, must be called only from the goroutine running the Test function.

However, this is easy to overlook and it is not immediately obvious that the code below is a bad test:

func TestFoo(t *testing.T) {
    go func() {
        t.Fatal("fatal") // Called from outside the Test function
    }()
    time.Sleep(1 * time.Second)
}

This outputs (what a user expects):

--- FAIL: TestFoo (1.00s)
    foo_test.go:10: fatal

Giving the user a false sense of having written correct test.

However, when running the test with the -race flag, it becomes obvious this is not okay:

==================
WARNING: DATA RACE
....
==================
--- FAIL: TestFoo (1.00s)
    foo_test.go:10: fatal

Perhaps the vet tool can check for these cases.

@josharian
Copy link
Contributor

I think if the race detector catches the problem then vet doesn't need to check too.

@dsnet dsnet added this to the Unplanned milestone May 14, 2016
@dsnet
Copy link
Member Author

dsnet commented May 14, 2016

The difficulty is that the Fatal case isn't always triggered, so the race detector isn't able to detect them. But when Fatal is actually hit, funky things can sometimes happen.

Go vet offers the ability to detect these abuses of the testing package earlier on.

It comes down to static analysis vs dynamic analysis.

dominikh added a commit to dominikh/go-staticcheck that referenced this issue Nov 12, 2016
Inspired by golang/go#15674

Idea-By: Joe Tsai <joetsai@digital-static.net>
dominikh added a commit to dominikh/go-staticcheck that referenced this issue Nov 13, 2016
Inspired by golang/go#15674

Idea-By: Joe Tsai <joetsai@digital-static.net>
dominikh added a commit to dominikh/go-tools that referenced this issue Jan 24, 2017
Inspired by golang/go#15674

Idea-By: Joe Tsai <joetsai@digital-static.net>
@adonovan adonovan added the Analysis Issues related to static analysis (vet, x/tools/go/analysis) label Apr 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Analysis Issues related to static analysis (vet, x/tools/go/analysis) help wanted
Projects
None yet
Development

No branches or pull requests

4 participants