crash iOS 14 - Unbalanced call to dispatch_group_leave()

I have received this report through firebase analytics(Crashlytics) and I have no idea what it is. Would you help me?
The problem is only in iOS 14. (iOS 14.0.0, 14.2.0)
There is no such case in versions below iOS 14

Code Block language
crash_info_entry_0
BUG IN CLIENT OF LIBDISPATCH: Unbalanced call to dispatch_group_leave()


Stack trace

Crashed: Thread #1
EXCBREAKPOINT 0x000000019836f7f4

Crashed: Thread
0 libdispatch.dylib 0x19836f7f4 <redacted> + 36
1 libdispatch.dylib 0x19833d8c4 dispatch
groupleave + 126
2 ??? 0x754b330102689c00 (Missing)
3 Life4MePlus 0x102682b8c -[AISynchronizationOperation main] + 38 (AISynchronizationOperation.m:38)
4 Foundation 0x199a9c0f0 <redacted> + 864
5 ??? 0x0 (Missing)
6 ??? 0x221c4e81dfc97800 (Missing)

com.apple.main-thread
com.apple.main-thread
0 libsystem
kernel.dylib 0x1c4643dd0 machmsgtrap + 8
1 libsystemkernel.dylib 0x1c4643184 machmsg + 76
2 ??? 0x4b112781986bac00 (Missing)
3 ??? 0x31204581986b4e00 (Missing)
4 ??? 0x0 (Missing)
5 ??? 0x0 (Missing)
6 ??? 0x0 (Missing)
7 ??? 0x0 (Missing)
8 ??? 0x0 (Missing)
9 libdyld.dylib 0x19837be60 <redacted> + 4

Thread #2
Thread
0 libsystemkernel.dylib 0x1c46685bc workqkernreturn + 8
1 libsystempthread.dylib 0x1dfc90954 pthreadwqthread + 352
2 ??? 0x5d3bd101dfc97800 (Missing)

Thread #3
Thread
0 libsystem
kernel.dylib 0x1c46685bc _workqkernreturn + 8
1 libsystempthread.dylib 0x1dfc90954 pthreadwqthread + 352
2 ??? 0x0 (Missing)

Thread #4
Thread
0 libsystem
kernel.dylib 0x1c46685bc _workqkernreturn + 8
1 libsystempthread.dylib 0x1dfc90954 pthreadwqthread + 352
2 ??? 0x3453b581dfc97800 (Missing)

com.apple.uikit.eventfetch-thread
com.apple.uikit.eventfetch-thread
0 libsystem
kernel.dylib 0x1c4643dd0 machmsgtrap + 8
1 libsystemkernel.dylib 0x1c4643184 machmsg + 76
2 ??? 0x0 (Missing)
3 ??? 0x61681c01986b5000 (Missing)
4 ??? 0x0 (Missing)
5 ??? 0x0 (Missing)
6 ??? 0x0 (Missing)
7 ??? 0x4a214b819b109000 (Missing)
8 ??? 0x15469a8199a9c100 (Missing)
9 ??? 0x0 (Missing)
10 ??? 0x0 (Missing)

Thread #5
Thread
0 libsystemkernel.dylib 0x1c46685bc workqkernreturn + 8
1 libsystempthread.dylib 0x1dfc90954 pthreadwqthread + 352
2 ??? 0x0 (Missing)

Thread #6
Thread
0 libsystem
kernel.dylib 0x1c46685bc _workqkernreturn + 8
1 libsystempthread.dylib 0x1dfc90954 pthreadwqthread + 352
2 ??? 0x0 (Missing)

Thread #7
Thread
0 libsystem
kernel.dylib 0x1c46685bc _workqkernreturn + 8
1 libsystempthread.dylib 0x1dfc90954 pthreadwqthread + 352
2 ??? 0x0 (Missing)

com.twitter.crashlytics.ios.MachExceptionServer
com.twitter.crashlytics.ios.MachExceptionServer
0 Life4MePlus 0x10277e5dc CLSProcessRecordAllThreads + 376 (CLSProcess.c:376)
1 Life4MePlus 0x10277e5dc CLSProcessRecordAllThreads + 376 (CLSProcess.c:376)
2 Life4MePlus 0x10277ea50 CLSProcessRecordAllThreads + 407 (CLSProcess.c:407)
3 Life4MePlus 0x10276ee60 CLSHandler + 26 (CLSHandler.m:26)
4 Life4MePlus 0x10276a204 CLSMachExceptionServer + 446 (CLSMachException.c:446)
5 libsystem
pthread.dylib 0x1dfc8eca8 pthreadstart + 320
6 ??? 0x7c25b581dfc97800 (Missing)

AFRKNetworking
AFRKNetworking
0 libsystemkernel.dylib 0x1c4643dd0 machmsgtrap + 8
1 libsystem
kernel.dylib 0x1c4643184 machmsg + 76
2 ??? 0x3713a981986bac00 (Missing)
3 ??? 0x0 (Missing)
4 ??? 0x684cc081986b4400 (Missing)
5 ??? 0x7237968199932000 (Missing)
6 ??? 0x6813c40199964800 (Missing)
7 ??? 0x452e2b010368f800 (Missing)
8 Foundation 0x199a9c0f0 <redacted> + 864
9 ??? 0x0 (Missing)
10 ??? 0x710e8a81dfc97800 (Missing)

Thread #8
Thread
0 libsystem
pthread.dylib 0x1dfc97774 <redacted> + 6

com.apple.NSURLConnectionLoader
com.apple.NSURLConnectionLoader
0 libsystemkernel.dylib 0x1c4643dd0 machmsgtrap + 8
1 libsystem
kernel.dylib 0x1c4643184 mach_msg + 76
2 ??? 0x0 (Missing)
3 ??? 0x0 (Missing)
4 ??? 0x60677381986b4400 (Missing)
5 ??? 0x386bc00198f6d000 (Missing)
6 ??? 0x552580199a9c100 (Missing)
7 ??? 0x47378301dfc8ec00 (Missing)
8 ??? 0x0 (Missing)

Is frame 3 of the crashed thread your code? If so, it seems like that you have a bug with how you handle Dispatch groups.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Have you solved this issue?

I am facing the same issue with no luck so far.
I have a theory about the completion handler taking around 30s and crashing because of that, but since It only happens on iOS 14 I don't think It could be

I encountered the same problem.This crash only apears above iOS14,including iOS15.There is no such case in versions below iOS 14 too And there is not frame about the function of my app on the top of the crashed stack trace.So I can hardly find why this crash happends.

Crashed: com.apple.main-thread 0 libdispatch.dylib 0x19b7b87b8

dispatch_group_leave.cold.1 + 36 1 libdispatch.dylib 0x19b784638 _dispatch_group_wake + 138

2 libdispatch.dylib 0x19b781a54 _dispatch_call_block_and_release + 32

3 libdispatch.dylib 0x19b7837ec _dispatch_client_callout + 20

4 libdispatch.dylib 0x19b791c40 _dispatch_main_queue_callback_4CF + 884

5 CoreFoundation 0x19bb101f8 CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE + 16

6 CoreFoundation 0x19bb0a0d0 __CFRunLoopRun + 2524

7 CoreFoundation 0x19bb091c0 CFRunLoopRunSpecific + 600

8 GraphicsServices 0x1b30f1734 GSEventRunModal + 164

9 UIKitCore 0x19e5777e4 -[UIApplication _run] + 1072

10 UIKitCore 0x19e57d054 UIApplicationMain + 168

11 MyApp 0x1022eae98 main + 22 (main.m:22)

12 libdyld.dylib 0x19b7c5cf8 start + 4

The immediate cause of this crash is clear: Some code within your process is using a Dispatch group in an unbalanced way. For example, this code will trigger the crash:

let g = DispatchGroup()
g.enter()
g.leave()
g.leave()

As to why it’s happening in your app, it’s hard to say. Can you post a full Apple crash report? Use the text attachment feature (click the paperclip icon and then choose Add File) to avoid clogging up the timeline.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I have checked all call to dispatch_group_leave in my project

All the source code, right? This problem could be caused by a framework for which you don’t have source, either an Apple one built in to the OS or a third-party one that you bundle in your app.

Please, can I do anything else to help you locate the crash ?

Nothing that immediately springs to mind. An Apple crash report might offer some further insight into this, but even that’s uncertain.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I also encountered a very similar issue. And now, it's fixed. Hope my findings can be helpful.

By adding event tracks, I finally found the completion handlers of these functions are eventually called more than once in several async tasks!

1. 
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler;

2. 
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler;

BTW, to help me investigate this issue, I wrapped the completion handler and passed the wrapping handler to the downstream. So, when the downside calls the wrapping completion handler, I can count the times of calling it. Then, if the wrapping completion handler is called before, it should return without calling the wrapped completion handler again.

crash iOS 14 - Unbalanced call to dispatch_group_leave()
 
 
Q