title: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Failed to set posix_spawn_file_actions for fd -1 at index 0
body: <p>In Swift 4, on Xcode Version 9.3.1 (9E501), I am utilizing <code>DispatchQueue.global(qos: .background).async {...}</code> to run a <code>while(true)</code> loop in the background:</p>
  
  <pre><code>func appStartDispatchQueue() {
      DispatchQueue.global(qos: .background).async {
  
          while(true) {
              let networkStatus = Verification.onCompanyNetwork()
  
              DispatchQueue.main.async {
                  self.onNetwork = networkStatus
              }
              sleep(3)
          }
      }
  }
  </code></pre>
  
  <p><code>Verification.onCompanyNetwork()</code> is running a ping via bash script to see if there is response from a server on a corporate network.</p>
  
  <p>I need this thread running in the background at all times for this application - so even if it fails, how can I catch the exception and make sure the thread starts up again? Right now I'm not even sure why the thread is failing...</p>
  
  <p>Two discovered crash scenarios so far:</p>
  
  <ul>
  <li>while running app (outside of Xcode), the app crashes on average after 3-6 hours of continuous running</li>
  <li>while running app (inside of Xcode), the app does NOT crash, even after 3-6 hours of continuous running</li>
  </ul>
  
  <p>The entire error message is found below:</p>
  
  <pre><code>Crashed Thread:        1  Dispatch queue: com.apple.root.background-qos
  
  Exception Type:        EXC_CRASH (SIGABRT)
  Exception Codes:       0x0000000000000000, 0x0000000000000000
  Exception Note:        EXC_CORPSE_NOTIFY
  
  Application Specific Information:
  *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Failed to set posix_spawn_file_actions for fd -1 at index 0 with errno 9'
  terminating with uncaught exception of type NSException
  abort() called
  
  Application Specific Backtrace 1:
  0   CoreFoundation                      0x00007fff339d9ecd __exceptionPreprocess + 256
  1   libobjc.A.dylib                     0x00007fff5fa91720 objc_exception_throw + 48
  2   CoreFoundation                      0x00007fff339d9cff +[NSException raise:format:] + 201
  3   Foundation                          0x00007fff35ccf581 -[NSConcreteTask launchWithDictionary:error:] + 3236
  4   AdminAccess                         0x000000010f268b32 _T011AdminAccess9ScriptingC12shellCommandSSSgSS0D4Path_SaySSG9argumentstFZ + 370
  5   AdminAccess                         0x000000010f266e20 _T011AdminAccess9ScriptingC13runBashScriptS2S10scriptName_SS3argtFZ + 32496
  6   AdminAccess                         0x000000010f25779b _T011AdminAccess12VerificationC16onCompanyNetworkSbyFZ + 107
  7   AdminAccess                         0x000000010f26a834 _T011AdminAccess11AppDelegateC21appStartDispatchQueueyyFyycfU_ + 36
  8   AdminAccess                         0x000000010f26a99d _T011AdminAccess11AppDelegateC21appStartDispatchQueueyyFyycfU_TA + 13
  9   AdminAccess                         0x000000010f26aa1d _T0Ieg_IeyB_TR + 45
  10  libdispatch.dylib                   0x00007fff60b10d53 _dispatch_call_block_and_release + 12
  11  libdispatch.dylib                   0x00007fff60b11dcf _dispatch_client_callout + 8
  12  libdispatch.dylib                   0x00007fff60b1fe80 _dispatch_root_queue_drain + 671
  13  libdispatch.dylib                   0x00007fff60b204bd _dispatch_worker_thread2 + 90
  14  libsystem_pthread.dylib             0x00007fff60d526dd _pthread_wqthread + 619
  15  libsystem_pthread.dylib             0x00007fff60d52405 start_wqthread + 13
  
  Thread 0:: Dispatch queue: com.apple.main-thread
  0   libsystem_kernel.dylib          0x00007fff60c9917a mach_msg_trap + 10
  1   libsystem_kernel.dylib          0x00007fff60c996d0 mach_msg + 60
  2   com.apple.CoreFoundation        0x00007fff339230c2 __CFRunLoopServiceMachPort + 337
  3   com.apple.CoreFoundation        0x00007fff33922611 __CFRunLoopRun + 1654
  4   com.apple.CoreFoundation        0x00007fff33921d48 CFRunLoopRunSpecific + 463
  5   com.apple.HIToolbox             0x00007fff32bb8ab5 RunCurrentEventLoopInMode + 293
  6   com.apple.HIToolbox             0x00007fff32bb87eb ReceiveNextEventCommon + 618
  7   com.apple.HIToolbox             0x00007fff32bb8568 _BlockUntilNextEventMatchingListInModeWithFilter + 64
  8   com.apple.AppKit                0x00007fff30e73363 _DPSNextEvent + 997
  9   com.apple.AppKit                0x00007fff30e72102 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1362
  10  com.apple.AppKit                0x00007fff30e6c165 -[NSApplication run] + 699
  11  com.apple.AppKit                0x00007fff30e5b8a3 NSApplicationMain + 780
  12  mace.AdminAccess                0x000000010f26be6d main + 13 (AppDelegate.swift:18)
  13  libdyld.dylib                   0x00007fff60b5fed9 start + 1
  
  Thread 1 Crashed:: Dispatch queue: com.apple.root.background-qos
  0   libsystem_kernel.dylib          0x00007fff60c9f23e __pthread_kill + 10
  1   libsystem_pthread.dylib         0x00007fff60d55c1c pthread_kill + 285
  2   libsystem_c.dylib               0x00007fff60c081c9 abort + 127
  3   libc++abi.dylib                 0x00007fff5e289231 abort_message + 231
  4   libc++abi.dylib                 0x00007fff5e2893cd default_terminate_handler() + 265
  5   libobjc.A.dylib                 0x00007fff5fa93c87 _objc_terminate() + 97
  6   libc++abi.dylib                 0x00007fff5e294dfe std::__terminate(void (*)()) + 8
  7   libc++abi.dylib                 0x00007fff5e294e73 std::terminate() + 51
  8   libdispatch.dylib               0x00007fff60b11de3 _dispatch_client_callout + 28
  9   libdispatch.dylib               0x00007fff60b1fe80 _dispatch_root_queue_drain + 671
  10  libdispatch.dylib               0x00007fff60b204bd _dispatch_worker_thread2 + 90
  11  libsystem_pthread.dylib         0x00007fff60d526dd _pthread_wqthread + 619
  12  libsystem_pthread.dylib         0x00007fff60d52405 start_wqthread + 13
  
  Thread 2:: com.apple.NSEventThread
  0   libsystem_kernel.dylib          0x00007fff60c9917a mach_msg_trap + 10
  1   libsystem_kernel.dylib          0x00007fff60c996d0 mach_msg + 60
  2   com.apple.CoreFoundation        0x00007fff339230c2 __CFRunLoopServiceMachPort + 337
  3   com.apple.CoreFoundation        0x00007fff33922611 __CFRunLoopRun + 1654
  4   com.apple.CoreFoundation        0x00007fff33921d48 CFRunLoopRunSpecific + 463
  5   com.apple.AppKit                0x00007fff30e7bf89 _NSEventThread + 160
  6   libsystem_pthread.dylib         0x00007fff60d53305 _pthread_body + 126
  7   libsystem_pthread.dylib         0x00007fff60d5626f _pthread_start + 70
  8   libsystem_pthread.dylib         0x00007fff60d52415 thread_start + 13
  
  Thread 3:
  0   libsystem_pthread.dylib         0x00007fff60d523f8 start_wqthread + 0
  1   ???                             0x0000000054485244 0 + 1414025796
  
  Thread 4:
  0   libsystem_pthread.dylib         0x00007fff60d523f8 start_wqthread + 0
  1   ???                             0x0000000000000903 0 + 2307
  
  Thread 1 crashed with X86 Thread State (64-bit):
    rax: 0x0000000000000000  rbx: 0x00007000054b9000  rcx: 0x00007000054b88b8  rdx: 0x0000000000000000
    rdi: 0x0000000000002703  rsi: 0x0000000000000006  rbp: 0x00007000054b88f0  rsp: 0x00007000054b88b8
     r8: 0x00007000054b8778   r9: 0x00007000054b8950  r10: 0x0000000000000000  r11: 0x0000000000000206
    r12: 0x0000000000002703  r13: 0x0000003000000008  r14: 0x0000000000000006  r15: 0x000000000000002d
    rip: 0x00007fff60c9f23e  rfl: 0x0000000000000206  cr2: 0x00007fff93949188
  </code></pre>