# sqlite3.OperationalError: database is locked

- **ID:** `python/pytest-xdist-fixture-not-thread-safe`
- **Domain:** python
- **Category:** system_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## 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.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.5.x | active | — | — |
| 3.6.x | active | — | — |

## 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

- **** — WAL allows concurrent readers but still serializes writers; parallel tests writing cause 'database is locked' to persist. (65% fail)
- **** — Masks flakiness and slows the suite; long transactions still hold locks and eventually exhaust retries. (60% fail)
