Skip to content

Multithreaded fork appears flaky on OSX #14232

Closed
@alexcrichton

Description

@alexcrichton

In the following program, a number of threads are made, and then each thread forks of a child that sleeps forever and then immediately kills it. I would expect this program to succeed continuously, but it wedges on OSX occasionally, reporting a successful signal delivery, but failing to actually deliver the signal apparently.

#include <stdlib.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>

void *work(void *arg) {
#define M 100
  int i;
  for (i = 0; i < M; i++) {
    int a = fork();
    assert(a >= 0);
    if (a == 0) {
      sleep(1000000000);
      return arg;
    }
    assert(kill(a, SIGTERM) == 0);
    int ret;
    assert(waitpid(a, &ret, 0) == a);
    assert(!WIFEXITED(ret));
  }
  return arg;
}

int main() {
#define N 8
  pthread_t c[N];
  int i;
  for (i = 0; i < N; i++) {
    assert(pthread_create(&c[i], NULL, work, (void*) (size_t) i) == 0);
  }
  for (i = 0; i < N; i++) {
    assert(pthread_join(c[i], NULL) == 0);
  }
}

This is essentially how we fork() in libnative, and it's how we're using fork from libgreen. Trying to investigate a solution to this, but I'm starting to think that multithreaded fork is just fundamentally broken on basically all platforms except linux.

This issue has appeared as various forms of flakiness on the bots, which is why I started investigating.

Metadata

Metadata

Assignees

Labels

P-mediumMedium priorityT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions