go runtime_error ai_generated true

fatal error: sync: unlock of unlocked mutex

ID: go/fatal-error-sync-unlock-of-unlocked-mutex

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active — — —
1.21 active — — —

Root Cause

mu.Unlock() is called when the mutex is not held, usually because a defer unlock runs after an early return or a double unlock.

generic

中文

在互斥锁未被持有的情况下调用 mu.Unlock(),通常因为提前返回后 defer unlock 仍执行,或重复解锁。

Workarounds

  1. 95% success
    Use defer mu.Unlock() immediately after mu.Lock() in the same function so they always pair.
  2. 90% success
    Audit all code paths: ensure no early return between Lock and Unlock without defer, and never unlock in a different function than the one that locked.

Dead Ends

Common approaches that don't work:

  1. 98% fail

    This is a fatal runtime error, not a panic; recover cannot catch it and the process exits.

  2. 80% fail

    The flag itself needs synchronization and is often set incorrectly, reintroducing the bug under concurrency.