python system_error ai_generated true

sqlite3.OperationalError: database is locked

ID: python/pytest-xdist-fixture-not-thread-safe

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-11-18First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.5.x active — — —
3.6.x active — — —

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.

generic

中文

pytest-xdist 在并行 worker 中运行测试,共享单个 SQLite 文件或 session 作用域资源。SQLite 的文件锁在跨进程时冲突。

Workarounds

  1. 94% success
    @pytest.fixture(scope='session')
    def db_path(worker_id):
        return f'/tmp/testdb_{worker_id}.sqlite'
    
    # run: pytest -n auto
  2. 92% success
    @pytest.fixture(scope='session')
    def db(tmp_path_factory, worker_id):
        p = tmp_path_factory.mktemp(worker_id) / 'db.sqlite'
        conn = sqlite3.connect(p)
        init_schema(conn)
        yield conn
        conn.close()

Dead Ends

Common approaches that don't work:

  1. 65% fail

    WAL allows concurrent readers but still serializes writers; parallel tests writing cause 'database is locked' to persist.

  2. 60% fail

    Masks flakiness and slows the suite; long transactions still hold locks and eventually exhaust retries.