由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“无法为索引0的fd -1设置posix_spawn_file_actions

时间:2019-01-28 15:46:23

标签: swift asynchronous crash dispatch-queue

在Swift 4中,在Xcode版本9.3.1(9E501)上,我正在利用DispatchQueue.global(qos: .background).async {...}在后​​台运行while(true)循环:

func appStartDispatchQueue() {
    DispatchQueue.global(qos: .background).async {

        while(true) {
            let networkStatus = Verification.onCompanyNetwork()

            DispatchQueue.main.async {
                self.onNetwork = networkStatus
            }
            sleep(3)
        }
    }
}

Verification.onCompanyNetwork()正在通过bash脚本运行ping命令,以查看公司网络中的服务器是否响应。

我需要此线程始终在此应用程序的后台运行-因此,即使它失败,如何捕获异常并确保线程再次启动?现在我什至不知道为什么线程失败了...

到目前为止,发现了两种崩溃情况:

  • 在运行应用程序(在Xcode外部)时,该应用程序在连续运行3-6小时后平均崩溃
  • 在运行应用程序时(在Xcode内部),即使连续运行3-6小时后,该应用程序也不会崩溃

可以在下面找到整个错误消息:

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

1 个答案:

答案 0 :(得分:0)

最好的方法是使用https://github.com/ashleymills/Reachability.swift

在此链接中,您将始终了解如何请求google.com服务器。

https://github.com/ashleymills/Reachability.swift/blob/master/ReachabilitySample/ViewController.swift

有很多方法可以做到

let hostReachability = Reachability(hostName:"www.apple.com")
hostReachability.startNotifier()

NSNotificationCenter.defaultCenter().addObserver(
      self, 
      selector: "reachabilityChanged:", 
      name: kReachabilityChangedNotification, 
      object: nil) 

它将避免您使用while(true)或类似难以处理的东西。

enum NetworkStatus {
    case notReachable, reachableViaWiFi, reachableViaWWAN
}
var currentReachabilityStatus: NetworkStatus