A mutex is locked again by the code that holds it, which blocks forever

tx0/go:double-lockdefaultwarningbug, concurrency

What it finds

``go s.mu.Lock() n := len(s.items) s.mu.Lock() ` The mutexes of sync` are not reentrant: locking one that the same goroutine already holds waits forever. A read lock taken twice can deadlock as well, as soon as a writer waits in between, so it is reported too.

Reported is a Lock or RLock statement on the same receiver as an earlier one in the same block, when the statements between them are plain assignments, declarations and expression statements that call nothing but builtins such as len or append and receive from no channel. Any other call might release the lock, and another goroutine might while this one waits, so the search stops there.

From the rule's source, double_lock.rs.

Example from the rule's tests
package p import "sync" type store struct {	mu    sync.Mutex	items []int} func (s *store) add(v int) {	s.mu.Lock()	n := len(s.items)	s.mu.Lock()tx0 reports: s.mu is locked again while it is held	s.items = append(s.items[:n], v)	s.mu.Unlock()} func (s *store) reset() {	s.mu.Lock()	s.items = nil	s.mu.Unlock()	s.mu.Lock()	s.items = nil	s.unlock()	s.mu.Lock()} func (s *store) unlock() { s.mu.Unlock() }
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/go:double-lock" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/go:double-lock] reason="..."

tx0 explain tx0/go:double-lock shows the same in the terminal. Found a false positive? Report it.