go runtime_error ai_generated true

fatal error: all goroutines are asleep - deadlock! goroutine 1 [sync.Mutex.Lock]:

ID: go/deadlock-mutex-double-lock

Also available as: JSON · Markdown · 中文
80%Fix Rate
90%Confidence
0Evidence
2024-02-27First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0+ active

Root Cause

A goroutine locks a sync.Mutex twice without unlocking in between, or two goroutines acquire two mutexes in opposite order. The runtime detects a full stop.

generic

中文

一个 goroutine 在未解锁的情况下两次锁定同一个 sync.Mutex,或两个 goroutine 以相反顺序获取两个互斥锁。运行时检测到完全停止。

Workarounds

  1. 96% success
    func (s *Service) Do() {
        s.mu.Lock()
        defer s.mu.Unlock()
        // ...
    }
  2. 93% success
    // Always lock A before B
    func transfer(a, b *Account) {
        if a.id < b.id { a.mu.Lock(); b.mu.Lock() } else { b.mu.Lock(); a.mu.Lock() }
        defer a.mu.Unlock(); defer b.mu.Unlock()
    }

Dead Ends

Common approaches that don't work:

  1. 90% fail

    sync.Mutex has no timeout API; you must restructure the code or use a channel-based lock.

  2. 95% fail

    All runnable goroutines are blocked on the mutex; more Ps do not help.