python
runtime_error
ai_generated
true
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) 数据库已锁定
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) database is locked
ID: python/pytest-parallel-db-fixture-conflict
80%修复率
87%置信度
0证据数
2025-11-30首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 7.x | active | — | — | — |
| 8.x | active | — | — | — |
根因分析
多个 pytest-xdist worker 共享同一个 SQLite 文件;并发写入导致锁竞争和 OperationalError。
English
Multiple pytest-xdist workers share the same SQLite file; concurrent writes cause lock contention and OperationalError.
解决方案
-
95% 成功率
@pytest.fixture(scope='session') def db_url(tmp_path_factory, worker_id): if worker_id == 'master': path = tmp_path_factory.mktemp('db') / 'test.db' else: path = tmp_path_factory.mktemp(f'db_{worker_id}') / 'test.db' return f'sqlite:///{path}' -
92% 成功率
# docker-compose.yml provides postgres # conftest.py @pytest.fixture(scope='session') def db_url(worker_id): return f'postgresql://test:test@localhost/test_{worker_id}'
无效尝试
常见但无效的做法:
-
70% 失败
Reduces frequency but not correctness; long-running transactions still deadlock and CI slows down.
-
90% 失败
Defeats the purpose of parallel tests; CI time returns to serial levels.
-
75% 失败
WAL helps readers but writers still serialize; contention remains under heavy xdist load.