{
  "id": "go/too-many-open-files-in-goroutine",
  "signature": "panic: runtime error: too many open files (in goroutine)",
  "signature_zh": "恐慌：运行时错误：打开的文件过多（在 goroutine 中）",
  "regex": "panic:\\ runtime\\ error:\\ too\\ many\\ open\\ files\\ \\(in\\ goroutine\\)",
  "domain": "go",
  "category": "io_error",
  "subcategory": null,
  "root_cause": "A goroutine is opening many file descriptors without closing them, exhausting the system limit. This can happen in concurrent programs that handle many connections or files.",
  "root_cause_type": "generic",
  "root_cause_zh": "goroutine 打开了大量文件描述符但没有关闭，耗尽了系统限制。这可能在处理大量连接或文件的并发程序中发生。",
  "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"
    },
    {
      "version": "1.2",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Increasing the system ulimit without fixing the code",
      "why_fails": "This only delays the problem; if the code doesn't close files properly, the new limit will also be exhausted eventually.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "Using defer to close files but not handling errors",
      "why_fails": "defer ensures close is called, but if multiple goroutines open files concurrently, the defer may not execute quickly enough, leading to file descriptor exhaustion.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Use a semaphore to limit concurrent file operations",
      "success_rate": 0.9,
      "how": "sem := make(chan struct{}, 10) // limit 10 concurrent opens\nfor i := 0; i < 100; i++ {\n    go func() {\n        sem <- struct{}{}\n        defer func() { <-sem }()\n        f, err := os.Open(\"file.txt\")\n        if err != nil {\n            log.Fatal(err)\n        }\n        defer f.Close()\n        // process file\n    }()\n}",
      "condition": "",
      "sources": []
    },
    {
      "action": "Ensure all files are closed in a timely manner",
      "success_rate": 0.95,
      "how": "f, err := os.Open(\"file.txt\")\nif err != nil {\n    return err\n}\ndefer f.Close()\n// use f",
      "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-09-12",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}