go runtime_error ai_generated true

恐慌:sync: Mutex 已锁定(复制了 sync.Mutex 的值)

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

ID: go/mutex-copy-after-use

其他格式: JSON · Markdown 中文 · English
80%修复率
87%置信度
0证据数
2024-02-20首次发现

版本兼容性

版本状态引入弃用备注
1.19 active
1.21 active

根因分析

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

English

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

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. 90% 失败

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

  2. 80% 失败

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