UI freeze during layouting

One of our users reported a very strange bug where our app freezes and eventually crashes on some screen transitions.

From different crash logs we could determine that the app freezes up when we call view.layoutIfNeeded() for animating constraint changes. It then gets killed by the watchdog 10 seconds later:

Exception Type: EXC_CRASH (SIGKILL)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Termination Reason: FRONTBOARD 2343432205
<RBSTerminateContext| domain:10 code:0x8BADF00D explanation:scene-update watchdog transgression: app<bundleID(2A01F261-3554-44C0-B5A9-EBEB446484AD)>:6921 exhausted real (wall clock) time allowance of 10.00 seconds
ProcessVisibility: Background
ProcessState: Running
WatchdogEvent: scene-update
WatchdogVisibility: Background
WatchdogCPUStatistics: (
"Elapsed total CPU time (seconds): 24.320 (user 18.860, system 5.460), 29% CPU",
"Elapsed application CPU time (seconds): 10.630, 12% CPU"
) reportType:CrashLog maxTerminationResistance:Interactive>

The crash stack trace looks slightly different, depending on the UI transition that is happening. Here are the two we observed so far. Both are triggered by the layoutIfNeeded() call.

Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 CoreAutoLayout 0x1b09f90e4 -[NSISEngine valueForEngineVar:] + 8
1 UIKitCore 0x18f919478 -[_UIViewLayoutEngineRelativeAlignmentRectOriginCache origin] + 372
2 UIKitCore 0x18f918f18 -[UIView _nsis_center:bounds:inEngine:forLayoutGuide:] + 1372
3 UIKitCore 0x18f908e9c -[UIView(Geometry) _applyISEngineLayoutValuesToBoundsOnly:] + 248
4 UIKitCore 0x18f9089e0 -[UIView(Geometry) _resizeWithOldSuperviewSize:] + 148
5 CoreFoundation 0x18d0cd6a4 __NSARRAY_IS_CALLING_OUT_TO_A_BLOCK__ + 24
6 CoreFoundation 0x18d0cd584 -[__NSArrayM enumerateObjectsWithOptions:usingBlock:] + 432
7 UIKitCore 0x18f8e62b0 -[UIView(Geometry) resizeSubviewsWithOldSize:] + 128
8 UIKitCore 0x18f977194 -[UIView(AdditionalLayoutSupport) _is_layout] + 124
9 UIKitCore 0x18f976c2c -[UIView _updateConstraintsAsNecessaryAndApplyLayoutFromEngine] + 800
10 UIKitCore 0x18f903944 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2728
11 QuartzCore 0x18ec15498 CA::Layer::layout_if_needed(CA::Transaction*) + 496
12 UIKitCore 0x18f940c10 -[UIView(Hierarchy) layoutBelowIfNeeded] + 312
Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 QuartzCore 0x18ec2cfe0 -[CALayer animationForKey:] + 176
1 UIKitCore 0x18fa5b258 UniqueAnimationKeyForLayer + 192
2 UIKitCore 0x18fa5ab7c __67-[_UIViewAdditiveAnimationAction runActionForKey:object:arguments:]_block_invoke_2 + 468
3 UIKitCore 0x18fa5ba5c -[_UIViewAdditiveAnimationAction runActionForKey:object:arguments:] + 1968
4 QuartzCore 0x18eb9e938 CA::Layer::set_bounds(CA::Rect const&, bool) + 428
5 QuartzCore 0x18eb9e760 -[CALayer setBounds:] + 132
6 UIKitCore 0x18f941770 -[UIView _backing_setBounds:] + 64
7 UIKitCore 0x18f940404 -[UIView(Geometry) setBounds:] + 340
8 UIKitCore 0x18f908f84 -[UIView(Geometry) _applyISEngineLayoutValuesToBoundsOnly:] + 480
9 UIKitCore 0x18f9089e0 -[UIView(Geometry) _resizeWithOldSuperviewSize:] + 148
10 CoreFoundation 0x18d0cd6a4 __NSARRAY_IS_CALLING_OUT_TO_A_BLOCK__ + 24
11 CoreFoundation 0x18d132488 -[__NSSingleObjectArrayI enumerateObjectsWithOptions:usingBlock:] + 92
12 UIKitCore 0x18f8e62b0 -[UIView(Geometry) resizeSubviewsWithOldSize:] + 128
13 UIKitCore 0x18f977194 -[UIView(AdditionalLayoutSupport) _is_layout] + 124
14 UIKitCore 0x18f976c2c -[UIView _updateConstraintsAsNecessaryAndApplyLayoutFromEngine] + 800
15 UIKitCore 0x18f916258 -[UIView(Hierarchy) layoutSubviews] + 204
16 UIKitCore 0x18f903814 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2424
17 QuartzCore 0x18ec15498 CA::Layer::layout_if_needed(CA::Transaction*) + 496
18 UIKitCore 0x18f940c10 -[UIView(Hierarchy) layoutBelowIfNeeded] + 312

So far, we only know of one iPad Air M1 where this is happening. But we don't know how many users experience this issue without reporting it.

Does anyone know what could cause Auto Layout or Core Animation to block in those calls? We have no clue so far...

Your application is probably not blocking, but rather something is invalidating layout as a response to layout, resulting in infinite layout.

UI freeze during layouting
 
 
Q