python
data_error
ai_generated
true
sqlite3.OperationalError: database is locked
ID: python/pytest-parallel-fixture-db-collision
80%Fix Rate
86%Confidence
0Evidence
2025-01-09First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
Multiple xdist workers share the same SQLite file (or the same fixture-created DB path) and contend for write locks.
generic中文
多个 xdist 工作进程共享同一个 SQLite 文件(或同一 fixture 创建的数据库路径)并争用写锁。
Workarounds
-
95% success
@pytest.fixture def db(tmp_path, worker_id): path = tmp_path / f"test-{worker_id}.db" conn = sqlite3.connect(path) yield conn conn.close() -
90% success
# docker-compose with postgres # pytest.ini [pytest] addopts = -n auto # DATABASE_URL=postgresql://user:pass@localhost/test
-
88% success
pytestmark = pytest.mark.xdist_group(name="db") # run: pytest -n auto --dist=loadgroup
Dead Ends
Common approaches that don't work:
-
70% fail
Reduces but does not eliminate lock contention under load.
-
60% fail
Disables parallelism; CI time blows up.
-
85% fail
Autocommit increases contention rather than reducing it.