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

Posted

技术标签:

【中文标题】由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法在索引 0 处为 fd -1 设置 posix_spawn_file_actions【英文标题】:Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Failed to set posix_spawn_file_actions for fd -1 at index 0 【发布时间】:2019-06-21 16:00:05 【问题描述】:

在 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】:

最好的方法是使用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

【讨论】:

以上是关于由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法在索引 0 处为 fd -1 设置 posix_spawn_file_actions的主要内容,如果未能解决你的问题,请参考以下文章

目标 C:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序

由于未捕获的异常而终止应用程序 [UIImageView _isResizable]

由于未捕获的异常而终止应用程序,同时加载视图

由于未捕获的异常“NSInternalInconsistencyException”错误而终止应用程序

“由于未捕获的异常而终止应用程序”在推送视图控制器时崩溃

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,