Skip to content

Fix spurious setpgid failures in the process spawner (macOS) - #153

Open
ibirol wants to merge 1 commit into
masterfrom
macos-setpgid-race
Open

ibirol wants to merge 1 commit into
masterfrom
macos-setpgid-race

Conversation

@ibirol

@ibirol ibirol commented Oct 26, 2026

Copy link
Copy Markdown

On macOS, btllib's process spawner exits with setpgid failed in ... process under ordinary load. This PR makes setpgid() failures fatal only when the spawner really did not end up in its own process group, and checks fork().

Problem

process_spawner_init() forks the spawner and calls setpgid() from both the parent and the child, so the spawner is in its own group whichever runs first. Any failure other than EACCES in the parent, and any failure in the child, exits the program.

On macOS, setpgid() returns EPERM in two cases where nothing is wrong:

  • The other side has already moved the process into its new group.
  • Briefly, when many processes change groups at the same time.

Every program linked to btllib runs this at startup, and GoldPolish starts many of them in parallel. On an Apple Silicon Mac, polishing the GoldRush demo data failed in 2 of 9 runs. A third run hung, waiting on a helper that had exited.

Separately, the return value of fork() is never checked. When fork() fails, for example at the per-user process limit, the parent calls setpgid(-1, -1) and reports setpgid failed in parent process. This is the likely cause of that message in BirolLab/goldpolish#11, which raising ulimit -u resolved.

Change

In src/btllib/process_pipeline.cpp, process_spawner_init():

  • Treat setpgid() as successful when the process already leads its own group (getpgrp() == getpid() in the child, getpgid(pid) == pid in the parent).
  • Retry EPERM up to 200 times, 0.5 ms apart. Other errors fail immediately, as before. The retry is needed: with only the group-leader check, 1 of 10 GoldPolish runs still hung.
  • Check fork() and report fork failed: <reason>.
  • Include strerror() in the setpgid error messages.

On Linux, behaviour is unchanged unless setpgid() fails, which the existing code already treated as fatal.

Testing

All on macOS with Apple Silicon (arm64); not run on Linux.

Test Before After
GoldPolish on the GoldRush demo data, through goldrush-ulimit 2 failed and 1 hung, of 9 runs 20 of 20 clean
GoldRush demo (tests/goldrush_test_demo.sh) failed at polishing passes (L50 = 2)
  • Standalone reproducer: 32 workers each fork 300 times using the same pattern. The child's setpgid(0, 0) returned EPERM while not yet a group leader 5 times in 57,600 forks, over six runs. It is rare, so run the program a few times. In the three runs that also retried, all 4 such failures succeeded on the second attempt. The code is below.
  • btllib tests: meson test --repeat=5 on a debugoptimized build: 100 of 100 passed (20 tests, 5 times each).
  • Formatting: clang-format 18 reports no changes.
Reproducer (C)
// Many workers each fork repeatedly; child and parent both call setpgid().
// Counts failures where the child is not yet a group leader.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/mman.h>
int main(int argc, char **argv) {
  int workers = argc > 1 ? atoi(argv[1]) : 32, iters = argc > 2 ? atoi(argv[2]) : 300;
  int *child_eperm = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
  *child_eperm = 0;
  for (int w = 0; w < workers; ++w) if (fork() == 0) {
    for (int i = 0; i < iters; ++i) {
      int go[2]; pipe(go);
      pid_t pid = fork();
      if (pid == 0) {
        close(go[1]);
        if (setpgid(0, 0) != 0 && errno == EPERM && getpgrp() != getpid())
          __sync_fetch_and_add(child_eperm, 1);
        char b; read(go[0], &b, 1); _exit(0);
      }
      close(go[0]);
      setpgid(pid, pid);
      write(go[1], "x", 1); close(go[1]); waitpid(pid, NULL, 0);
    }
    _exit(0);
  }
  while (wait(NULL) > 0) {}
  printf("forks=%d child EPERM while not group leader=%d\n", workers * iters, *child_eperm);
  return 0;
}

🤖 Generated with Claude Code

process_spawner_init() forks a spawner and calls setpgid() from both the
parent and the child, so that either order leaves the spawner in its own
process group. Any failure other than EACCES in the parent was fatal.

On macOS, setpgid() returns EPERM when the other side has already moved
the process, and occasionally under load when many processes start at
once. btllib then exits with "setpgid failed in ... process". In
GoldPolish this killed polishing in 2 of 9 runs on an Apple Silicon Mac
and left one run hung, waiting on a helper that had exited.

- Treat setpgid() as successful when the process already leads its own
  group (getpgrp()/getpgid() == pid), from either side.
- Retry EPERM briefly (up to 200 attempts, 0.5 ms apart).
- Check the return value of fork(). A failed fork (e.g. at the per-user
  process limit) went on to call setpgid(-1, -1) and was reported as
  "setpgid failed in parent process". This is the likely cause of that
  message in BirolLab/goldpolish#11, which raising ulimit -u resolved.
  It now reports "fork failed" and the reason.
- Include strerror() in the setpgid error messages.

Tested on macOS (Apple Silicon, arm64): GoldPolish ran 20 of 20 times
without failures or hangs, and the GoldRush demo passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@lcoombe lcoombe left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine to me!

Note that we haven't seen the error on macOS CI here or in the goldpolish repo, so this may be an issue limited to apple silicon - there are unfortunately not any free macOS ARM64 images currently available in azure devops, so we can't add that to CI.

It would be good to get a review from @jwong-1 and/or @parham-k as well before we merge.

This branch has not been deployed

No deployments
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.

3 participants