Skip to content

Instantly share code, notes, and snippets.

@jacobvosmaer
Last active August 28, 2019 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacobvosmaer/69fae756e88d2f5f4ef5091ceeba1d88 to your computer and use it in GitHub Desktop.
Save jacobvosmaer/69fae756e88d2f5f4ef5091ceeba1d88 to your computer and use it in GitHub Desktop.
/*
macOS fork bug reproducer for https://github.com/golang/go/issues/33565
cc -o fork-leak fork-leak.c
while true; do sh -c 'exec ./fork-leak 1 $$'; done
In another terminal, run:
while sleep 2; do pgrep fork-leak; done
What happens, if you're on the "right" version of macOS, is that
at some point you seeing leaked forked 'fork-leak' processes.
This unexpected because as you can see below in do_fork, the fork
should immediately exit. Why do some of them not exit?
macos 10.14.5
*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void *do_fork(void *arg) {
if (!fork()) {
printf("fork happened!\n");
exit(0);
}
return NULL;
}
void check_err(int err, char *msg) {
if (!err) {
return;
}
printf("%s: %d\n", msg, err);
exit(1);
}
int main(int argc, char **argv) {
pthread_t tid;
useconds_t wait_time;
if (argc < 2) {
printf("usage: %s SLEEP_US\n", argv[0]);
exit(1);
}
wait_time = atoi(argv[1]);
check_err(pthread_create(&tid, NULL, do_fork, NULL), "pthread_create");
// check_err(pthread_join(tid, NULL), "pthread_join");
usleep(wait_time);
return 0;
}
@jacobvosmaer
Copy link
Author

Cross-referencing: this is a C reproducer for golang/go#33565

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