{
  "id": "go/goroutine-racy-file-write",
  "signature": "fatal error: concurrent file writes (log file)",
  "signature_zh": "致命错误：并发文件写入（日志文件）",
  "regex": "fatal\\ error:\\ concurrent\\ file\\ writes\\ \\(log\\ file\\)",
  "domain": "go",
  "category": "io_error",
  "subcategory": null,
  "root_cause": "Multiple goroutines writing to the same file without synchronization, causing corrupted output.",
  "root_cause_type": "generic",
  "root_cause_zh": "多个协程同时写入同一个文件而没有同步，导致输出损坏。",
  "versions": [
    {
      "version": "1.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "1.22",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Using os.File.Write directly without lock",
      "why_fails": "File writes are not atomic; interleaved writes corrupt data.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "Using bufio.Writer without synchronization",
      "why_fails": "Bufio.Writer buffers writes but still racy when flushing.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Use a mutex to serialize file writes",
      "success_rate": 0.95,
      "how": "var mu sync.Mutex\nf, _ := os.Create(\"log.txt\")\nmu.Lock()\nf.Write([]byte(\"data\"))\nmu.Unlock()",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use a dedicated goroutine to handle all writes via channel",
      "success_rate": 0.9,
      "how": "ch := make(chan string)\ngo func() {\n    f, _ := os.Create(\"log.txt\")\n    defer f.Close()\n    for s := range ch {\n        f.WriteString(s)\n    }\n}()\nch <- \"log entry\"",
      "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.87,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2025-10-01",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}