{
  "id": "python/pytest-xdist-shared-state",
  "signature": "AssertionError: assert 1 == 2\n# passes with -p no:xdist, fails with -n auto",
  "signature_zh": "AssertionError: assert 1 == 2\n# 使用 -p no:xdist 时通过，使用 -n auto 时失败",
  "regex": "AssertionError:\\ assert\\ 1\\ ==\\ 2\\\n\\#\\ passes\\ with\\ \\-p\\ no:xdist,\\ fails\\ with\\ \\-n\\ auto",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Tests share global state (module-level caches, singletons, files) that is safe serially but races or persists across worker processes under pytest-xdist.",
  "root_cause_type": "generic",
  "root_cause_zh": "测试共享全局状态（模块级缓存、单例、文件），在串行下安全，但在 pytest-xdist 的多个 worker 进程下产生竞态或持久化。",
  "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": "Masks the race condition; reruns may pass by luck and hide real bugs, and flaky plugin adds overhead.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "More workers means more contention, making the race more likely, not less.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Slows tests and does not eliminate the race; still fails intermittently on slow CI machines.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.9,
      "how": "@pytest.fixture(autouse=True)\ndef reset_state():\n    myapp.cache.clear()\n    yield\n    myapp.cache.clear()",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.88,
      "how": "def test_db(tmp_path, worker_id):\n    if worker_id == 'master':\n        db = tmp_path / 'db.sqlite'\n    else:\n        db = tmp_path / f'db_{worker_id}.sqlite'\n    ...",
      "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.85,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-10-21",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}