Skip to content

Instantly share code, notes, and snippets.

Last active Aug 28, 2019
What would you like to do?
macOS fork bug reproducer for
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");
return NULL;
void check_err(int err, char *msg) {
if (!err) {
printf("%s: %d\n", msg, err);
int main(int argc, char **argv) {
pthread_t tid;
useconds_t wait_time;
if (argc < 2) {
printf("usage: %s SLEEP_US\n", argv[0]);
wait_time = atoi(argv[1]);
check_err(pthread_create(&tid, NULL, do_fork, NULL), "pthread_create");
// check_err(pthread_join(tid, NULL), "pthread_join");
return 0;
Copy link

jacobvosmaer commented Aug 28, 2019

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