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

linter: loopvarcapture should check interface methods #102679

Merged
merged 1 commit into from
May 11, 2023

Conversation

srosenberg
Copy link
Member

The loopvarcapture linter is part of roachvet. It flags incorrect uses of loop variables captured by reference in Go routines or defer statements--a common source of data races.

In addition to checking Go routines created via the go keyword, the linter also checks against other (internal) APIs, which are known to create Go routines; this is specified via GoRoutineFunctions.

An existing bug prevented the linter from checking inside Monitor.Go, which are commonly used inside roachtests.
This PR fixes the linter bug as well as the pre-existing bug inside a roachtest, which this linter now detects.

This PR also removes the uncessary hack, i.e., RunDespiteErrors, introduced in [1]. By allowing the type checker to swallow errors, the test was susceptible to silent errors, e.g., buggy testdata. Instead, we work around by disabling CGO.

Epic: none
Fixes: #102678
Release note: None

[1] #84867 (comment)

@srosenberg srosenberg requested a review from renatolabs May 1, 2023 03:03
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@srosenberg srosenberg added the backport-23.1.x Flags PRs that need to be backported to 23.1 label May 1, 2023
The `loopvarcapture` linter is part of `roachvet`.
It flags incorrect uses of loop variables captured by reference
in Go routines or defer statements--a common source of data races.

In addition to checking Go routines created via the `go` keyword,
the linter also checks against other (internal) APIs, which are known
to create Go routines; this is specified via `GoRoutineFunctions`.

An existing bug prevented the linter from checking inside `Monitor.Go`,
which are commonly used inside roachtests.
This PR fixes the linter bug as well as the pre-existing bug inside
a roachtest, which this linter now detects.

This PR also removes the uncessary hack, i.e., `RunDespiteErrors`, introduced
in [1]. By allowing the type checker to swallow errors, the test was susceptible
to silent errors, e.g., buggy testdata. Instead, we work around by disabling CGO.

Epic: none
Fixes: cockroachdb#102678
Release note: None

[1] cockroachdb#84867 (comment)
@srosenberg srosenberg requested a review from a team as a code owner May 1, 2023 03:47
@srosenberg srosenberg requested review from smg260 and removed request for a team May 1, 2023 03:47
@srosenberg
Copy link
Member Author

srosenberg commented May 1, 2023

First attempt failed [1], [2], as expected (since it didn't include the fix to pkg/cmd/roachtest/tests/kv.go),

compilepkg: nogo: errors found by nogo during build-time code analysis:
[03:19:20 ](https://teamcity.cockroachdb.com/buildConfiguration/Cockroach_Ci_Tests_UnusedLint/9890552?buildTab=log&focusLine=2515&linesState=2515)      pkg/cmd/roachtest/tests/kv.go:767:69: loop variable 'i' captured by reference, often leading to data races (loopvarcapture)

[1] https://teamcity.cockroachdb.com/buildConfiguration/Cockroach_Ci_Tests_UnusedLint/9890552
[2] https://teamcity.cockroachdb.com/buildConfiguration/Cockroach_Ci_Tests_LocalRoachtest/9890543

Copy link
Contributor

@renatolabs renatolabs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the investigation and fix! I have a vague memory of using StaticCallee over Callee, but the latter makes more sense.

Copy link
Collaborator

@herkolategan herkolategan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 5 of 5 files at r1, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @smg260)

@srosenberg
Copy link
Member Author

TFTR!

bors r=renatolabs,herkolategan

@craig
Copy link
Contributor

craig bot commented May 11, 2023

Build succeeded:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-23.1.x Flags PRs that need to be backported to 23.1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

loopvarcapture linter should check interface methods
4 participants