{
  "id": "go/mutex-lock-already-held",
  "signature": "fatal error: sync: unlock of unlocked mutex",
  "signature_zh": "致命错误：对未锁定的互斥锁执行解锁操作",
  "regex": "fatal error: sync: unlock of unlocked mutex",
  "domain": "go",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Calling Unlock() on a sync.Mutex that was not locked by the same goroutine, or double-unlocking a mutex.",
  "root_cause_type": "generic",
  "root_cause_zh": "在同一个协程中对未锁定的 sync.Mutex 调用 Unlock()，或者对同一个互斥锁重复解锁。",
  "versions": [
    {
      "version": "Go 1.18",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Go 1.19",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Go 1.20",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Go 1.21",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Go 1.22",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Defer runs even if the lock was never acquired (e.g., due to early return), causing an unlock of an unlocked mutex.",
      "fail_rate": 0.75,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "The Go runtime checks that the unlocking goroutine is the same one that locked it; cross-goroutine unlocks panic.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Always pair Lock() and Unlock() in the same function, using defer immediately after Lock() to avoid mismatches",
      "success_rate": 0.95,
      "how": "Always pair Lock() and Unlock() in the same function, using defer immediately after Lock() to avoid mismatches",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use sync.Mutex.TryLock() (Go 1.18+) to check if lock is held before unlocking, but prefer structured patterns",
      "success_rate": 0.85,
      "how": "Use sync.Mutex.TryLock() (Go 1.18+) to check if lock is held before unlocking, but prefer structured patterns",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "始终在同一个函数中配对 Lock() 和 Unlock()，在 Lock() 后立即使用 defer 以避免不匹配",
    "使用 sync.Mutex.TryLock()（Go 1.18+）在解锁前检查锁是否被持有，但推荐使用结构化模式"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://pkg.go.dev/sync#Mutex",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.85,
  "fix_success_rate": 0.85,
  "resolvable": "true",
  "first_seen": "2023-01-15",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}