{
  "id": "python/pytest-parallel-fixture-db-collision",
  "signature": "sqlite3.OperationalError: database is locked",
  "signature_zh": "sqlite3.OperationalError：数据库已锁定",
  "regex": "sqlite3\\.OperationalError:\\ database\\ is\\ locked",
  "domain": "python",
  "category": "data_error",
  "subcategory": null,
  "root_cause": "Multiple xdist workers share the same SQLite file (or the same fixture-created DB path) and contend for write locks.",
  "root_cause_type": "generic",
  "root_cause_zh": "多个 xdist 工作进程共享同一个 SQLite 文件（或同一 fixture 创建的数据库路径）并争用写锁。",
  "versions": [
    {
      "version": "3.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Reduces but does not eliminate lock contention under load.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Disables parallelism; CI time blows up.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Autocommit increases contention rather than reducing it.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "@pytest.fixture\ndef db(tmp_path, worker_id):\n    path = tmp_path / f\"test-{worker_id}.db\"\n    conn = sqlite3.connect(path)\n    yield conn\n    conn.close()",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "# docker-compose with postgres\n# pytest.ini\n[pytest]\naddopts = -n auto\n# DATABASE_URL=postgresql://user:pass@localhost/test",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.88,
      "how": "pytestmark = pytest.mark.xdist_group(name=\"db\")\n# run: pytest -n auto --dist=loadgroup",
      "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": "2025-01-09",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}