{
  "id": "python/pytest-teardown-error-generator",
  "signature": "ERROR at teardown of test_create_user\n  File \"...\", line ..., in db_session\n    session.close()\nRuntimeError: generator didn't stop after throw()",
  "signature_zh": "test_create_user 的 teardown 出错\n  文件 \"...\"，行 ...，在 db_session 中\n    session.close()\nRuntimeError: generator didn't stop after throw()",
  "regex": "ERROR\\ at\\ teardown\\ of\\ test_create_user\\\n\\ \\ File\\ \"\\.\\.\\.\",\\ line\\ \\.\\.\\.,\\ in\\ db_session\\\n\\ \\ \\ \\ session\\.close\\(\\)\\\nRuntimeError:\\ generator\\ didn't\\ stop\\ after\\ throw\\(\\)",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "A yield-based fixture raised or yielded again after the test injected an exception. Typically caused by a `try/finally` that yields inside `finally`, or a generator fixture that catches exceptions and yields again.",
  "root_cause_type": "generic",
  "root_cause_zh": "基于 yield 的 fixture 在测试注入异常后再次 yield 或抛出异常。通常由在 `finally` 内 yield 的 `try/finally`，或捕获异常后再次 yield 的生成器 fixture 引起。",
  "versions": [
    {
      "version": "3.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "pytest requires the generator to stop after the exception is thrown; a second yield triggers the same error.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Loses teardown entirely; DB session/resources are never closed, causing connection leaks.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "import pytest\n\n@pytest.fixture\ndef db_session():\n    session = SessionLocal()\n    try:\n        yield session\n    finally:\n        session.close()  # no yield here",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "import contextlib\n\n@contextlib.contextmanager\ndef db_session():\n    s = SessionLocal()\n    try:\n        yield s\n    finally:\n        s.close()\n\n# then in fixture\n@pytest.fixture\ndef db():\n    with db_session() as s:\n        yield s",
      "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-09-17",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}