Skip to content

import setproctitle after forking on macOS#450

Closed
Otto-AA wants to merge 1 commit intomainfrom
fix-macos-segfault
Closed

import setproctitle after forking on macOS#450
Otto-AA wants to merge 1 commit intomainfrom
fix-macos-segfault

Conversation

@Otto-AA
Copy link
Collaborator

@Otto-AA Otto-AA commented Oct 30, 2025

Fixes #446

On macOS this likely costs a few miliseconds per mutation (the import takes +-1ms on my linux system).

@nkakouros
Copy link

What does this need to move forward?

@Otto-AA
Copy link
Collaborator Author

Otto-AA commented Mar 5, 2026

I think the #476 PR makes sense (not using setproctitle on MacOS), but with an opt-in config to use setproctitle.

The reasons I haven't fixed it yet are:

  • minimal repros don't really work (i.e. making a small python script with setproctitle and afterwards os.fork() does not crash). This makes it hard to find workarounds
  • I work on Linux, hard to reproduce again :)
  • there's documentation that importing setproctitle after forking (as done in this PR) should work (see this comment or this PR), but apparently it does not work in mutmut (see this comment)?? 🤯
  • even when we remove setproctitle from mutmut, running mutmut on an app with setproctitle or similar will likely also crash

So I don't really understand the bug. Sorry it's taking long, I guess it's time to fix it even without really understanding it.

@nicklafleur
Copy link
Contributor

nicklafleur commented Mar 6, 2026

From what I can tell this is an issue with the MacOS foundations API and setproctitle's C bindings/implementation being not fork safe with python's fork library. There is a way to avoid this entirely with subprocess, but that comes at the cost of the performance improvements that come with fork which are pretty significant when killing a mutants can be <0.5s with forking but need an additional 2s of import time (if you're lucky in large codebases) with subprocesses.

I do have a future PR (commits can be found at https://github.com/lyft/mutmut/tree/nicklafleur/oss-complete) which implements a few different forking strategies (mainly due to the fact that we also use libraries like gunicorn/gevent/etc which pollute the internal state of the main process). We could absolutely add a "subprocess" mode to this if the need is there. It was actually the initial implementation but I moved away due to poor performance.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Segfault on macOS when setproctitle() is called after os.fork() in worker processes

3 participants