{
  "id": "python/pytest-capture-fd-error",
  "signature": "OSError: [Errno 9] Bad file descriptor\nDuring handling of the above exception, another exception occurred:\nValueError: I/O operation on closed file",
  "signature_zh": "OSError: [Errno 9] 错误的文件描述符\n处理上述异常时又出现另一个异常:\nValueError: 对已关闭文件进行 I/O 操作",
  "regex": "OSError:\\ \\[Errno\\ 9\\]\\ Bad\\ file\\ descriptor\\\nDuring\\ handling\\ of\\ the\\ above\\ exception,\\ another\\ exception\\ occurred:\\\nValueError:\\ I/O\\ operation\\ on\\ closed\\ file",
  "domain": "python",
  "category": "system_error",
  "subcategory": null,
  "root_cause": "A test closed sys.stdout/sys.stderr or a file descriptor that pytest's capture machinery (capfd/capsys) was using. This corrupts pytest's internal fd tracking.",
  "root_cause_type": "generic",
  "root_cause_zh": "某个测试关闭了 sys.stdout/sys.stderr 或 pytest 捕获机制（capfd/capsys）正在使用的文件描述符。这会破坏 pytest 内部的 fd 追踪。",
  "versions": [
    {
      "version": "7.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "8.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Hides the real bug: the test is still closing shared fds; other plugins may still fail.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "The fd is already closed; subsequent capture operations still raise.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.92,
      "how": "Never close sys.stdout/sys.stderr in tests. If needed, use capsys:\n  def test_x(capsys):\n      out = capsys.readouterr().out\nOr save/restore explicitly:\n  saved = sys.stdout\n  try: ...\n  finally: sys.stdout = saved",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.88,
      "how": "For fd-level capture use capfd and avoid closing the underlying fd:\n  def test_x(capfd):\n      out, err = capfd.readouterr()",
      "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.82,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-08-27",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}