Developer ditches pytest-xdist, fixes flaky tests with 60-line subprocess solution
A development team traced persistent flaky test failures in their 17,000-test suite to pytest-xdist's reuse of persistent worker processes, which allowed module-level state to leak between test files. Because xdist workers remain alive across multiple files, shared singletons, caches, and memoized environment variables were inadvertently carried over, causing intermittent failures that were difficult to reproduce. The team replaced xdist with a roughly 60-line custom solution that spawns a fresh Python interpreter for each of their 850 test files using subprocess.Popen managed by a ThreadPoolExecutor. Running one pytest process per file rather than per individual test kept spawn overhead to around 3.5 minutes, fitting within CI time budgets while eliminating cross-file state contamination. A key implementation challenge was ensuring timed-out subprocesses were fully killed, including grandchild processes such as servers, requiring explicit process-group termination on both Linux and Windows.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in