{
  "id": "go/cgo-errno-handling",
  "signature": "error: cgo: C function returned error: errno 2",
  "signature_zh": "错误：cgo：C函数返回错误：errno 2",
  "regex": "error:\\ cgo:\\ C\\ function\\ returned\\ error:\\ errno\\ 2",
  "domain": "go",
  "category": "system_error",
  "subcategory": null,
  "root_cause": "C function sets `errno` on failure, but Go code does not check it, leading to silent failures.",
  "root_cause_type": "generic",
  "root_cause_zh": "C函数在失败时设置`errno`，但Go代码未检查，导致静默失败。",
  "versions": [
    {
      "version": "1.18",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "1.19",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Ignoring the return value of C function",
      "why_fails": "No error handling at all.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "Using `C.errno` directly without thread safety",
      "why_fails": "errno is thread-local in C, but Go may not preserve it.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Check return value and use `C.CString` to get error message",
      "success_rate": 0.9,
      "how": "ret := C.someCFunction()\nif ret != 0 {\n    errno := C.errno\n    errStr := C.GoString(C.strerror(errno))\n    return fmt.Errorf(\"C error: %s\", errStr)\n}",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use `cgo.Handle` to capture error in a thread-safe way",
      "success_rate": 0.8,
      "how": "// Not directly applicable; use mutex to protect errno reading",
      "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": "2025-01-10",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}