python runtime_error ai_generated true

worker 'gw3' crashed while running 'tests/test_db.py::test_insert' replacing crashed worker gw3 ... INTERNALERROR> psycopg2.OperationalError: connection already closed

ID: python/pytest-xdist-worker-crash-fixture-scope

Also available as: JSON · Markdown · 中文
80%Fix Rate
83%Confidence
0Evidence
2024-08-30First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active — — —

Root Cause

A session-scoped fixture holds a database connection that is shared across xdist workers, but each worker runs in a separate process and the connection is not fork-safe.

generic

中文

会话级 fixture 持有跨 xdist worker 共享的数据库连接,但每个 worker 在独立进程运行,连接不是 fork 安全的。

Workarounds

  1. 90% success
    import pytest
    
    @pytest.fixture(scope='function')
    def db_conn():
        conn = psycopg2.connect(dsn)
        yield conn
        conn.close()
    
    # or use worker id: os.environ['PYTEST_XDIST_WORKER']
  2. 85% success
    pytest -n 4 --dist loadscope
    # conftest.py
    import os
    worker = os.environ.get('PYTEST_XDIST_WORKER', 'master')
    DB_NAME = f'test_{worker}'

Dead Ends

Common approaches that don't work:

  1. 85% fail

    More workers means more concurrent connections to a non-fork-safe fixture; crash rate increases.

  2. 65% fail

    Retries mask the fork-safety issue and cause flaky tests under load.