Repository navigation
Description
Similar to pytest-xdist: https://pytest-xdist.readthedocs.io/en/stable/how-to.html
I'm not sure yet how to implement this in mutmut, as the pytest setup is different (in pytest-xdist, session fixtures are run once per worker. Not sure how we could replicate this use case in mutmut)
I have a project which connects to a local postgres instance for integration tests. When running tests in parallel, they should connect to different databases on this instance, otherwise they would conflict with each other (e.g. test A deletes data from some table, while test B tries to read from this table). This means I currently cannot run these tests with mutmut.
The setup with pytest-xdist is:
- start e.g. 12 processes with
PYTEST_XDIST_WORKERset togw1,gw2, ..., gw12` - for each process, it runs the pytest session fixture to setup the database
2.1. The fixture connects to postgres and creates a databasetest_db_{PYTEST_XDIST_WORKER}
2.2. Afterwards, we have 12 different databases - When running a test, it runs against the
test_db_{PYTEST_XDIST_WORKER}database
Therefore, each worker uses its own database.
I think something like step (1) would be rather straightforward: We use fork and do not have explicit workers, but we run at most n processes in parallel. We could create n ids, keep track of which ids are still free, and everytime we fork another process we take one of the free ids.
However, I'm not sure how we would go about running session fixtures once per "worker". I suppose, we would could:
spawn(and notfork) 12 worker processes- set
MUTMUT_WORKER_1/2/.../12 - In each process run the complete test suite / an init method (for caching and running the database setup fixtures)
- Split the mutants across the workers (or use a multiprocessing queue/...)
- Inside of the worker, we sequentially run a single
forkto create isolated copies of the worker process (with everything already setup) and run tests for mutations there - Workers write their results into a results queue and the main process saves the stats in files (we should not write the
foo.py.metafiles from different processes)
Activity
nicklafleur commentedon Apr 27, 2026
I have some new forking strategies I needed to implement to support some libraries we use at Lyft (gevent) coming up in a PR (I believe it's either next or after 1 re-org PR, need to check) which partially addresses this kind of thing. I had initially moved away from the subprocess implementation as it resulted is slower performance, but the xdist-like use-case was not one I had considered when removing it. We could fairly easily add a new run mode which can better handle this kind of case by extending the new abstraction (MutantRunner) to handle this behaviour.
I've opened a draft of the in-flight here, I realized recently that the org has changed default branch viz settings for public forks
willianantunes-jsm commentedon Sep 2, 2026
Hi Otto, I ran into exactly this and ended up building the "one worker, many mutants" approach you sketch above, so here is how it went in practice.
My case: a Databricks Connect (Spark) suite where every process pays around 5 seconds to open a session before the first test runs. Since mutmut forks a fresh child for each mutant, a 308-mutant layer was paying 308 sessions. That turned out to be most of the run.
What I did was keep the whole mutmut pipeline as is (copy, mutate, stats, clean run, forced fail, all imported from
mutmut.__main__) and only replace the per-mutant loop. N forked workers each take a batch of mutants and, for each one, setMUTANT_UNDER_TESTand callpytest.main()with the covering tests. The trampoline already reads the env var on every call, and the parent already callspytest.main()several times, so nothing new was needed there. Each worker writes its verdicts to a file and the parent folds them intoSourceFileMutationData, soresults,showandbrowsework unchanged.Numbers with 6 workers, and the verdicts were identical to the fork-per-mutant run every time:
On layer C the mutant phase itself went from about 3m47s to about 35s; what is left is the baseline passes.
A few things bit me along the way that would probably bite any implementation of this:
The parent must not have live connections when it forks. One run hung for 20 minutes because the fork raced an in-flight gRPC call; grpc logs "skipping fork() handlers" and the children's new channels never come back, not even with SIGALRM, since the signal only lands when the C call returns. Stopping the parent's session after the forced-fail pass fixed it (it is never used after that anyway). Because of the same thing, I added a small watchdog in the parent: if a worker's results file stops growing for longer than any single mutant could take, kill it and leave its mutants as "not checked". And the usual fork hygiene: flush stdio before forking, and make the worker
os._exiton any exception, otherwise it falls back into the parent's loop and starts forking too.It is about 170 lines. If this is the direction you want to take with #510's
MutantRunner, I can share it as a gist or turn it into a PR.nicklafleur commentedon Sep 3, 2026
Sorry, kinda dropped off the map due to a job change and some vacation this summer and losing access to my mac where I was working on this project (maybe it's time I finally get around to #397 lol), I'm actually in the process of sorting out rebasing and all that due to the drift from main, with my goal to have the first sub-part merged in by Monday.
willianantunes-jsm commentedon Sep 3, 2026
Welcome back, no rush! I read through #510 and I think my thing fits as a third
process_isolationnext to hot-fork, so here it is as is, in case it helps while you rebase: https://gist.github.com/willianantunes-jsm/d7fd8bee36bcd5b2743cb37ec51580d8The one difference: hot-fork still forks a grandchild per mutant, and in my case the expensive part (a Spark session) is created inside the tests, so every grandchild would pay it again. The gist keeps N workers alive and runs many mutants in each, so it's paid once per worker. Everything else is mutmut's own pipeline imported as is.
Your clean parent is also the proper fix for the gRPC fork poisoning I mentioned. Once your first part lands, happy to port this onto
MutantRunneras a PR if you think it's worth having.