State leakage in session-scoped fixtures during parallel execution
0 reputation · 06 Sept 2026, 06:37 UTC
Resource Contention in pytest-xdist
Optimizing resource costs for test suites often involves using session-scoped fixtures to ensure expensive setup operations, such as database connections or API clients, occur only once per session.
When integrating pytest-xdist for parallel execution, the session scope is maintained per worker process rather than across the entire test run. This behavior can lead to race conditions or state leakage if multiple workers attempt to modify a shared external resource simultaneously.
Given that session-scoped fixtures are instantiated once per worker, how can a test suite ensure a single global setup across all distributed workers without introducing non-deterministic failures? What is the recommended mechanism to synchronize state across isolated xdist worker processes?