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
}

pkg/ccl/changefeedccl/kvevent/blocking_buffer.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ func (b *blockingBuffer) pop() (e Event, ok bool, err error) {
150150
func (b *blockingBuffer) notifyOutOfQuota(canFlush bool) {
151151
b.mu.Lock()
152152
b.mu.canFlush = canFlush
153-
// nolint:deferunlock
154153
b.mu.Unlock()
155154

156155
if canFlush {
@@ -166,7 +165,6 @@ func (b *blockingBuffer) notifyOutOfQuota(canFlush bool) {
166165
func (b *blockingBuffer) producerBlocked() {
167166
b.mu.Lock()
168167
b.mu.numBlocked++
169-
// nolint:deferunlock
170168
b.mu.Unlock()
171169
}
172170

@@ -185,7 +183,6 @@ func (b *blockingBuffer) quotaAcquiredAfterWait() {
185183
// Clear out canFlush since we know that producers no longer blocked.
186184
b.mu.canFlush = false
187185
}
188-
// nolint:deferunlock
189186
b.mu.Unlock()
190187
}
191188

pkg/ccl/changefeedccl/metrics.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,6 @@ func MakeMetrics(histogramWindow time.Duration) metric.Struct {
848848
maxBehind = behind
849849
}
850850
}
851-
// nolint:deferunlock
852851
m.mu.Unlock()
853852
return maxBehind.Nanoseconds()
854853
})

pkg/ccl/changefeedccl/schema_registry.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ func newConfluentSchemaRegistry(
148148
}
149149
schemaRegistrySingletons.cachePerEndpoint[baseURL] = src
150150
}
151-
// nolint:deferunlock
152151
schemaRegistrySingletons.mu.Unlock()
153152

154153
s, err := getAndDeleteParams(u)

pkg/ccl/changefeedccl/schemafeed/schema_feed.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,6 @@ func (tf *schemaFeed) Run(ctx context.Context) error {
280280
func (tf *schemaFeed) primeInitialTableDescs(ctx context.Context) error {
281281
tf.mu.Lock()
282282
initialTableDescTs := tf.mu.highWater
283-
// nolint:deferunlock
284283
tf.mu.Unlock()
285284
var initialDescs []catalog.Descriptor
286285

@@ -313,7 +312,6 @@ func (tf *schemaFeed) primeInitialTableDescs(ctx context.Context) error {
313312
tbl := desc.(catalog.TableDescriptor)
314313
tf.mu.typeDeps.ingestTable(tbl)
315314
}
316-
// nolint:deferunlock
317315
tf.mu.Unlock()
318316

319317
return tf.ingestDescriptors(ctx, hlc.Timestamp{}, initialTableDescTs, initialDescs, tf.validateDescriptor)
@@ -512,7 +510,6 @@ func (tf *schemaFeed) pauseOrResumePolling(
512510
func (tf *schemaFeed) highWater() hlc.Timestamp {
513511
tf.mu.Lock()
514512
highWater := tf.mu.highWater
515-
// nolint:deferunlock
516513
tf.mu.Unlock()
517514
return highWater
518515
}

pkg/ccl/changefeedccl/sink_kafka.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ func (l *maybeLocker) Lock() {
5252
}
5353
func (l *maybeLocker) Unlock() {
5454
if l.locked {
55-
// nolint:deferunlock
5655
l.wrapped.Unlock()
5756
l.locked = false
5857
}
@@ -422,7 +421,6 @@ func (s *kafkaSink) Flush(ctx context.Context) error {
422421
if !immediateFlush {
423422
s.mu.flushCh = flushCh
424423
}
425-
// nolint:deferunlock
426424
s.mu.Unlock()
427425

428426
if immediateFlush {
@@ -439,7 +437,6 @@ func (s *kafkaSink) Flush(ctx context.Context) error {
439437
s.mu.Lock()
440438
flushErr := s.mu.flushErr
441439
s.mu.flushErr = nil
442-
// nolint:deferunlock
443440
s.mu.Unlock()
444441
return flushErr
445442
}

pkg/ccl/changefeedccl/sink_pubsub_v2.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,9 @@ func (sc *pubsubSinkClient) maybeCreateTopic(topic string) error {
142142
sc.mu.RLock()
143143
_, ok := sc.mu.topicCache[topic]
144144
if ok {
145-
// nolint:deferunlock
146145
sc.mu.RUnlock()
147146
return nil
148147
}
149-
// nolint:deferunlock
150148
sc.mu.RUnlock()
151149
sc.mu.Lock()
152150
defer sc.mu.Unlock()

pkg/ccl/changefeedccl/sink_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ func (p *asyncProducerMock) consume() (cleanup func()) {
177177
case m := <-p.inputCh:
178178
p.mu.Lock()
179179
p.mu.outstanding = append(p.mu.outstanding, m)
180-
// nolint:deferunlock
181180
p.mu.Unlock()
182181
}
183182
}
@@ -196,7 +195,6 @@ func (p *asyncProducerMock) acknowledge(n int, ch chan *sarama.ProducerMessage)
196195
p.mu.Lock()
197196
outstanding = append(outstanding, p.mu.outstanding...)
198197
p.mu.outstanding = p.mu.outstanding[:0]
199-
// nolint:deferunlock
200198
p.mu.Unlock()
201199

202200
for _, m := range outstanding {

pkg/ccl/multiregionccl/datadriven_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,10 @@ SET CLUSTER SETTING kv.allocator.min_lease_transfer_interval = 5m
249249
// Setup tracing for the input.
250250
mu.Lock()
251251
traceStmt = d.Input
252-
// nolint:deferunlock
253252
mu.Unlock()
254253
defer func() {
255254
mu.Lock()
256255
traceStmt = ""
257-
// nolint:deferunlock
258256
mu.Unlock()
259257
}()
260258

pkg/ccl/multiregionccl/region_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,11 @@ func TestConcurrentAddDropRegions(t *testing.T) {
199199
if firstOp {
200200
firstOp = false
201201
close(firstOpStarted)
202-
// nolint:deferunlock
203202
mu.Unlock()
204203
// Don't promote any members before the second operation reaches
205204
// the schema changer as well.
206205
<-secondOpFinished
207206
} else {
208-
// nolint:deferunlock
209207
mu.Unlock()
210208
}
211209
return nil

pkg/ccl/multitenantccl/tenantcostclient/tenant_side.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,6 @@ func (c *tenantSideCostController) onTick(ctx context.Context, newTime time.Time
441441
c.mu.consumption.PGWireEgressBytes += deltaPGWireEgressBytes
442442
c.mu.consumption.RU += float64(ru)
443443
newConsumption := c.mu.consumption
444-
// nolint:deferunlock
445444
c.mu.Unlock()
446445

447446
// Update the average RUs consumed per second, based on the latest stats.
@@ -880,7 +879,6 @@ func (c *tenantSideCostController) onExternalIO(
880879
if c.shouldAccountForExternalIORUs() {
881880
c.mu.consumption.RU += float64(totalRU)
882881
}
883-
// nolint:deferunlock
884882
c.mu.Unlock()
885883

886884
return nil

0 commit comments

Comments
 (0)