title: Why does a semaphore_wait_trap cause EXC_CRASH (SIGKILL)
body: <p>I have an iOS customer reporting a consistent, crashing bug, with the following stack:</p>
  
  <pre><code>Exception Type:  EXC_CRASH (SIGKILL)
  Exception Codes: 0x0000000000000000, 0x0000000000000000
  Exception Note:  EXC_CORPSE_NOTIFY
  Termination Reason: Namespace SPRINGBOARD, Code 0x8badf00d
  Triggered by Thread:  0
  
  Filtered syslog:
  None found
  
  
  Thread 0 Crashed:
  0   libsystem_kernel.dylib          0x000000018eb741a8 semaphore_wait_trap + 8
  1   libdispatch.dylib               0x000000018ea5f7ec _dispatch_semaphore_wait_slow + 216
  2   Scruff                          0x00000001003bf0fc -[TMCache setObject:forKey:] (TMCache.m:332)
  3   Scruff                          0x00000001000c2a40 __44-[MSSProfileStreamingDataSource downloaded:]_block_invoke (MSSProfileStreamingDataSource.m:532)
  4   libdispatch.dylib               0x000000018ea4d200 _dispatch_call_block_and_release + 24
  5   libdispatch.dylib               0x000000018ea4d1c0 _dispatch_client_callout + 16
  6   libdispatch.dylib               0x000000018ea51d6c _dispatch_main_queue_callback_4CF + 1000
  7   CoreFoundation                  0x000000018fb71f2c __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
  8   CoreFoundation                  0x000000018fb6fb18 __CFRunLoopRun + 1660
  9   CoreFoundation                  0x000000018fa9e048 CFRunLoopRunSpecific + 444
  10  GraphicsServices                0x0000000191521198 GSEventRunModal + 180
  11  UIKit                           0x0000000195a77818 -[UIApplication _run] + 684
  12  UIKit                           0x0000000195a72550 UIApplicationMain + 208
  13  Scruff                          0x00000001000cc9b8 main (main.m:22)
  14  libdyld.dylib                   0x000000018ea805b8 start + 4
  </code></pre>
  
  <p>Can someone explain exactly what <code>semaphore_wait_trap</code> is doing, and in what conditions it would trigger an <code>EXEC_CRASH</code>? <code>SIGKILL</code> means someone else triggered a kill - does this mean that iOS is killing the app because the semaphore wait took too long?</p>
  
  <p>Here is the code in question for <code>TMCache</code>, which is the <a href="https://github.com/tumblr/TMCache/blob/master/TMCache/TMCache.m" rel="nofollow">Tumblr cache library for iOS</a>:</p>
  
  <pre><code>- (void)setObject:(id &lt;NSCoding&gt;)object forKey:(NSString *)key
  {
      if (!object || !key)
          return;
  
      dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  
      [self setObject:object forKey:key block:^(TMCache *cache, NSString *key, id object) {
          dispatch_semaphore_signal(semaphore);
      }];
  
      dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
  
      #if !OS_OBJECT_USE_OBJC
      dispatch_release(semaphore);
      #endif
  }
  </code></pre>
  
  <p>Line 332 is the final line of the method, and <code>OS_OBJECT_USE_OBJC</code> is true, so I assume this crash is because of <code>dispatch_semaphore_wait</code>.</p>