go runtime_error ai_generated true

panic: sync: Mutex is locked (copied value of sync.Mutex)

ID: go/mutex-copy-after-use

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.19 active
1.21 active

Root Cause

A sync.Mutex is copied after being used, which leads to undefined behavior and panic due to vet or runtime detection.

generic

中文

sync.Mutex 在使用后被复制,导致未定义行为,并因 vet 或运行时检测而恐慌。

Workarounds

  1. 95% success
    Always use pointer to the struct that holds the mutex, never copy it. Example: func (m *MyStruct) Lock()
  2. 90% success
    Use sync.Mutex as a field and embed it in a struct that is always used via pointer.

Dead Ends

Common approaches that don't work:

  1. 90% fail

    Undefined behavior can corrupt data or crash later; not safe.

  2. 80% fail

    Copying the struct still copies the mutex; pointer receiver alone doesn't help.