简体   繁体   English

iOS:调试神秘的CFNetwork<redacted> 在后台获取期间在 com.apple.NSURLConnectionLoader 线程上崩溃

[英]iOS: Debugging mysterious CFNetwork <redacted> crash on com.apple.NSURLConnectionLoader thread during background fetch

I have started seeing this crash on a few devices in production.我已经开始在生产中的一些设备上看到这种崩溃。 The info provided by Fabric Crashlytics and iOS is very limited in this case and I'm not sure how to debug it.在这种情况下,Fabric Crashlytics 和 iOS 提供的信息非常有限,我不确定如何调试它。

The only thing that's common for the crashes is it happened on iPhone 5S / iOS 10.2.1, but that might be just a coincidence.崩溃的唯一共同点是它发生在 iPhone 5S / iOS 10.2.1 上,但这可能只是巧合。

What's worth mentioning is that I'm using Alamofire (4.3.0) where a similar problem should have been already fixed.值得一提的是,我正在使用Alamofire (4.3.0) ,其中应该已经修复了类似的问题

Crashlog:崩溃日志:

EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x0000000000000020

Crashed: com.apple.NSURLConnectionLoader
0  libobjc.A.dylib                0x189c400a0 objc_retain + 16
1  CFNetwork                      0x18b92d1d4 <redacted> + 240
2  CFNetwork                      0x18b888e68 <redacted> + 348
3  CFNetwork                      0x18b95dc80 <redacted> + 104
4  CFNetwork                      0x18b95dc0c <redacted> + 36
5  CFNetwork                      0x18b8f32ac <redacted> + 332
6  CFNetwork                      0x18b8f3120 <redacted> + 60
7  CFNetwork                      0x18b8f30b8 <redacted> + 268
8  CFNetwork                      0x18b865040 <redacted> + 116
9  CFNetwork                      0x18b7f7290 <redacted> + 48
10 CFNetwork                      0x18b7f71c4 <redacted> + 220
11 CFNetwork                      0x18b7f5550 <redacted> + 128
12 CFNetwork                      0x18b92ca7c <redacted> + 1904
13 CFNetwork                      0x18b92c23c <redacted> + 144
14 CFNetwork                      0x18b92e18c <redacted> + 28
15 libdispatch.dylib              0x18a07a1bc _dispatch_client_callout + 16
16 libdispatch.dylib              0x18a085ab0 _dispatch_block_invoke_direct + 376
17 CFNetwork                      0x18ba2a598 <redacted> + 36
18 CoreFoundation                 0x18b0c9c18 CFArrayApplyFunction + 68
19 CFNetwork                      0x18ba2a47c <redacted> + 136
20 CFNetwork                      0x18ba2b7a4 <redacted> + 312
21 CFNetwork                      0x18ba2b510 <redacted> + 64
22 CoreFoundation                 0x18b19eb5c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
23 CoreFoundation                 0x18b19e4a4 __CFRunLoopDoSources0 + 524
24 CoreFoundation                 0x18b19c0a4 __CFRunLoopRun + 804
25 CoreFoundation                 0x18b0ca2b8 CFRunLoopRunSpecific + 444
26 CFNetwork                      0x18b8cfa70 <redacted> + 336
27 Foundation                     0x18bd04e68 __NSThread__start__ + 1024
28 libsystem_pthread.dylib        0x18a285850 <redacted> + 240
29 libsystem_pthread.dylib        0x18a285760 _pthread_start + 282
30 libsystem_pthread.dylib        0x18a282d94 thread_start + 4

Update:更新:

After adding extra logging, I have found out that the crash is happening during Background App Refresh .添加额外的日志记录后,我发现崩溃发生在后台应用程序刷新期间。 According to the documentation the "app has up to 30 seconds of wall-clock time to perform the download operation and call the specified completion handler block".根据文档,“应用程序有长达 30 秒的挂钟时间来执行下载操作并调用指定的完成处理程序块”。

However, I can see in the logs that the crash occurs at the same time (the same second - I can't see the milliseconds in the crash logs) as the request is being fired.但是,我可以在日志中看到崩溃发生在请求被触发的同时(同一秒 - 我看不到崩溃日志中的毫秒数)。 In other words, there's almost no time between the system calls func application(_ application:, performFetchWithCompletionHandler: and the crash.换句话说,在系统调用func application(_ application:, performFetchWithCompletionHandler:和崩溃之间几乎没有时间。

Therefore, this should not be the case where the system kills the app for spending too much time in the background execution.因此,这不应该是系统因为在后台执行中花费太多时间而杀死应用程序的情况。

This has nothing to do with AlamoFire.这与 AlamoFire 无关。 The NSURLSession and NSURLConnection stack are insanely complex, and are largely written in C based on CoreFoundation, which means they also don't get the benefits of ARC. NSURLSession 和 NSURLConnection 堆栈非常复杂,并且主要是基于 CoreFoundation 用 C 编写的,这意味着它们也没有获得 ARC 的好处。 Chances are, this is a subtle multithreading bug somewhere deep in the stack that only occurs in certain circumstances, and is probably highly timing-dependent.很有可能,这是堆栈深处某处的一个微妙的多线程错误,仅在某些情况下才会发生,并且可能高度依赖于时间。

In this particular case, an object has gotten nilled out (probably by a zeroing weak reference somewhere) and some part of the CF stack is still using it, and tries to dereference the object to retain it using C-level APIs, resulting in a NULL pointer dereference.在这种特殊情况下,一个对象已经被清零(可能是由于某个地方的弱引用归零)并且 CF 堆栈的某些部分仍在使用它,并尝试使用 C 级 API 取消引用该对象以保留它,导致NULL 指针取消引用。

More specifically, you're probably seeing this crash: https://github.com/PhilipsHue/PhilipsHueSDK-iOS-OSX/issues/52更具体地说,您可能会看到此崩溃: https : //github.com/PhilipsHue/PhilipsHueSDK-iOS-OSX/issues/52

As for avoiding it, you are unlikely to be successful.至于避免它,你不太可能成功。 Your best bet is to make sure your app properly saves state and relaunches as quickly as possible from a cold start so that the difference between a cold start and a warm start doesn't matter.最好的办法是确保您的应用程序正确保存状态并从冷启动尽快重新启动,以便冷启动和热启动之间的区别无关紧要。

That said, one thing you might try is canceling all outstanding foreground connections as soon as you get notified that you're going into the background, and then restarting them in a session with discretionary set to YES.也就是说,您可以尝试的一件事是,一旦收到您将进入后台的通知,就立即取消所有未完成的前台连接,然后在discretionary设置为 YES 的会话中重新启动它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM