IOS Crash with SIGABRT on CFNetwork

I use my own url protocol in my application, extending NSURLProtocol. It works fine in most cases, but I see that the next crash is being reported using crash tests. I can not reproduce it myself. What bothers me the most: I don’t see my application in the stack trace of a broken thread, and therefore I don’t know where to start debugging. Below is a crash report

Thread : Crashed: com.apple.NSURLConnectionLoader 0 libsystem_kernel.dylib 0x3562cc84 __pthread_kill + 8 1 libsystem_pthread.dylib 0x356d0733 pthread_kill + 62 2 libsystem_c.dylib 0x355c4f21 abort + 108 3 libsystem_c.dylib 0x355a47eb __assert_rtn + 302 4 CFNetwork 0x22b82e45 CFURLProtocol_NS::_protocolInterface_cancelLoad() + 322 5 CFNetwork 0x22c3740f ___ZN19URLConnectionLoader27_private_ScheduleOriginLoadEPK12NSURLRequestPK20_CFCachedURLResponse_block_invoke_2 + 38 6 CFNetwork 0x22b66ccd ___ZNK19URLConnectionLoader25withExistingProtocolAsyncEU13block_pointerFvP11URLProtocolE_block_invoke + 16 7 libdispatch.dylib 0x35513bd7 _dispatch_client_callout + 22 8 libdispatch.dylib 0x3551d187 _dispatch_block_invoke$VARIANT$mp + 446 9 CFNetwork 0x22b66caf RunloopBlockContext::_invoke_block(void const*, void*) + 18 10 CoreFoundation 0x2326ab51 CFArrayApplyFunction + 36 11 CFNetwork 0x22b66b97 RunloopBlockContext::perform() + 182 12 CFNetwork 0x22b66a61 MultiplexerSource::perform() + 216 13 CFNetwork 0x22b668f9 MultiplexerSource::_perform(void*) + 48 14 CoreFoundation 0x23319bff __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 14 15 CoreFoundation 0x233197ed __CFRunLoopDoSources0 + 452 16 CoreFoundation 0x23317b5b __CFRunLoopRun + 794 17 CoreFoundation 0x2326b119 CFRunLoopRunSpecific + 520 18 CoreFoundation 0x2326af05 CFRunLoopRunInMode + 108 19 CFNetwork 0x22bd8bbf +[NSURLConnection(Loader) _resourceLoadLoop:] + 486 20 Foundation 0x241291b5 __NSThread__start__ + 1148 21 libsystem_pthread.dylib 0x356cf85b _pthread_body + 138 22 libsystem_pthread.dylib 0x356cf7cf _pthread_start + 110 23 libsystem_pthread.dylib 0x356cd724 thread_start + 8 

The intriguing part in the stack trace is a few lines.

 4 CFNetwork 0x22b82e45 CFURLProtocol_NS::_protocolInterface_cancelLoad() + 322 5 CFNetwork 0x22c3740f ___ZN19URLConnectionLoader27_private_ScheduleOriginLoadEPK12NSURLRequestPK20_CFCachedURLResponse_block_invoke_2 + 38 6 CFNetwork 0x22b66ccd ___ZNK19URLConnectionLoader25withExistingProtocolAsyncEU13block_pointerFvP11URLProtocolE_block_invoke + 16 

I can determine by looking at the stack traces on the startLoading and stopLoading methods in NSURLProtocol that ___ZN19URLConnectionLoader27_private_ScheduleOriginLoadEPK12NSURLRequestPK20_CFCachedURLResponse_block_invoke_2 call_Lead_Locate_Load_Load_Load_Locate_Load_Load_Load_Location_Locate_Load_Load_Locate_Locate_Locate_Lock.png. So, why and how is the call canceled right after the download attempt?

Any help is appreciated. Thank you

Update:

I can reproduce a similar (not an exact track), and I see the following statement.

 Assertion failed: (_protocolInstance == nil), function _protocolInterface_startLoad, file /BuildRoot/Library/Caches/com.apple.xbs/Sources/CFNetwork/CFNetwork-758.0.2/Session/LocalSession.mm, line 1341. 

and the next is backtracking.

 (lldb) bt * thread #6: tid = 0xe687, 0x34515d24 libsystem_kernel.dylib`__pthread_kill + 8, name = 'com.apple.NSURLConnectionLoader', stop reason = signal SIGABRT * frame #0: 0x34515d24 libsystem_kernel.dylib`__pthread_kill + 8 frame #1: 0x345b974a libsystem_pthread.dylib`pthread_kill + 62 frame #2: 0x344adf40 libsystem_c.dylib`abort + 108 frame #3: 0x3448d80a libsystem_c.dylib`__assert_rtn + 302 frame #4: 0x2202de4c CFNetwork`CFURLProtocol_NS::_protocolInterface_startLoad(_CFCachedURLResponse const*) + 324 frame #5: 0x220e22e6 CFNetwork`___ZN19URLConnectionLoader27_private_ScheduleOriginLoadEPK12NSURLRequestPK20_CFCachedURLResponse_block_invoke_2 + 38 frame #6: 0x22011cd4 CFNetwork`___ZNK19URLConnectionLoader25withExistingProtocolAsyncEU13block_pointerFvP11URLProtocolE_block_invoke + 16 frame #7: 0x003a5d72 libdispatch.dylib`_dispatch_client_callout + 22 frame #8: 0x003ad8d8 libdispatch.dylib`_dispatch_block_invoke + 468 frame #9: 0x22011cb6 CFNetwork`RunloopBlockContext::_invoke_block(void const*, void*) + 18 frame #10: 0x22710c80 CoreFoundation`CFArrayApplyFunction + 36 frame #11: 0x22011b9e CFNetwork`RunloopBlockContext::perform() + 182 frame #12: 0x22011a68 CFNetwork`MultiplexerSource::perform() + 216 frame #13: 0x22011900 CFNetwork`MultiplexerSource::_perform(void*) + 48 frame #14: 0x227bfc3e CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 14 frame #15: 0x227bf7c0 CoreFoundation`__CFRunLoopDoSources0 + 344 frame #16: 0x227bdb9a CoreFoundation`__CFRunLoopRun + 794 frame #17: 0x22711248 CoreFoundation`CFRunLoopRunSpecific + 520 frame #18: 0x22711034 CoreFoundation`CFRunLoopRunInMode + 108 frame #19: 0x22083ee6 CFNetwork`+[NSURLConnection(Loader) _resourceLoadLoop:] + 486 frame #20: 0x235cc634 Foundation`__NSThread__start__ + 1148 frame #21: 0x345b8872 libsystem_pthread.dylib`_pthread_body + 138 frame #22: 0x345b87e6 libsystem_pthread.dylib`_pthread_start + 110 frame #23: 0x345b6740 libsystem_pthread.dylib`thread_start + 8 

Here is the above version of my protocol class, where MyConnection works very similar to NSURLConnection ..

@implementation MyProtocol

 - (void) startLoading { NSURLRequest *request = self.request; self.myConnection = [[MyConnection alloc] initWithRequest:request delegate:self startImmediately:NO]; NSRunLoop *loop = [NSRunLoop currentRunLoop]; [self.myConnection scheduleInRunLoop:loop forMode:loop.currentMode]; [self.myConnection start]; } - (void) stopLoading { if (self.myConnection) { [self.myConnection cancel]; self.myConnection = nil; } } #pragma mark MyConnectionDelegate - (void) myConnection:(MyConnection *)connection didReceiveData:(NSData *)data { [self.client URLProtocol:self didLoadData:data]; } - (void) myConnectionDidFinishLoading:(MyConnection *)connection { [self.client URLProtocolDidFinishLoading:self]; } //..other delegate methods are implemented similarly 

Between, the exception code that I see is

 Exception Type: EXC_CRASH (SIGABRT) Exception Codes: 0x0000000000000000, 0x0000000000000000 Exception Note: EXC_CORPSE_NOTIFY Triggered by Thread: 4 
+7
ios ios9 sigabrt cfnetwork nsurlprotocol
source share
2 answers

I don’t know if you already do this, but maybe try to force the start and cancel to be in the same thread?

Try using -com.apple.CoreData.ConcurrencyDebug 1 for debugging - I'm not sure if it only debugs a CoreData thread, but it's worth a try.

0
source share

You can always do all the work of your protocol class in the main thread using the performSelectorOnMainThread function (or on some well-known thread) and see if this helps.

In addition, make sure that you have marked the request and that you refuse to process the tags requested earlier so that you do not receive an endless loop of creating a user protocol handler.

In addition, you will probably need to open a DTS-enabled incident to find out which statement fails. Unfortunately, CFNetwork is not one of the components of open source, otherwise it would be easy to debug.

0
source share

All Articles