Skip to content

Commit d80a9f5

Browse files
committed
lint: make a smarter mutex lock linter
This commit udpates the lock linter to work by keeping track of locks and if we find any if conditions, loops, or function calls before finding a matching unlock it will report. It will also report if the matching unlock is >5 lines away from the lock. It will ignore cases where a `nolint:deferunlock` is present. Release note: None
1 parent 8368312 commit d80a9f5

File tree

285 files changed

+573
-999
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

285 files changed

+573
-999
lines changed

build/bazelutil/nogo_config.json

Lines changed: 156 additions & 83 deletions
Large diffs are not rendered by default.

pkg/acceptance/localcluster/cluster.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,6 @@ func (n *Node) Alive() bool {
475475
func (n *Node) StatusClient(ctx context.Context) serverpb.StatusClient {
476476
n.Lock()
477477
existingClient := n.statusClient
478-
// nolint:deferunlock
479478
n.Unlock()
480479

481480
if existingClient != nil {
@@ -589,7 +588,6 @@ func (n *Node) startAsyncInnerLocked(ctx context.Context, joins ...string) error
589588
_ = errors.As(waitErr, &execErr)
590589
n.Lock()
591590
n.setNotRunningLocked(execErr)
592-
// nolint:deferunlock
593591
n.Unlock()
594592
}(n.cmd)
595593

@@ -697,7 +695,6 @@ func (n *Node) waitUntilLive(dur time.Duration) error {
697695
if n.cmd != nil {
698696
pid = n.cmd.Process.Pid
699697
}
700-
// nolint:deferunlock
701698
n.Unlock()
702699
if pid == 0 {
703700
log.Info(ctx, "process already quit")
@@ -720,14 +717,12 @@ func (n *Node) waitUntilLive(dur time.Duration) error {
720717
if n.Cfg.RPCPort == 0 {
721718
n.Lock()
722719
n.rpcPort = pgURL.Port()
723-
// nolint:deferunlock
724720
n.Unlock()
725721
}
726722

727723
pgURL.Path = n.Cfg.DB
728724
n.Lock()
729725
n.pgURL = pgURL.String()
730-
// nolint:deferunlock
731726
n.Unlock()
732727

733728
var uiURL *url.URL
@@ -744,7 +739,6 @@ func (n *Node) waitUntilLive(dur time.Duration) error {
744739
// http port is required but isn't initialized yet.
745740
n.Lock()
746741
n.db = makeDB(n.pgURL, n.Cfg.NumWorkers, n.Cfg.DB)
747-
// nolint:deferunlock
748742
n.Unlock()
749743

750744
{
@@ -775,7 +769,6 @@ func (n *Node) Kill() {
775769
for ok := false; !ok; {
776770
n.Lock()
777771
ok = n.cmd == nil
778-
// nolint:deferunlock
779772
n.Unlock()
780773
}
781774
}
@@ -811,7 +804,6 @@ func (n *Node) Signal(s os.Signal) {
811804
func (n *Node) Wait() *exec.ExitError {
812805
n.Lock()
813806
ch := n.notRunning
814-
// nolint:deferunlock
815807
n.Unlock()
816808
if ch == nil {
817809
log.Warning(context.Background(), "(*Node).Wait called when node was not running")

pkg/ccl/auditloggingccl/audit_log_config.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ func UpdateAuditConfigOnChange(
8585
}
8686
acl.Lock()
8787
acl.Config = config
88-
// nolint:deferunlock
8988
acl.Unlock()
9089
}
9190

pkg/ccl/backupccl/backup_test.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2593,11 +2593,9 @@ func TestBackupRestoreDuringUserDefinedTypeChange(t *testing.T) {
25932593
if numTypeChangesStarted == len(tc.queries) {
25942594
close(typeChangesStarted)
25952595
}
2596-
// nolint:deferunlock
25972596
mu.Unlock()
25982597
<-waitForBackup
25992598
} else {
2600-
// nolint:deferunlock
26012599
mu.Unlock()
26022600
}
26032601
return nil
@@ -2626,7 +2624,6 @@ CREATE TYPE d.greeting AS ENUM ('hello', 'howdy', 'hi');
26262624
if numTypeChangesFinished == totalQueries {
26272625
close(typeChangesFinished)
26282626
}
2629-
// nolint:deferunlock
26302627
mu.Unlock()
26312628
}(query, len(tc.queries))
26322629
}
@@ -6208,7 +6205,6 @@ func TestRestoreErrorPropagates(t *testing.T) {
62086205

62096206
jobsTableKey.Lock()
62106207
jobsTableKey.key = tc.ApplicationLayer(0).Codec().TablePrefix(uint32(systemschema.JobsTable.GetID()))
6211-
// nolint:deferunlock
62126208
jobsTableKey.Unlock()
62136209

62146210
runner.Exec(t, `SET CLUSTER SETTING jobs.metrics.interval.poll = '30s'`)
@@ -6391,7 +6387,6 @@ INSERT INTO foo.bar VALUES (110), (210), (310), (410), (510)`)
63916387
startingSpan := mkSpan(id1, "/Tenant/10/Table/:id/1", "/Tenant/10/Table/:id/2")
63926388
mu.Lock()
63936389
require.Equal(t, []string{startingSpan.String()}, mu.exportRequestSpans)
6394-
// nolint:deferunlock
63956390
mu.Unlock()
63966391
resetStateVars()
63976392

@@ -6402,7 +6397,6 @@ INSERT INTO foo.bar VALUES (110), (210), (310), (410), (510)`)
64026397
resumeSpan := mkSpan(id1, "/Tenant/10/Table/:id/1/510/0", "/Tenant/10/Table/:id/2")
64036398
mu.Lock()
64046399
require.Equal(t, []string{startingSpan.String(), resumeSpan.String()}, mu.exportRequestSpans)
6405-
// nolint:deferunlock
64066400
mu.Unlock()
64076401
resetStateVars()
64086402

@@ -6421,7 +6415,6 @@ INSERT INTO foo.bar VALUES (110), (210), (310), (410), (510)`)
64216415
}
64226416
mu.Lock()
64236417
require.Equal(t, expected, mu.exportRequestSpans)
6424-
// nolint:deferunlock
64256418
mu.Unlock()
64266419
resetStateVars()
64276420

@@ -6455,7 +6448,6 @@ INSERT INTO baz.bar VALUES (110, 'a'), (210, 'b'), (310, 'c'), (410, 'd'), (510,
64556448
}
64566449
mu.Lock()
64576450
require.Equal(t, expected, mu.exportRequestSpans)
6458-
// nolint:deferunlock
64596451
mu.Unlock()
64606452
resetStateVars()
64616453

pkg/ccl/backupccl/backupinfo/manifest_handling.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1541,7 +1541,6 @@ func GetBackupManifests(
15411541
manifests[i] = desc
15421542
}
15431543
subMem.Shrink(ctx, size)
1544-
// nolint:deferunlock
15451544
memMu.Unlock()
15461545

15471546
return err

pkg/ccl/backupccl/restore_job.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,6 @@ func restore(
318318
introducedSpanFrontier,
319319
targetRestoreSpanSize.Get(&execCtx.ExecCfg().Settings.SV),
320320
progressTracker.useFrontier)
321-
// nolint:deferunlock
322321
progressTracker.mu.Unlock()
323322
if err != nil {
324323
return roachpb.RowCount{}, err
@@ -2154,7 +2153,6 @@ func insertStats(
21542153
mu.completedBatches++
21552154
remainingBatches := totalNumBatches - mu.completedBatches
21562155
completedBatches := mu.completedBatches
2157-
// nolint:deferunlock
21582156
mu.Unlock()
21592157
if insertStatsProgress.ShouldLog() {
21602158
logStatsProgress(remainingBatches, completedBatches)

pkg/ccl/backupccl/tenant_backup_nemesis_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,15 +428,13 @@ func (r *randomBackupNemesis) Stop() {
428428
func (r *randomBackupNemesis) TablesToCheck() []string {
429429
r.mu.Lock()
430430
ret := append([]string(nil), r.mu.tablesToCheck...)
431-
// nolint:deferunlock
432431
r.mu.Unlock()
433432
return ret
434433
}
435434

436435
func (r *randomBackupNemesis) addTable(name string) {
437436
r.mu.Lock()
438437
r.mu.tablesToCheck = append(r.mu.tablesToCheck, name)
439-
// nolint:deferunlock
440438
r.mu.Unlock()
441439
}
442440

pkg/ccl/changefeedccl/cdctest/schema_registry.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,6 @@ func (r *SchemaRegistry) EncodedAvroToNative(b []byte) (interface{}, error) {
207207

208208
r.mu.Lock()
209209
jsonSchema := r.mu.schemas[id]
210-
// nolint:deferunlock
211210
r.mu.Unlock()
212211
codec, err := goavro.NewCodec(jsonSchema)
213212
if err != nil {

pkg/ccl/changefeedccl/changefeed_processors.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,7 +1187,6 @@ func (cf *changeFrontier) Start(ctx context.Context) {
11871187
cf.metricsID = cf.metrics.mu.id
11881188
cf.metrics.mu.id++
11891189
sli.RunningCount.Inc(1)
1190-
// nolint:deferunlock
11911190
cf.metrics.mu.Unlock()
11921191

11931192
cf.sliMetricsID = cf.sliMetrics.claimId()
@@ -1230,7 +1229,6 @@ func (cf *changeFrontier) closeMetrics() {
12301229
}
12311230
delete(cf.metrics.mu.resolved, cf.metricsID)
12321231
cf.metricsID = -1
1233-
// nolint:deferunlock
12341232
cf.metrics.mu.Unlock()
12351233

12361234
cf.sliMetrics.closeId(cf.sliMetricsID)
@@ -1383,7 +1381,6 @@ func (cf *changeFrontier) forwardFrontier(resolved jobspb.ResolvedSpan) error {
13831381
if cf.metricsID != -1 {
13841382
cf.metrics.mu.resolved[cf.metricsID] = newResolved
13851383
}
1386-
// nolint:deferunlock
13871384
cf.metrics.mu.Unlock()
13881385

13891386
return cf.maybeEmitResolved(newResolved)

pkg/ccl/changefeedccl/event_processing.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -646,15 +646,13 @@ func (c *parallelEventConsumer) incInFlight() {
646646
c.mu.Lock()
647647
c.mu.inFlight++
648648
c.metrics.ParallelConsumerInFlightEvents.Update(int64(c.mu.inFlight))
649-
// nolint:deferunlock
650649
c.mu.Unlock()
651650
}
652651

653652
func (c *parallelEventConsumer) decInFlight() {
654653
c.mu.Lock()
655654
c.mu.inFlight--
656655
notifyFlush := c.mu.waiting && c.mu.inFlight == 0
657-
// nolint:deferunlock
658656
c.mu.Unlock()
659657

660658
// If someone is waiting on a flush, signal to them
@@ -708,7 +706,6 @@ func (c *parallelEventConsumer) Flush(ctx context.Context) error {
708706
c.mu.Lock()
709707
c.mu.waiting = false
710708
c.mu.flushFrontier = c.spanFrontier.Frontier()
711-
// nolint:deferunlock
712709
c.mu.Unlock()
713710
return nil
714711
}

0 commit comments

Comments
 (0)