-
Notifications
You must be signed in to change notification settings - Fork 4.1k
lint: add check to ensure mutex Unlock() calls are deferred #105366
Copy link
Copy link
Closed
Labels
A-lintersA-observability-infC-enhancementSolution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)O-postmortemOriginated from a Postmortem action item.Originated from a Postmortem action item.db-cy-23quality-fridayA good issue to work on on Quality FridayA good issue to work on on Quality Friday
Description
Is your feature request related to a problem? Please describe.
We recently dealt with some deadlocks due to code written in this style:
func myFunc() error {
// do some work...
p.Lock()
if err := somethingThatCanPanic(); err != nil {
p.Unlock()
return err
}
p.Unlock()
// do some more work...
return nil
}
A safer way to write this code would be something like:
func myFunc() error {
// do some work...
doRiskyWork := func() error {
p.Lock()
defer p.Unlock()
return somethingThatCanPanic()
}
if err := doRiskyWork(); err != nil {
return err
}
// do some more work...
return nil
}
It would be nice to have a lint rule that enforces .Unlock() calls on mutexes use the defer keyword, to help avoid this pitfall in the future (and identify lingering cases of it).
We recognize that in some cases, using defer is not ideal, so we should ensure that programmers have the option to use lint:ignore for this rule. However, as the organization scales, we should set the default expectation to help avoid this pattern from being used in the future.
Jira issue: CRDB-29001
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
A-lintersA-observability-infC-enhancementSolution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)O-postmortemOriginated from a Postmortem action item.Originated from a Postmortem action item.db-cy-23quality-fridayA good issue to work on on Quality FridayA good issue to work on on Quality Friday