_dispatch_assert_queue_fail under iOS 16.4.1. Don't understand the error.

Since updating to iOS 16.4.1, our app crashes randomly with the following error message.

Exception Codes: 0x0000000000000001, 0x0000000199d542c0
Termination Reason: SIGNAL 5 Trace/BPT trap: 5
Terminating Process: exc handler [499]

Triggered by Thread:  2

Thread 2 Crashed:
0   libdispatch.dylib             	       0x199d542c0 _dispatch_assert_queue_fail + 120
1   libdispatch.dylib             	       0x199d54248 dispatch_assert_queue + 196
2   UIKitCore                     	       0x1949c870c -[UIImageView _mainQ_beginLoadingIfApplicable] + 88
3   UIKitCore                     	       0x19495db34 -[UIImageView setHidden:] + 68
4   UIKitCore                     	       0x19492f128 -[UIButtonLegacyVisualProvider _updateImageView] + 372
5   UIKitCore                     	       0x19492ed78 -[UIButtonLegacyVisualProvider layoutSubviews] + 116
6   UIKitCore                     	       0x19492ecc4 -[UIButton layoutSubviews] + 40
7   UIKitCore                     	       0x1948fbef4 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1920
8   QuartzCore                    	       0x193db64ac CA::Layer::layout_if_needed(CA::Transaction*) + 500
9   QuartzCore                    	       0x193dc9a28 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 148
10  QuartzCore                    	       0x193ddae54 CA::Context::commit_transaction(CA::Transaction*, double, double*) + 444
11  QuartzCore                    	       0x193e0a3c0 CA::Transaction::commit() + 648
12  QuartzCore                    	       0x193e54b08 CA::Transaction::release_thread(void*) + 228
13  libsystem_pthread.dylib       	       0x1f2214b9c _pthread_tsd_cleanup + 620
14  libsystem_pthread.dylib       	       0x1f2217560 _pthread_exit + 84
15  libsystem_pthread.dylib       	       0x1f22140cc _pthread_wqthread_exit + 80
16  libsystem_pthread.dylib       	       0x1f2213e64 _pthread_wqthread + 424
17  libsystem_pthread.dylib       	       0x1f2213b7c start_wqthread + 8

I have trouble interpreting where our error lies within the exception. Can anyone give me a hint as to what kind of problem this might be?

Kind regards

Answered by Frameworks Engineer in 752540022

The most likely cause is that you manipulated a UIButton from a secondary thread, and now that the thread is exiting, Core Animation is committing a transaction due to your manipulation, resulting in this eventual assertion.

So the immediate cause of this crash is a Dispatch queue assert. UIImageView, in frame 2, has called dispatch_assert_queue to assert that it’s on the main queue. It isn’t on the main queue, and thus Dispatch has trapped.

As to why it’s doing this from a secondary queue, that’s very much a UIKit question and thus kinda outside of my area of expertise. I’ve added the UIKit tag to your thread to see if that might garner some other eyes.

Share and Enjoy

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

Accepted Answer

The most likely cause is that you manipulated a UIButton from a secondary thread, and now that the thread is exiting, Core Animation is committing a transaction due to your manipulation, resulting in this eventual assertion.

You are accessing a UIKit API from a secondary thread. You can try reproducing the error with the Main Thread Checker enabled (In your scheme -> Diagnostics -> Main Thread Checker).

I got a similar _dispatch_assert_queue_fail crash but the stack trace looks a bit different. I couldn't find the root cause since the stack trace does not show anything related to our code. Do you have any idea?

Interestingly, this crash happens only in iOS 18+.

Note: I attached the full stack

trace
-[UIImageView _mainQ_beginLoadingIfApplicable]
_dispatch_assert_queue_fail

Crashed: com.apple.SwiftUI.AsyncRenderer
EXC_BREAKPOINT 0x000000019c21e3b4
        
Crashed: com.apple.SwiftUI.AsyncRenderer
0  libdispatch.dylib              0x63b4 _dispatch_assert_queue_fail + 120
1  libdispatch.dylib              0x633c _dispatch_assert_queue_fail + 194
2  UIKitCore                      0xb80c4 -[UIImageView _mainQ_beginLoadingIfApplicable] + 76
3  UIKitCore                      0xb7fb4 -[UIImageView setHidden:] + 68
4  UIKitCore                      0xb7d70 -[UIButtonLegacyVisualProvider _updateImageView] + 372
5  UIKitCore                      0xe8830 -[UIButtonLegacyVisualProvider baselineOffsetsAtSize:] + 196
6  UIKitCore                      0xe8748 -[UIButton _baselineOffsetsAtSize:] + 52
7  SwiftUI                        0x29a48 PlatformViewLayoutEngine.explicitAlignment(_:at:) + 124
8  SwiftUI                        0x299c0 protocol witness for LayoutEngine.explicitAlignment(_:at:) in conformance PlatformViewLayoutEngine<A> + 16
9  SwiftUICore                    0x136f4 LayoutEngineBox.explicitAlignment(_:at:) + 120
10 SwiftUICore                    0x135a4 LayoutProxy.explicitAlignment(_:at:) + 244
11 SwiftUICore                    0x129d4 UnaryLayoutEngine.explicitAlignment(_:at:) + 208
12 SwiftUICore                    0x13718 protocol witness for LayoutEngine.explicitAlignment(_:at:) in conformance UnaryLayoutEngine<A> + 16
13 SwiftUICore                    0x136f4 LayoutEngineBox.explicitAlignment(_:at:) + 120
14 SwiftUICore                    0x12130 StackLayout.UnmanagedImplementation.explicitAlignment(_:at:) + 348
15 SwiftUICore                    0x307b8 StackLayout.explicitAlignment(_:in:proposal:) + 136

...

57 SwiftUICore                    0x1b5000 ViewGraph.updateOutputsAsync(at:) + 560
58 SwiftUICore                    0x1b45d4 ViewRendererHost.renderAsync(interval:targetTimestamp:) + 524
59 SwiftUI                        0x69d40 UIHostingViewBase.displayLinkTimer(timestamp:targetTimestamp:isAsyncThread:) + 256
60 SwiftUI                        0x691d8 DisplayLink.displayLinkTimer(_:) + 280
61 SwiftUI                        0x69094 @objc DisplayLink.displayLinkTimer(_:) + 60
62 QuartzCore                     0xec1a4 CA::Display::DisplayLinkItem::dispatch_(CA::SignPost::Interval<(CA::SignPost::CAEventCode)835322056>&) + 48
63 QuartzCore                     0xebb78 CA::Display::DisplayLink::dispatch_items(unsigned long long, unsigned long long, unsigned long long) + 884
64 QuartzCore                     0xc3b58 display_timer_callback(__CFMachPort*, void*, long, void*) + 456
65 CoreFoundation                 0x79b68 __CFMachPortPerform + 176
66 CoreFoundation                 0x79aa4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 60
67 CoreFoundation                 0x799c4 __CFRunLoopDoSource1 + 524
68 CoreFoundation                 0x762ac __CFRunLoopRun + 2248
69 CoreFoundation                 0xc8274 CFRunLoopRunSpecific + 588
70 Foundation                     0x29b48 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 212
71 Foundation                     0x25d10 -[NSRunLoop(NSRunLoop) run] + 64
72 SwiftUI                        0x375f60 specialized static DisplayLink.asyncThread(arg:) + 792
73 SwiftUI                        0x375c30 @objc static DisplayLink.asyncThread(arg:) + 72
74 Foundation                     0x115a54 __NSThread__start__ + 724
75 libsystem_pthread.dylib        0x17d0 _pthread_start + 136
76 libsystem_pthread.dylib        0x1480 thread_start + 8
_dispatch_assert_queue_fail under iOS 16.4.1. Don't understand the error.
 
 
Q