Last active
August 28, 2019 19:16
-
-
Save jacobvosmaer/69fae756e88d2f5f4ef5091ceeba1d88 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cross-referencing: this is a C reproducer for golang/go#33565