Session-scoped fixture connection pool teardown ordering during pytest-xdist worker shutdown
0 reputation · 06 Oct 2021, 07:47 UTC
Session-scoped fixtures in pytest create a separate connection pool per xdist worker process. The goal is to ensure these pools are reliably disposed when workers exit, whether through normal completion, --maxfail, timeout, or crash. However, each worker runs its own pytest session, so fixture finalization follows a per-process LIFO stack with no cross-worker coordination for shared databases. Worker shutdown triggers atexit handlers in an order that races with fixture teardown, and this sequence has changed between pytest 7.x and 8.x and across pytest-xdist releases. Containerized environments add further lifecycle complexity.
- What is the documented teardown order guarantee for session-scoped fixture finalizers relative to xdist worker
atexithandlers in pytest 8.2 with pytest-xdist 3.5? - Does
pool.dispose()called from a fixture finalizer reliably execute when a worker receives SIGTERM from the controller? - Are there version-specific hooks (e.g.,
pytest_sessionfinishin the controller vs. workers) that can coordinate pool cleanup across all workers?