{
  "id": "python/pytest-teardown-error-masked",
  "signature": "ERROR at teardown of test_create_user\n  File \"tests/conftest.py\", line 40, in db_session\n    session.rollback()\n  sqlalchemy.exc.PendingRollbackError: This Session's transaction has been rolled back due to a previous exception during flush.",
  "signature_zh": "ERROR at teardown of test_create_user\n  File \"tests/conftest.py\", line 40, in db_session\n    session.rollback()\n  sqlalchemy.exc.PendingRollbackError: 由于 flush 期间之前的异常，此 Session 的事务已被回滚。",
  "regex": "ERROR\\ at\\ teardown\\ of\\ test_create_user\\\n\\ \\ File\\ \"tests/conftest\\.py\",\\ line\\ 40,\\ in\\ db_session\\\n\\ \\ \\ \\ session\\.rollback\\(\\)\\\n\\ \\ sqlalchemy\\.exc\\.PendingRollbackError:\\ This\\ Session's\\ transaction\\ has\\ been\\ rolled\\ back\\ due\\ to\\ a\\ previous\\ exception\\ during\\ flush\\.",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "The test left the SQLAlchemy session in a failed state (unhandled exception during flush); the fixture teardown then calls rollback/close on a session that is already invalid, producing a teardown ERROR that masks the original failure.",
  "root_cause_type": "generic",
  "root_cause_zh": "测试使 SQLAlchemy session 处于失败状态（flush 期间未处理异常）；fixture 清理时对已失效的 session 调用 rollback/close，产生 teardown ERROR 掩盖了原始失败。",
  "versions": [
    {
      "version": "2.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Hides the real error and leaves the session dirty, cascading failures into later tests.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Does not clear the failed transaction state; rollback still raises.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.9,
      "how": "Use a nested transaction + savepoint pattern and always roll back cleanly:\n@pytest.fixture\ndef db_session(engine):\n    conn = engine.connect()\n    trans = conn.begin()\n    session = Session(bind=conn)\n    try:\n        yield session\n    finally:\n        session.close()\n        trans.rollback()\n        conn.close()",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.85,
      "how": "Or use `session.begin_nested()` and explicitly rollback the savepoint:\n@pytest.fixture\ndef db_session(engine):\n    session = Session(bind=engine)\n    session.begin_nested()\n    yield session\n    session.rollback()\n    session.close()",
      "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-09-14",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}