一、最近米家App进入前台的时候上报上来一个Crash
{"app_name":"MiHome","timestamp":"2019-04-03 11:00:09.66 +0800","app_version":"4.15.101","slice_uuid":"d41578a2-af0f-3fc5-b202-c120aeaacba2","adam_id":0,"build_version":"4.15.101.1334","bundleID":"com.xiaomi.mihome.dailybuild","share_with_app_devs":false,"is_first_party":false,"bug_type":"109","os_version":"iPhone OS 12.1 (16B92)","incident_id":"0DD1416C-EFFC-45CC-A5B1-7099FD5582E1","name":"MiHome"} Incident Identifier: 0DD1416C-EFFC-45CC-A5B1-7099FD5582E1 CrashReporter Key: 0d34987d160e2a213f0eea4224c7be37b0e5f7ff Hardware Model: iPhone10,2 Process: MiHome [12041] Path: /private/var/containers/Bundle/Application/C164FC42-7011-4900-B31D-C1FBF97A4927/MiHome.app/MiHome Identifier: com.xiaomi.mihome.dailybuild Version: 4.15.101.1334 (4.15.101) AppStoreTools: 10B61 Code Type: ARM-64 (Native) Role: Non UI Parent Process: launchd [1] Coalition: com.xiaomi.mihome.dailybuild [15486] Date/Time: 2019-04-03 11:00:09.4286 +0800 Launch Time: 2019-04-03 10:56:55.4746 +0800 OS Version: iPhone OS 12.1 (16B92) Baseband Version: 3.11.00 Report Version: 104 Exception Type: EXC_CRASH (SIGKILL) Exception Codes: 0x0000000000000000, 0x0000000000000000 Exception Note: EXC_CORPSE_NOTIFY Termination Reason: Namespace SPRINGBOARD, Code 0x8badf00d Termination Description: SPRINGBOARD, scene-update watchdog transgression: com.xiaomi.mihome.dailybuild exhausted real (wall clock) time allowance of 10.00 seconds | ProcessVisibility: Foreground | ProcessState: Running | WatchdogEvent: scene-update | WatchdogVisibility: Foreground | WatchdogCPUStatistics: ( | "Elapsed total CPU time (seconds): 3.890 (user 3.890, system 0.000), 6% CPU", | "Elapsed application CPU time (seconds): 0.695, 1% CPU" | ) Triggered by Thread: 0 Thread 0 name: Dispatch queue: com.apple.main-thread Thread 0 Crashed: 0 libsystem_kernel.dylib 0x00000001c97a79fc __ulock_wait + 8 1 libsystem_platform.dylib 0x00000001c981b8f8 _os_unfair_lock_lock_slow + 172 2 libobjc.A.dylib 0x00000001c8df4830 objc_sync_enter + 32 3 MiHome 0x00000001035add48 -[MHTutkIotcManager registerNotifDictString] + 22076744 (MHTutkIotcManager.m:286) 4 MiHome 0x00000001035ad654 -[MHTutkIotcManager appEnterForeground:] + 22074964 (MHTutkIotcManager.m:241) 5 MiHome 0x0000000104b22c8c __43-[MHNotificationCenter processNotificaton:]_block_invoke + 44575884 (MHNotificationCenter.m:162) 6 CoreFoundation 0x00000001c9af56bc -[__NSArrayM enumerateObjectsWithOptions:usingBlock:] + 216 7 MiHome 0x0000000104b22b6c -[MHNotificationCenter processNotificaton:] + 44575596 (MHNotificationCenter.m:164) 8 CoreFoundation 0x00000001c9b7e928 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 20 9 CoreFoundation 0x00000001c9b7e8f4 ___CFXRegistrationPost_block_invoke + 64 10 CoreFoundation 0x00000001c9b7dde4 _CFXRegistrationPost + 392 11 CoreFoundation 0x00000001c9b7da90 ___CFXNotificationPost_block_invoke + 96 12 CoreFoundation 0x00000001c9af5d70 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1404 13 CoreFoundation 0x00000001c9b7d51c _CFXNotificationPost + 696 14 Foundation 0x00000001ca58dc10 -[NSNotificationCenter postNotificationName:object:userInfo:] + 68 15 UIKitCore 0x00000001f6e29c30 -[UIApplication _sendWillEnterForegroundCallbacks] + 232 16 UIKitCore 0x00000001f66abdc4 -[__UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:] + 2180 17 UIKitCore 0x00000001f66a9a74 __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke + 772 18 UIKitCore 0x00000001f66a9720 -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] + 432 19 UIKitCore 0x00000001f66ae8e0 __125-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke + 220 20 UIKitCore 0x00000001f66af840 _performActionsWithDelayForTransitionContext + 112 21 UIKitCore 0x00000001f66ae798 -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] + 248 22 UIKitCore 0x00000001f66b3684 -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:] + 368 23 UIKitCore 0x00000001f69f7090 -[UIApplicationSceneClientAgent scene:handleEvent:withCompletion:] + 468 24 FrontBoardServices 0x00000001cc6456e4 __80-[FBSSceneImpl updater:didUpdateSettings:withDiff:transitionContext:completion:]_block_invoke.359 + 228 25 libdispatch.dylib 0x00000001c964a484 _dispatch_client_callout + 16 26 libdispatch.dylib 0x00000001c96213f0 _dispatch_block_invoke_direct$VARIANT$armv81 + 216 27 FrontBoardServices 0x00000001cc683a9c __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 40 28 FrontBoardServices 0x00000001cc683728 -[FBSSerialQueue _performNext] + 416 29 FrontBoardServices 0x00000001cc683d44 -[FBSSerialQueue _performNextFromRunLoopSource] + 56 30 CoreFoundation 0x00000001c9ba01cc __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24 31 CoreFoundation 0x00000001c9ba014c __CFRunLoopDoSource0 + 88 32 CoreFoundation 0x00000001c9b9fa30 __CFRunLoopDoSources0 + 176 33 CoreFoundation 0x00000001c9b9a8fc __CFRunLoopRun + 1040 34 CoreFoundation 0x00000001c9b9a1cc CFRunLoopRunSpecific + 436 35 GraphicsServices 0x00000001cbe11584 GSEventRunModal + 100 36 UIKitCore 0x00000001f6e11054 UIApplicationMain + 212 37 MiHome 0x00000001026e9a00 main + 6593024 (main.m:19) 38 libdyld.dylib 0x00000001c965abb4 start + 4 Thread 28 name: Dispatch queue: com.apple.root.default-qos Thread 28: 0 libsystem_kernel.dylib 0x00000001c97a7428 __semwait_signal + 8 1 libsystem_c.dylib 0x00000001c971c5d0 nanosleep + 212 2 libsystem_c.dylib 0x00000001c971c4a4 usleep + 64 3 MiHome 0x00000001027a1d50 IOTC_Connect_UDP + 7347536 (UDP_Connect.c:2410) 4 MiHome 0x00000001035ac59c -[MHTutkIotcManager mh_iotcConnectByUIDParallel:uId:sId:] + 22070684 (MHTutkIotcManager.m:129) 5 MiHome 0x00000001035b28d0 __63-[MHTUTKP2PConnection connectIOTCDeviceWithUId:did:completion:]_block_invoke + 22096080 (MHTUTKP2PConnection.m:533) 6 libdispatch.dylib 0x00000001c96496c8 _dispatch_call_block_and_release + 24 7 libdispatch.dylib 0x00000001c964a484 _dispatch_client_callout + 16 8 libdispatch.dylib 0x00000001c9620a7c _dispatch_queue_override_invoke + 664 9 libdispatch.dylib 0x00000001c962cafc _dispatch_root_queue_drain + 344 10 libdispatch.dylib 0x00000001c962d35c _dispatch_worker_thread2 + 116 11 libsystem_pthread.dylib 0x00000001c982c17c _pthread_wqthread + 472 12 libsystem_pthread.dylib 0x00000001c982ecec start_wqthread + 4
从Crash类型可以查询出来,Termination Reason: Namespace SPRINGBOARD, Code 0x8badf00d 代表主线程无响应,被watchdog杀掉了
主线程被阻塞,无非几种原因、死锁,方法执行耗时过长。
查询主线程,确实发现@synchronized(self) 加锁了,并且也是等锁导致等时间过长。子线程等方法中 使用了@synchronized加锁,内部操作长时间不返回,导致主线程被阻塞。
总结:
所有关于加锁的,一定要考虑加锁的粒度,同时考虑等锁等是不是主线程。
耗时过长的操作,设计线程安全的,可以放在队列中,使用队列解决。