···2424#include <string.h>
2525#include <stdbool.h>
2626#include <stdlib.h>
2727+#include <signal.h>
2828+#include <unistd.h>
2929+#include <sys/syscall.h>
27302831// The point of this file is build macOS threads on top of native libc's threads,
2932// otherwise it would not be possible to make native calls from these threads.
···162165int __darling_thread_terminate(void* stackaddr,
163166 unsigned long freesize, unsigned long pthobj_size)
164167{
168168+ if (getpid() == syscall(SYS_gettid))
169169+ {
170170+ // dispatch_main() calls pthread_exit(NULL) on the main thread,
171171+ // which turns the our process into a zombie.
172172+ // Let's just hang around forever.
173173+ sigset_t mask;
174174+ memset(&mask, 0, sizeof(mask));
175175+176176+ while (1)
177177+ sigsuspend(&mask);
178178+ }
179179+165180 struct reaper_item* item = (struct reaper_item*) malloc(sizeof(struct reaper_item));
166181 item->thread = pthread_self();
167182 item->stack = stackaddr;