Skip to content

Instantly share code, notes, and snippets.

@seivan
Forked from landonf/pthread_deadlock.m
Created July 2, 2013 16:40
Show Gist options
  • Select an option

  • Save seivan/5910928 to your computer and use it in GitHub Desktop.

Select an option

Save seivan/5910928 to your computer and use it in GitHub Desktop.
#import <pthread.h>
static void unsafe_signal_handler (int signo) {
/* Try to fetch thread names with the pthread API */
char name[512];
NSLog(@"Trying to use the pthread API from a signal handler. Is a deadlock coming?");
pthread_getname_np(pthread_self(), name, sizeof(name));
// We'll never reach this point. The process will stop here until the OS watchdog
// kills it in 20+ seconds, or the user force quits it. No crash report (or a partial corrupt
// one) will be written.
NSLog(@"We'll never reach this point.");
exit(1);
}
static void *enable_threading (void *ctx) {
return NULL;
}
int main(int argc, char *argv[]) {
/* Remove this line to test your own crash reporter */
signal(SIGSEGV, unsafe_signal_handler);
/* We have to use pthread_create() to enable locking in malloc/pthreads/etc -- this
* would happen by default in any real application, as the standard frameworks
* (such as dispatch) will trigger similar calls into the pthread APIs. */
pthread_t thr;
pthread_create(&thr, NULL, enable_threading, NULL);
/* This is the actual code that triggers a reproducible deadlock; include this
* in your own app to test a different crash reporter's behavior.
*
* While this is a simple test case to reliably trigger a deadlock, it's not necessary
* to crash inside of a pthread call to trigger this bug. Any thread sitting inside of
* pthread() at the time a crash occurs would trigger the same deadlock. */
pthread_getname_np(pthread_self(), (char *)0x1, 1);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment