{
  "id": "go/mutex-copy-value",
  "signature": "panic: sync: mutex is locked (copied value)",
  "signature_zh": "恐慌：同步：互斥锁已锁定（复制值）",
  "regex": "panic:\\ sync:\\ mutex\\ is\\ locked\\ \\(copied\\ value\\)",
  "domain": "go",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "A sync.Mutex (or other sync primitives) is copied by value after being locked. Mutexes should never be copied; copying a locked mutex leads to undefined behavior and can cause panics.",
  "root_cause_type": "generic",
  "root_cause_zh": "sync.Mutex（或其他同步原语）在锁定后按值复制。互斥锁不应被复制；复制已锁定的互斥锁会导致未定义行为，并可能引起恐慌。",
  "versions": [
    {
      "version": "1.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "1.1",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Using a pointer to the mutex but still copying it",
      "why_fails": "If the struct containing the mutex is copied, the mutex is still copied even if it's a pointer field; the pointer itself is copied, but the underlying mutex is shared incorrectly.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    },
    {
      "action": "Ignoring the panic and continuing execution",
      "why_fails": "The panic is fatal and will crash the program; ignoring it is not possible without recovery, and even then the mutex state is corrupted.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Use a pointer to the struct that contains the mutex",
      "success_rate": 0.95,
      "how": "type SafeStruct struct {\n    mu sync.Mutex\n    data int\n}\nfunc NewSafeStruct() *SafeStruct {\n    return &SafeStruct{}\n}",
      "condition": "",
      "sources": []
    },
    {
      "action": "Embed the mutex as a pointer field",
      "success_rate": 0.9,
      "how": "type SafeStruct struct {\n    mu   *sync.Mutex\n    data int\n}\nfunc NewSafeStruct() *SafeStruct {\n    return &SafeStruct{mu: &sync.Mutex{}}\n}",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.85,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-06-15",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}