python data_error ai_generated true

sqlite3.OperationalError: database is locked

ID: python/pytest-parallel-fixture-db-collision

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2025-01-09First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
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

  1. 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()
  2. 90% success
    # docker-compose with postgres
    # pytest.ini
    [pytest]
    addopts = -n auto
    # DATABASE_URL=postgresql://user:pass@localhost/test
  3. 88% success
    pytestmark = pytest.mark.xdist_group(name="db")
    # run: pytest -n auto --dist=loadgroup

Dead Ends

Common approaches that don't work:

  1. 70% fail

    Reduces but does not eliminate lock contention under load.

  2. 60% fail

    Disables parallelism; CI time blows up.

  3. 85% fail

    Autocommit increases contention rather than reducing it.