Skip to content

Commit 0441b43

Browse files
committed
gopls/internal/lsp/cache: use specific mutexes for module data
View.mu was documented as protecting "most mutable state", but in fact only protected module upgrades and vulns. To be consistent, use specific mutexes for this data. Change-Id: I7454ca7548a1f9c9db786eafbb9cbef3961194dd Reviewed-on: https://go-review.googlesource.com/c/tools/+/459791 TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Alan Donovan <[email protected]> Run-TryBot: Robert Findley <[email protected]> gopls-CI: kokoro <[email protected]>
1 parent 33071fb commit 0441b43

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

gopls/internal/lsp/cache/view.go

+14-17
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,16 @@ type View struct {
6464
explicitGowork span.URI // explicitGowork: if non-empty, a user-specified go.work location (TODO: deprecate)
6565
workspaceInformation // grab-bag of Go environment information (TODO: cleanup)
6666

67-
// mu protects most mutable state of the view.
68-
//
69-
// TODO(rfindley): specify exactly which mutable state is guarded.
70-
mu sync.Mutex
71-
7267
importsState *importsState
7368

7469
// moduleUpgrades tracks known upgrades for module paths in each modfile.
7570
// Each modfile has a map of module name to upgrade version.
76-
moduleUpgrades map[span.URI]map[string]string
71+
moduleUpgradesMu sync.Mutex
72+
moduleUpgrades map[span.URI]map[string]string
7773

7874
// vulns maps each go.mod file's URI to its known vulnerabilities.
79-
vulns map[span.URI]*govulncheck.Result
75+
vulnsMu sync.Mutex
76+
vulns map[span.URI]*govulncheck.Result
8077

8178
// filesByURI maps URIs to the canonical URI for the file it denotes.
8279
// We also keep a set of candidates for a given basename
@@ -997,8 +994,8 @@ func (v *View) IsGoPrivatePath(target string) bool {
997994
}
998995

999996
func (v *View) ModuleUpgrades(modfile span.URI) map[string]string {
1000-
v.mu.Lock()
1001-
defer v.mu.Unlock()
997+
v.moduleUpgradesMu.Lock()
998+
defer v.moduleUpgradesMu.Unlock()
1002999

10031000
upgrades := map[string]string{}
10041001
for mod, ver := range v.moduleUpgrades[modfile] {
@@ -1013,8 +1010,8 @@ func (v *View) RegisterModuleUpgrades(modfile span.URI, upgrades map[string]stri
10131010
return
10141011
}
10151012

1016-
v.mu.Lock()
1017-
defer v.mu.Unlock()
1013+
v.moduleUpgradesMu.Lock()
1014+
defer v.moduleUpgradesMu.Unlock()
10181015

10191016
m := v.moduleUpgrades[modfile]
10201017
if m == nil {
@@ -1027,8 +1024,8 @@ func (v *View) RegisterModuleUpgrades(modfile span.URI, upgrades map[string]stri
10271024
}
10281025

10291026
func (v *View) ClearModuleUpgrades(modfile span.URI) {
1030-
v.mu.Lock()
1031-
defer v.mu.Unlock()
1027+
v.moduleUpgradesMu.Lock()
1028+
defer v.moduleUpgradesMu.Unlock()
10321029

10331030
delete(v.moduleUpgrades, modfile)
10341031
}
@@ -1039,8 +1036,8 @@ var timeNow = time.Now // for testing
10391036
func (v *View) Vulnerabilities(modfiles ...span.URI) map[span.URI]*govulncheck.Result {
10401037
m := make(map[span.URI]*govulncheck.Result)
10411038
now := timeNow()
1042-
v.mu.Lock()
1043-
defer v.mu.Unlock()
1039+
v.vulnsMu.Lock()
1040+
defer v.vulnsMu.Unlock()
10441041

10451042
if len(modfiles) == 0 { // empty means all modfiles
10461043
for modfile := range v.vulns {
@@ -1059,8 +1056,8 @@ func (v *View) Vulnerabilities(modfiles ...span.URI) map[span.URI]*govulncheck.R
10591056
}
10601057

10611058
func (v *View) SetVulnerabilities(modfile span.URI, vulns *govulncheck.Result) {
1062-
v.mu.Lock()
1063-
defer v.mu.Unlock()
1059+
v.vulnsMu.Lock()
1060+
defer v.vulnsMu.Unlock()
10641061

10651062
v.vulns[modfile] = vulns
10661063
}

0 commit comments

Comments
 (0)