-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
82324: Add `loopvarcapture` linter r=srosenberg a=renatolabs This PR introduces a new linter: `loopvarcapture`. It reports uses of loop variables captured by reference in Go routines or `defer` statements, a common source of data races [1]. `govet` currently has a similar linter [2]; however, that project prioritizes having no false positives at the expense of allowing false negatives. This linter, on the other hand, represents the opinion that loop variables should not be captured by reference in Go routines even when it's safe to do so. That behavior is confusing and concurrency added to related code over time could lead to the introduction of data races, potentially manifesting as bugs in the product or flakiness in the tests. These issues are hard to debug and take a lot of developer time. For instance, the following code will be identified by the linter: ```go for i, obj := range collection { go func() { // ... myFunc(i) // variable 'i' captured by reference // ... }() showProgress := func() { fmt.Printf("finished processing: %#v\n", obj) } go func() { defer showProgress() // function 'showProgress' captures loop variable 'obj' by reference // ... }() } ``` To fix such cases, the code is expected to not use the loop variable in the closure, as in the examples below: ```go for i, obj := range collection { i := i // create copy of index variable go func() { // ... myFunc(i) // this is OK -- using loop's copy of the index variable // ... }() showProgress := func(obj MyStruct) { fmt.Printf("finished processing: %#v\n", obj) } go func(obj MyStruct) { defer showProgress(obj) // this is OK -- passing loop variable as parameter to the closure // ... }(obj) } ``` Developers are still able to use their own judgement and disable this linter in specific instances by using a `nolint` comment. The commits after the first one fix violations found by this new linter in multiple packages; all of the violations happened in test files. In most cases, the fixes did not actually solve data races because most of our unit tests run sequentially. There was a minor roachtest bug fix related to progress logging. Apologies for updating this many packages in one PR; the majority of the changes were mechanical and don't require much review by the different teams. Reviewing of the first commit should be mostly done by `@cockroachdb/test-eng.` [1] A Study of Real-World Data Races in Golang: https://arxiv.org/pdf/2204.00764.pdf [2] https://github.com/golangci/govet/blob/44ddbe260190d79165f4150b828650780405d801/rangeloop.go#L36 83214: kv: Fix test to correctly test the SystemClass r=ajwerner a=andrewbaptist Previously this test had several issues that are all addressed. First the system range it was writing to only had a single replica, so even if it was correctly written to, a network partition wouldn't test anything. Additionally, the address it was attempting to write to was a lease change, which is not in the system range. The reason this worked previously is that there was an early exist from the change lease code if the old and new leaseholders were the same, so it didn't actually attempt to write to the leaseholder name at all. This change addresses all this, but writing to a key in the liveness range and first replicating the liveness range to all nodes. Release note: None Co-authored-by: Renato Costa <[email protected]> Co-authored-by: Andrew Baptist <[email protected]>
- Loading branch information
Showing
47 changed files
with
1,133 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.