Skip to content

Support parallel tests with external database #474

Open

Description

@Otto-AA

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:

  1. start e.g. 12 processes with PYTEST_XDIST_WORKER set to gw1, gw2, ..., gw12`
  2. for each process, it runs the pytest session fixture to setup the database
    2.1. The fixture connects to postgres and creates a database test_db_{PYTEST_XDIST_WORKER}
    2.2. Afterwards, we have 12 different databases
  3. 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:

  1. spawn (and not fork) 12 worker processes
  2. set MUTMUT_WORKER_1/2/.../12
  3. In each process run the complete test suite / an init method (for caching and running the database setup fixtures)
  4. Split the mutants across the workers (or use a multiprocessing queue/...)
  5. Inside of the worker, we sequentially run a single fork to create isolated copies of the worker process (with everything already setup) and run tests for mutations there
  6. Workers write their results into a results queue and the main process saves the stats in files (we should not write the foo.py.meta files from different processes)

Activity

  1. nicklafleur commented on Apr 27, 2026

    @nicklafleur
    Collaborator

    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

  2. willianantunes-jsm commented on Sep 2, 2026

    @willianantunes-jsm

    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, set MUTANT_UNDER_TEST and call pytest.main() with the covering tests. The trampoline already reads the env var on every call, and the parent already calls pytest.main() several times, so nothing new was needed there. Each worker writes its verdicts to a file and the parent folds them into SourceFileMutationData, so results, show and browse work unchanged.

    Numbers with 6 workers, and the verdicts were identical to the fork-per-mutant run every time:

    mutants 1 child (mutmut default) 6 children, fork per mutant 6 workers, batched
    Layer A, all Spark tests 308 57 min 15m53s 6m09s
    Layer B, all Spark tests 145 33m50s not measured 3m56s
    Layer C, mostly pure Python 313 12m28s 3m58s 1m57s
    Whole project, 5 layers 938 ~2 h ~14 min

    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._exit on 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.

  3. nicklafleur commented on Sep 3, 2026

    @nicklafleur
    Collaborator

    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.

  4. willianantunes-jsm commented on Sep 3, 2026

    @willianantunes-jsm

    Welcome back, no rush! I read through #510 and I think my thing fits as a third process_isolation next to hot-fork, so here it is as is, in case it helps while you rebase: https://gist.github.com/willianantunes-jsm/d7fd8bee36bcd5b2743cb37ec51580d8

    The 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 MutantRunner as a PR if you think it's worth having.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions