{
  "id": "python/pytest-fixture-finalizer-exception",
  "signature": "ERROR at teardown of test_x\nRuntimeError: generator didn't stop after throw()\nDuring handling of the above exception, another exception occurred:\nValueError: I/O operation on closed file",
  "signature_zh": "test_x 的 teardown 期间出错\nRuntimeError: 生成器在 throw() 之后未停止\n处理上述异常时又出现另一个异常:\nValueError: 对已关闭文件进行 I/O 操作",
  "regex": "ERROR\\ at\\ teardown\\ of\\ test_x\\\nRuntimeError:\\ generator\\ didn't\\ stop\\ after\\ throw\\(\\)\\\nDuring\\ handling\\ of\\ the\\ above\\ exception,\\ another\\ exception\\ occurred:\\\nValueError:\\ I/O\\ operation\\ on\\ closed\\ file",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "A generator-based fixture swallowed the GeneratorExit/exception in a bare except or a finally that raised, preventing proper teardown. pytest then reports teardown errors that mask the real test result.",
  "root_cause_type": "generic",
  "root_cause_zh": "基于生成器的 fixture 在裸 except 或抛异常的 finally 中吞掉了 GeneratorExit/异常，阻止了正确的 teardown。pytest 随后报告 teardown 错误，掩盖了真正的测试结果。",
  "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": "If the except swallows GeneratorExit, the 'generator didn't stop' error persists.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Loses cleanup; resources leak across tests.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "Never catch BaseException or bare except in a yield fixture. Use try/finally:\n  @pytest.fixture\n  def res():\n      r = acquire()\n      try:\n          yield r\n      finally:\n          r.close()",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "Use addfinalizer for complex cleanup:\n  @pytest.fixture\n  def res(request):\n      r = acquire()\n      request.addfinalizer(r.close)\n      return r",
      "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.86,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-10-04",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}