{
  "id": "python/pytest-xdist-worker-crash-fixture-scope",
  "signature": "worker 'gw3' crashed while running 'tests/test_db.py::test_insert'\nreplacing crashed worker gw3\n...\nINTERNALERROR> psycopg2.OperationalError: connection already closed",
  "signature_zh": "worker 'gw3' 在运行 'tests/test_db.py::test_insert' 时崩溃\n正在替换崩溃的 worker gw3\n...\nINTERNALERROR> psycopg2.OperationalError: 连接已关闭",
  "regex": "worker\\ 'gw3'\\ crashed\\ while\\ running\\ 'tests/test_db\\.py::test_insert'\\\nreplacing\\ crashed\\ worker\\ gw3\\\n\\.\\.\\.\\\nINTERNALERROR>\\ psycopg2\\.OperationalError:\\ connection\\ already\\ closed",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "A session-scoped fixture holds a database connection that is shared across xdist workers, but each worker runs in a separate process and the connection is not fork-safe.",
  "root_cause_type": "generic",
  "root_cause_zh": "会话级 fixture 持有跨 xdist worker 共享的数据库连接，但每个 worker 在独立进程运行，连接不是 fork 安全的。",
  "versions": [
    {
      "version": "3.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "More workers means more concurrent connections to a non-fork-safe fixture; crash rate increases.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Retries mask the fork-safety issue and cause flaky tests under load.",
      "fail_rate": 0.65,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.9,
      "how": "import pytest\n\n@pytest.fixture(scope='function')\ndef db_conn():\n    conn = psycopg2.connect(dsn)\n    yield conn\n    conn.close()\n\n# or use worker id: os.environ['PYTEST_XDIST_WORKER']",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.85,
      "how": "pytest -n 4 --dist loadscope\n# conftest.py\nimport os\nworker = os.environ.get('PYTEST_XDIST_WORKER', 'master')\nDB_NAME = f'test_{worker}'",
      "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.83,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-08-30",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}