美文网首页iOS Developer
在Load里添加Fabric初始化引起的悲剧

在Load里添加Fabric初始化引起的悲剧

作者: 咖啡兑水 | 来源:发表于2016-12-05 17:01 被阅读0次

    遇到问题

    Fabric是检测崩溃的工具,适合放置所有执行代码之前,检测后面的崩溃并上传。

    于是我想,既然要放置最前,就越前越好吧,于是就把[Fabric with:]加在了Appdelegate的load方法里,这下不会有搜集不到的崩溃了吧,哈哈(真是天真了)。悲剧来了当我们的app启动的时候,发现进入不了主界面,囧。

    调用堆栈如下

    Thread 0 name:  Dispatch queue: com.apple.main-thread
    Thread 0:
    0   libsystem_kernel.dylib          0x0000000197e57570 __semwait_signal + 8
    1   libsystem_pthread.dylib         0x0000000197ef76d0 pthread_join + 472
    2   vImage                          0x00000001849dd678 InitCGInterfacesOnAnotherThread + 240
    3   libdispatch.dylib               0x0000000197d11950 _dispatch_client_callout + 12
    4   libdispatch.dylib               0x0000000197d12828 dispatch_once_f + 92
    5   vImage                          0x00000001849dd580 LoadCGInterfaces + 64
    6   vImage                          0x00000001849d0da4 vImageConverter_CreateCGPassList + 136
    7   vImage                          0x00000001849cf110 vImageConverter_CreateWithCGImageFormat + 116
    8   ImageIO                         0x00000001870ee888 CGImagePixelDataProviderCreate + 372
    9   ImageIO                         0x0000000187118a70 CGImagePixelDataProviderCreateConforming + 1772
    10  ImageIO                         0x00000001870ed288 CGImageDestinationAddImage + 4104
    11  UIKit                           0x000000018aae7e38 UIImagePNGRepresentation + 576
    12  mail                            0x0000000100d72dc8 -[FABIcon initWithPNGImageAtPath:size:prerendered:] + 220
    13  mail                            0x0000000100d76034 -[FABAppIconUtility largestIconInIconNames:prerendered:] + 908
    14  mail                            0x0000000100d76308 -[FABAppIconUtility iconFromIOS7StyleCFBundleIconsInFieldWithKey:] + 588
    15  mail                            0x0000000100d766c0 -[FABAppIconUtility fetchApplicationIcon] + 80
    16  mail                            0x0000000100d75868 -[FABAppIconUtility applicationIcon] + 44
    17  mail                            0x0000000100d780b4 -[FABApplicationIdentiferModel initWithInstallID:] + 160
    18  mail                            0x0000000100d731e0 -[Fabric init] + 320
    19  mail                            0x0000000100d73324 __19+[Fabric sharedSDK]_block_invoke + 32
    20  libdispatch.dylib               0x0000000197d11950 _dispatch_client_callout + 12
    21  libdispatch.dylib               0x0000000197d12828 dispatch_once_f + 92
    22  mail                            0x0000000100d732fc +[Fabric sharedSDK] + 104
    23  mail                            0x0000000100d73478 __15+[Fabric with:]_block_invoke + 72
    24  libdispatch.dylib               0x0000000197d11950 _dispatch_client_callout + 12
    25  libdispatch.dylib               0x0000000197d12828 dispatch_once_f + 92
    26  mail                            0x0000000100d73428 +[Fabric with:] + 224
    27  mail                            0x0000000100739cc4 -[CrashFree prepareInternal] (CrashFree.m:192)
    28  mail                            0x00000001007391c0 -[CrashFree prepare] (CrashFree.m:61)
    29  mail                            0x00000001009ba9d0 +[NEMKernelComponentInit nemInit] (NEMKernelComponentInit.m:28)
    30  mail                            0x0000000100520c18 +[AppDelegate load] (AppDelegate.m:44)
    31  libobjc.A.dylib                 0x00000001976c1db4 call_load_methods + 612
    32  libobjc.A.dylib                 0x00000001976c2d18 load_images + 252
    33  dyld                            0x000000012006de40 dyld::notifySingle(dyld_image_states, ImageLoader const*) + 276
    34  dyld                            0x0000000120079670 ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 304
    35  dyld                            0x00000001200794d8 ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 136
    36  dyld                            0x00000001200797a0 ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) + 80
    37  dyld                            0x000000012006e150 dyld::initializeMainExecutable() + 196
    38  dyld                            0x00000001200718bc dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long*) + 2664
    39  dyld                            0x000000012006d040 _dyld_start + 64
    
    Thread 1 name:  Dispatch queue: com.apple.libdispatch-manager
    Thread 1:
    0   libsystem_kernel.dylib          0x0000000197e3cc24 kevent64 + 8
    1   libdispatch.dylib               0x0000000197d21e6c _dispatch_mgr_invoke + 272
    2   libdispatch.dylib               0x0000000197d13998 _dispatch_mgr_thread + 48
    
    Thread 2 name:  WebThread
    Thread 2:
    0   libsystem_kernel.dylib          0x0000000197e570c0 __psynch_mutexwait + 8
    1   libsystem_pthread.dylib         0x0000000197ef1490 _pthread_mutex_lock + 416
    2   WebCore                         0x0000000194772670 _WebTryThreadLock(bool) + 120
    3   WebCore                         0x00000001947725d4 WebRunLoopLock(__CFRunLoopObserver*, unsigned long, void*) + 40
    4   CoreFoundation                  0x0000000185e4c2a0 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 28
    5   CoreFoundation                  0x0000000185e4922c __CFRunLoopDoObservers + 356
    6   CoreFoundation                  0x0000000185e49700 __CFRunLoopRun + 1076
    7   CoreFoundation                  0x0000000185d752d0 CFRunLoopRunSpecific + 392
    8   WebCore                         0x0000000194770890 RunWebThread(void*) + 464
    9   libsystem_pthread.dylib         0x0000000197ef3dc4 _pthread_body + 160
    10  libsystem_pthread.dylib         0x0000000197ef3d20 _pthread_start + 156
    11  libsystem_pthread.dylib         0x0000000197ef0ef4 thread_start + 0
    
    Thread 3 name:  JavaScriptCore::BlockFree
    Thread 3:
    0   libsystem_kernel.dylib          0x0000000197e57078 __psynch_cvwait + 8
    1   libsystem_pthread.dylib         0x0000000197ef2f28 _pthread_cond_wait + 620
    2   libc++.1.dylib                  0x0000000196e3ccac std::__1::condition_variable::wait(std::__1::unique_lock<std::__1::mutex>&) + 52
    3   JavaScriptCore                  0x0000000187369620 JSC::BlockAllocator::blockFreeingThreadMain() + 228
    4   JavaScriptCore                  0x0000000187364b9c WTF::wtfThreadEntryPoint(void*) + 20
    5   libsystem_pthread.dylib         0x0000000197ef3dc4 _pthread_body + 160
    6   libsystem_pthread.dylib         0x0000000197ef3d20 _pthread_start + 156
    7   libsystem_pthread.dylib         0x0000000197ef0ef4 thread_start + 0
    
    Thread 4 name:  JavaScriptCore::Marking
    Thread 4:
    0   libsystem_kernel.dylib          0x0000000197e57078 __psynch_cvwait + 8
    1   libsystem_pthread.dylib         0x0000000197ef2f28 _pthread_cond_wait + 620
    2   libc++.1.dylib                  0x0000000196e3ccac std::__1::condition_variable::wait(std::__1::unique_lock<std::__1::mutex>&) + 52
    3   JavaScriptCore                  0x0000000187612edc JSC::GCThread::waitForNextPhase() + 152
    4   JavaScriptCore                  0x0000000187612f80 JSC::GCThread::gcThreadMain() + 88
    5   JavaScriptCore                  0x0000000187364b9c WTF::wtfThreadEntryPoint(void*) + 20
    6   libsystem_pthread.dylib         0x0000000197ef3dc4 _pthread_body + 160
    7   libsystem_pthread.dylib         0x0000000197ef3d20 _pthread_start + 156
    8   libsystem_pthread.dylib         0x0000000197ef0ef4 thread_start + 0
    
    Thread 5:
    0   libsystem_kernel.dylib          0x0000000197e570c0 __psynch_mutexwait + 8
    1   libsystem_pthread.dylib         0x0000000197ef1490 _pthread_mutex_lock + 416
    2   libobjc.A.dylib                 0x00000001976c2c44 load_images + 40
    3   dyld                            0x000000012006de40 dyld::notifySingle(dyld_image_states, ImageLoader const*) + 276
    4   dyld                            0x0000000120079670 ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 304
    5   dyld                            0x00000001200794d8 ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 136
    6   dyld                            0x00000001200797a0 ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) + 80
    7   dyld                            0x0000000120070d34 dyld::runInitializers(ImageLoader*) + 92
    8   dyld                            0x0000000120076048 dlopen + 784
    9   libdyld.dylib                   0x0000000197d3db94 dlopen + 68
    10  vImage                          0x00000001849dcf1c InitCGInterfaces + 44
    11  vImage                          0x00000001849dd810 InitPthreadWrapper + 8
    12  libsystem_pthread.dylib         0x0000000197ef3dc4 _pthread_body + 160
    13  libsystem_pthread.dylib         0x0000000197ef3d20 _pthread_start + 156
    14  libsystem_pthread.dylib         0x0000000197ef0ef4 thread_start + 0
    

    看上去是死锁了,我去,怎么死锁的呢。仔细观察,我发现Thread 5有调用方法和主线程调用的方法在名字上有相似的地方,InitCGInterfaces这个在主线程和Thread 5都有涉及。于是我有预感,是主线程和Thread 5在争夺资源。继续调查,InitCGInterfacesOnAnotherThread从名字上看是在其他线程执行个什么东东,嗯,应该就是去Thread 5了,再一次坚定了我的猜测。InitCGInterfacesOnAnotherThread上面pthread_join, 貌似在等待Thread 5执行完毕。

    接下来看看Thread 5在做什么,libdyld.dylib 0x0000000197d3db94 dlopen + 68,它去加载了一个动态库,这样一来,又重新跑了遍load_images

    load_images就是去调用所有类的load方法的入口

    load_images的堆栈上方,是加锁的,并处理wait状态

    0   libsystem_kernel.dylib          0x0000000197e570c0 __psynch_mutexwait + 8
    1   libsystem_pthread.dylib         0x0000000197ef1490 _pthread_mutex_lock + 416
    

    目前为止情况明朗了。

    主线程在load_images中发起Thread 5,并在等待Thread 5结束,但Thread 5执行load_images,需要获取load_images的锁,但是这个锁被主线程占用,Thread 5结束不了,就没法释放,导致了死锁

    总结

    尽量不要在load中做过多复杂是事务,尤其是涉及到锁,多线程方法的代码,容易造成死锁

    相关文章

      网友评论

        本文标题:在Load里添加Fabric初始化引起的悲剧

        本文链接:https://www.haomeiwen.com/subject/hixlmttx.html