{
  "id": "python/pytest-xdist-fixture-not-thread-safe",
  "signature": "sqlite3.OperationalError: database is locked",
  "signature_zh": "sqlite3.OperationalError: 数据库已锁定",
  "regex": "sqlite3\\.OperationalError:\\ database\\ is\\ locked",
  "domain": "python",
  "category": "system_error",
  "subcategory": null,
  "root_cause": "pytest-xdist runs tests in parallel workers sharing a single SQLite file or session-scoped resource. SQLite's file lock conflicts across processes.",
  "root_cause_type": "generic",
  "root_cause_zh": "pytest-xdist 在并行 worker 中运行测试，共享单个 SQLite 文件或 session 作用域资源。SQLite 的文件锁在跨进程时冲突。",
  "versions": [
    {
      "version": "3.5.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.6.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "WAL allows concurrent readers but still serializes writers; parallel tests writing cause 'database is locked' to persist.",
      "fail_rate": 0.65,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Masks flakiness and slows the suite; long transactions still hold locks and eventually exhaust retries.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.94,
      "how": "@pytest.fixture(scope='session')\ndef db_path(worker_id):\n    return f'/tmp/testdb_{worker_id}.sqlite'\n\n# run: pytest -n auto",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.92,
      "how": "@pytest.fixture(scope='session')\ndef db(tmp_path_factory, worker_id):\n    p = tmp_path_factory.mktemp(worker_id) / 'db.sqlite'\n    conn = sqlite3.connect(p)\n    init_schema(conn)\n    yield conn\n    conn.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.86,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-11-18",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}