Contacts Framework Frequently Hangs on iOS 11

On iOS 11 only, the Contacts framework will frequently hang when calling the "enumerateContactsWithFetchRequest" method. It seems to happen more often when fetching a large number of contacts. The log reads "Error: service connection to com.apple.contactsd was interrupted." See attached call stack from the main thread.


If you're noticing the same behavior, please let Apple know so they make it a priority to get it fixed. I've reported the issue using Bug Reporter: https://bugreport.apple.com/web/?problemID=34671273


Many users are reporting that iOS 11 is slow, and this may be part of the issue. Apple apparently rewrote much of the Contacts Framework for iOS 11, and clearly there are bugs. Most operations are significantly slower than iOS 10.


The relevant callstack on the main thread is shown below:


#0 0x000000018569cbc4 in mach_msg_trap ()

#1 0x000000018569ca3c in mach_msg ()

#2 0x0000000104bce7e8 in _dispatch_mach_send_and_wait_for_reply ()

#3 0x0000000104bcec90 in dispatch_mach_send_with_result_and_wait_for_reply ()

#4 0x000000018580b9cc in xpc_connection_send_message_with_reply_sync ()

#5 0x00000001866ba3a8 in __NSXPCCONNECTION_IS_WAITING_FOR_A_SYNCHRONOUS_REPLY__ ()

#6 0x00000001866b9b30 in -[NSXPCConnection _sendInvocation:orArguments:count:methodSignature:selector:withProxy:] ()

#7 0x00000001866b8ae0 in -[NSXPCConnection _sendSelector:withProxy:arg1:arg2:] ()

#8 0x00000001866c5ef8 in _NSXPCDistantObjectSimpleMessageSend2 ()

#9 0x000000019078cd3c in -[CNXPCDataMapper fetchEncodedContactsForFetchRequest:error:cancelationToken:batchHandler:] ()

#10 0x000000019078c950 in -[CNXPCDataMapper fetchAndDecodeEncodedContactsForFetchRequest:error:cancelationToken:batchHandler:] ()

#11 0x000000019078c2bc in __52-[CNXPCDataMapper contactObservableForFetchRequest:]_block_invoke ()

#12 0x000000018d833f60 in __26-[CNObservable doOnError:]_block_invoke ()

#13 0x000000018d833ba0 in __31-[CNObservable doOnCompletion:]_block_invoke ()

#14 0x000000018d833ba0 in __31-[CNObservable doOnCompletion:]_block_invoke ()

#15 0x000000018d833f60 in __26-[CNObservable doOnError:]_block_invoke ()

#16 0x000000018d833744 in __27-[CNObservable doOnCancel:]_block_invoke ()

#17 0x000000018d834d18 in -[CNObservable enumerateObjectsUsingBlock:] ()

#18 0x00000001907dc99c in __91-[CNDataMapperContactStore enumerateContactsAndMatchInfoWithFetchRequest:error:usingBlock:]_block_invoke ()

#19 0x00000001857ecfac in os_activity_apply_f ()

#20 0x0000000190831bf0 in -[_CNContactsLogger fetchingContacts:] ()

#21 0x00000001907dc6b0 in -[CNDataMapperContactStore enumerateContactsAndMatchInfoWithFetchRequest:error:usingBlock:] ()

#22 0x00000001907b568c in -[CNContactStore enumerateContactsWithFetchRequest:error:usingBlock:] ()

Replies

I am experiencing the same issue with iOS 11. Both Contact and Address Book framework are doing the same.

I can get it to happen fairly frequently on my end, but Apple engineers are having a hard time reproducing it over there. Do you have a 100% repro case?

Sure, just load your iCloud account with 10,000 contacts.

Also, here are some insight, if you are using address book framework instead of Contact framework you'll see lots of warnings like below. It seems like Apple put a throttle mechanism for apps that enumerate large amount of contacts.

(Please do update here if you ever heard back from Apple. Thx)


Fetching in bulk 250 contacts!

Fetching in bulk 250 contacts!

Fetching in bulk 250 contacts!

Fetching in bulk 250 contacts!

...

Did you get any response from Apple regarding to this?

I'm seeing the same thing, or something like it. It's fairly rare/random, but seems to happen only on the first enumeration after app activation. My main test case has about 700 contacts, fwiw.

Did you ever hear back from Apple on this? I am seeing the same issue. Our user feedback suggests it's usually when there are more than ~2000 contacts and once the user has started hitting the issue it will happen every time the app is started, which is a bit of a problem for us!

I've noticed that it doesn't repro for me when running the app from the Xcode debugger. The same app build and device (iPhone 6) reproduce reliably when the debugger is not connected. Maybe Apple are struggling to repro because they're using a simulator or debugger?


Also I can't reproduce it on iPhone X.

Probably I see the same issue on an iPhone 8 Plus with iOS 11.2. The error: service connection to com.apple.contactsd was interrupted. Error communicating with XPC Service: Error Domain=NSCocoaErrorDomain Code=4097 "connection to service named com.apple.contactsd" UserInfo={NSDebugDescription=connection to service named com.apple.contactsd}

Although the hang was supposedly "fixed" as of iOS 11.2, it was replaced with an even worse bug. Rather than the contact fetch hanging/stalling, it now returns just a fraction of a user's contacts, and does so without returning a NSError. In my testing, under the right circumstances, contact fetches that should return 6,000 contacts only return 1,000 contacts!


I will write up another bug. It's sad that Apple's engineering team is having trouble with something as seemingly simple as a contact fetch...

We see same things as DonaldL and we have a lot of user complaints. Since iOS 11.2 contact fetch returns some random fraction of the address book. From our observation, it reproduces more often with the large overall number of contacts. Does anyone have some kind of stable workaround? Using some other API maybe? We tried to ignore contact fetches that suddenly return fewer contacts than a previous attempt (for up to 5 times), but still for some users it doesn't help. Apple doesn't fix it for a long time already, while our customers are suffering...

I submitted a bug using Bug Reporter: https://bugreport.apple.com/web/?problemID=36534009


Let's hope they fix it as soon as possible, hopefully with iOS 11.3

With the release of 11.3, I seem to not be running into this issue anymore. How about you?

I still have this problem with iOS 13. Has it been fixed?

Crashed: com.p1.addressbook
0 libsystem
kernel.dylib 0x183e55348 _pthreadkill + 8
1 libsystempthread.dylib 0x183f6d794 pthreadkill$VARIANT$armv81 + 360
2 libsystemc.dylib 0x183dc4fb8 abort + 140
3 libsystem
malloc.dylib 0x183e9ba08 nanozonedefaultreader + 330
4 libsystemmalloc.dylib 0x183e9cd20 nanomalloccheckclear + 412
5 libsystem
malloc.dylib 0x183e9bce0 nanocalloc + 80
6 libsystem
malloc.dylib 0x183e8d4f0 malloczonecalloc + 168
7 libsystemmalloc.dylib 0x183e8d424 calloc + 40
8 libobjc.A.dylib 0x18359845c class
createInstance + 76
9 libdispatch.dylib 0x183cc10c0 osobjectallocrealized + 40
10 libxpc.dylib 0x183fa2be4 xpcarraydeserialize + 104
11 libxpc.dylib 0x183fa2e70
xpcdictionarydeserializeapply + 48
12 libxpc.dylib 0x183fa24d8
xpcdictionaryapplywiref + 516
13 libxpc.dylib 0x183fa2e18 xpcdictionarydeserialize + 116
14 libxpc.dylib 0x183fa1944
xpcserializerunpack + 772
15 libxpc.dylib 0x183fa1514 xpcconnectionunpackmessage + 100
16 libxpc.dylib 0x183fa3f78 xpcconnectionsendmessagewithreplysync + 332
17 Foundation 0x184e553a8 NSXPCCONNECTIONISWAITINGFORASYNCHRONOUSREPLY + 12
18 Foundation 0x184e54b30 -[NSXPCConnection sendInvocation:orArguments:count:methodSignature:selector:withProxy:] + 3604
19 Foundation 0x184e53ae0 -[NSXPCConnection
sendSelector:withProxy:arg1:arg2:] + 136
20 Foundation 0x184e60ef8 NSXPCDistantObjectSimpleMessageSend2 + 64
21 Contacts 0x18ef6db80 -[CNXPCDataMapper fetchEncodedContactsForFetchRequest:error:cancelationToken:batchHandler:] + 304
22 Contacts 0x18ef6d860 -[CNXPCDataMapper fetchAndDecodeEncodedContactsForFetchRequest:error:cancelationToken:batchHandler:] + 308
23 Contacts 0x18ef6d1cc
52-[CNXPCDataMapper contactObservableForFetchRequest:]blockinvoke + 256
24 ContactsFoundation 0x18bff1ed0
26-[CNObservable doOnError:]blockinvoke + 260
25 ContactsFoundation 0x18bff1b10
31-[CNObservable doOnCompletion:]blockinvoke + 272
26 ContactsFoundation 0x18bff1b10
31-[CNObservable doOnCompletion:]blockinvoke + 272
27 ContactsFoundation 0x18bff1ed0
26-[CNObservable doOnError:]blockinvoke + 260
28 ContactsFoundation 0x18bff16b4
27-[CNObservable doOnCancel:]blockinvoke + 228
29 ContactsFoundation 0x18bff2c88 -[CNObservable enumerateObjectsUsingBlock:] + 388
30 Contacts 0x18efbd8ac
91-[CNDataMapperContactStore enumerateContactsAndMatchInfoWithFetchRequest:error:usingBlock:]blockinvoke + 512
31 libsystem
trace.dylib 0x183f84fa4 osactivityapplyf + 84
32 Contacts 0x18f012ae4 -[
CNContactsLogger fetchingContacts:] + 208
33 Contacts 0x18efbd5c0 -[CNDataMapperContactStore enumerateContactsAndMatchInfoWithFetchRequest:error:usingBlock:] + 300
34 Contacts 0x18ef9659c -[CNContactStore enumerateContactsWithFetchRequest:error:usingBlock:] + 168
35 putong 0x1033f9b3c -[PUGContactAddressBookManager fetchAddressBookPersonsFromAddressBook:] + 223 (PUGContactAddressBookManager.m:223)
36 putong 0x1033f9298 -[PUGContactAddressBookManager synchronizeAddressBookDatabase] + 164 (PUGContactAddressBookManager.m:164)
37 putong 0x1033f91dc -[PUGContactAddressBookManager updateAddressBookContactsWithCompletion:] + 152 (PUGContactAddressBookManager.m:152)
38 putong 0x1033f8fa4 _58-[PUGContactAddressBookManager updateAddressBookContacts:]blockinvoke2 + 107 (PUGContactAddressBookManager.m:107)
39 libdispatch.dylib 0x183cc1088 dispatchcallblockandrelease + 24
40 libdispatch.dylib 0x183cc1048
dispatchclientcallout + 16
41 libdispatch.dylib 0x183cff1c0 dispatchqueueserialdrain$VARIANT$armv81 + 520
42 libdispatch.dylib 0x183cffb30 dispatchqueueinvoke$VARIANT$armv81 + 340
43 libdispatch.dylib 0x183d00528
dispatchrootqueuedraindeferredwlh$VARIANT$armv81 + 380
44 libdispatch.dylib 0x183d085c4
dispatchworkloopworkerthread$VARIANT$armv81 + 640
45 libsystem
pthread.dylib 0x183f66fd0 pthreadwqthread + 932
46 libsystempthread.dylib 0x183f66c20 startwqthread + 4