Can not find origin of "This application is modifying the autolayout engine from a background thread"

0

My app crash only the first time i log into it. It used the GoogleMaps (3.7.0) pod, and I suspect the error comes from that library:

 Stack:(
    0   Foundation                          0x000000018c1b8550 80C31587-F538-3437-966F-300F6632EB95 + 2331984
    1   Foundation                          0x000000018bfb18c4 80C31587-F538-3437-966F-300F6632EB95 + 207044
    2   Foundation                          0x000000018bfb17d8 80C31587-F538-3437-966F-300F6632EB95 + 206808
    3   Foundation                          0x000000018bfb1448 80C31587-F538-3437-966F-300F6632EB95 + 205896
    4   UIKitCore                           0x000000019025a824 AA897CA9-8D15-3DD7-BB4F-8D90F9A28571 + 15611940
    5   UIKitCore                           0x000000019026db1c AA897CA9-8D15-3DD7-BB4F-8D90F9A28571 + 15690524
    6   libobjc.A.dylib                     0x000000018b9e0a4c A486C2B3-41B0-3F23-AF40-C5579F5BE97E + 6732
    7   QuartzCore                          0x0000000192883a9c A3363F67-6783-3979-8FA9-5563ABD4737D + 1403548
    8   QuartzCore                          0x0000000192883eec A3363F67-6783-3979-8FA9-5563ABD4737D + 1404652
    9   QuartzCore                          0x0000000192896614 A3363F67-6783-3979-8FA9-5563ABD4737D + 1480212
    10  QuartzCore                          0x00000001927db1c4 A3363F67-6783-3979-8FA9-5563ABD4737D + 713156
    11  QuartzCore                          0x0000000192805fd0 A3363F67-6783-3979-8FA9-5563ABD4737D + 888784
    12  QuartzCore                          0x0000000192806fb8 A3363F67-6783-3979-8FA9-5563ABD4737D + 892856
    13  libsystem_pthread.dylib             0x000000018b9dd104 5F848A4C-4907-3FC9-A76D-378FE8F2E4FC + 37124
    14  libsystem_pthread.dylib             0x000000018b9d58a0 5F848A4C-4907-3FC9-A76D-378FE8F2E4FC + 6304
    15  libsystem_pthread.dylib             0x000000018b9d7a20 5F848A4C-4907-3FC9-A76D-378FE8F2E4FC + 14880
    16  libsystem_pthread.dylib             0x000000018b9d7760 _pthread_wqthread + 424
    17  libsystem_pthread.dylib             0x000000018b9dd9e8 start_wqthread + 8
)

This is the traces log that is shown XCode.

enter image description here

Editor screenshot:

enter image description here

This is how I received the callbacks from my ViewModel. I added DispatchQueue.main in all the outputs to try to avoid this exception, but is still popping up....

disposeBag.insert(
            viewModel.outputs.onShowLoading
                .bind { show in
                    DispatchQueue.main.async { 
                        self.showLoading(show: show)
                    }
                },

            viewModel.outputs.onLoadDone
                .bind { mapInfo in
                    DispatchQueue.main.async {
                        self.loadMap(mapInfo: mapInfo)
                    }
                },

            viewModel.outputs.onLoadError
                .bind { error in
                    DispatchQueue.main.async {
                        print("ON LOAD ASSETS ERROR: \(error.localizedDescription)")
                    }
                },

            viewModel.outputs.onMultiClusterTap
                .bind { paths in
                    DispatchQueue.main.async {
                        self.centerMap(paths: paths)
                    }
                },

            viewModel.outputs.onLoadAssetsInfoDone
                .bind { assets in
                    DispatchQueue.main.async {
                        self.showBottomSheet(assets: assets)
                    }
                },

            viewModel.outputs.onDeviceChanged
                .bind {
                    DispatchQueue.main.async {
                        self!.showDeviceChangedError()
                    }
                }
        )

Edit (02/05/2020): I try to find the error using NBUIKitMainThreadGuard

But still I couldn't find the source of thes error. In addition, I found out that the error also occurs after a log out - login. So is not just the first time I install the app.

Edit (07/05/2020): Maybe as Werner Altewischer pointed out, there is a graphic library that is messing this out. This is my podfile, maybe helps solve this:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '11.0'
use_frameworks!

target 'WatchmanDoor' do

    pod 'DropDown', '2.3.13'
    pod 'GoogleMaps', '3.7.0'
    pod 'GooglePlaces'
    pod 'DKImagePickerController', '4.2.1'
    pod 'Alamofire', '4.9.1'
    pod 'iOSDFULibrary', '4.6.1'
    pod 'RxSwift', '~> 5.0.1'
    pod 'RxCocoa', '~> 5.0.1'
    pod 'Swinject', '~> 2.7.1'
    pod 'SwinjectAutoregistration', '~> 2.7.0'
    pod 'SwinjectStoryboard', '~> 2.2.0'
    pod 'FittedSheets', :git => 'https://github.com/WatchmanDoor/FittedSheets.git', :commit => '9824dd171499967226a4dd160aedc7d4d9db3a11'
    pod 'SDWebImage', '~> 5.5.0'
    pod 'NavigationDrawer', '1.0.2'

end

Could it be that the FittedSheets pod is the one that provokes the error? I fork the repository to update it to support Swift 5, but maybe is the one causing the exception.

UPDATE: Still facing the same error ... can not find anything else -_- Any idea where to start?

Full Crash Report:

Incident Identifier: 34A039F6-9E4A-4E5C-B9B4-AB531A8E8D93
CrashReporter Key:   5f326dac824b350bd2afaacbdb938d680de621bc
Hardware Model:      iPhone8,4
Process:             WatchmanDoor [476]
Path:                /private/var/containers/Bundle/Application/717E7E5E-4E42-4950-B8C8-FC2042C09EDC/WatchmanDoor.app/WatchmanDoor
Identifier:          com.stmseguridad.WatchmanDoor
Version:             1 (2.0.9)
Code Type:           ARM-64 (Native)
Role:                Foreground
Parent Process:      launchd [1]
Coalition:           com.stmseguridad.WatchmanDoor [625]


Date/Time:           2020-04-16 14:21:00.9449 +0200
Launch Time:         2020-04-16 14:18:12.7530 +0200
OS Version:          iPhone OS 13.4 (17E255)
Release Type:        User
Baseband Version:    9.52.01
Report Version:      104

Exception Type:  EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000001, 0x00000001a9d2d6e0
Termination Signal: Trace/BPT trap: 5
Termination Reason: Namespace SIGNAL, Code 0x5
Terminating Process: exc handler [476]
Triggered by Thread:  3

Last Exception Backtrace:
0   CoreFoundation                  0x1aa0cc5f0 __exceptionPreprocess + 224
1   libobjc.A.dylib                 0x1a9deebcc objc_exception_throw + 55
2   Foundation                      0x1aa5a3dac _AssertAutolayoutOnAllowedThreadsOnly + 419
3   Foundation                      0x1aa3b0c2c -[NSISEngine _optimizeWithoutRebuilding] + 67
4   Foundation                      0x1aa3b0b48 -[NSISEngine optimize] + 111
5   Foundation                      0x1aa3b07bc -[NSISEngine performPendingChangeNotifications] + 111
6   UIKitCore                       0x1ae6132d0 -[UIView+ 15499984 (Hierarchy) layoutSubviews] + 307
7   UIKitCore                       0x1ae625c7c -[UIView+ 15576188 (CALayerDelegate) layoutSublayersOfLayer:] + 2143
8   QuartzCore                      0x1b0bbf4ac -[CALayer layoutSublayers] + 283
9   QuartzCore                      0x1b0bc5604 CA::Layer::layout_if_needed+ 1406468 (CA::Transaction*) + 467
10  QuartzCore                      0x1b0bd0148 CA::Layer::layout_and_display_if_needed+ 1450312 (CA::Transaction*) + 139
11  QuartzCore                      0x1b0b18e34 CA::Context::commit_transaction+ 699956 (CA::Transaction*, double) + 295
12  QuartzCore                      0x1b0b427c4 CA::Transaction::commit+ 870340 () + 675
13  QuartzCore                      0x1b0b43764 CA::Transaction::release_thread+ 874340 (void*) + 227
14  libsystem_pthread.dylib         0x1a9de5f8c _pthread_tsd_cleanup + 579
15  libsystem_pthread.dylib         0x1a9de2df4 _pthread_exit + 79
16  libsystem_pthread.dylib         0x1a9de3e64 _pthread_wqthread_exit + 95
17  libsystem_pthread.dylib         0x1a9de3c04 _pthread_wqthread + 415
18  libsystem_pthread.dylib         0x1a9de6740 start_wqthread + 7


Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0:
0   libobjc.A.dylib                 0x00000001a9deab4c objc_msgSend + 44
1   AppleMetalGLRenderer            0x00000001c4b60624 GLDContextRec::setRenderState+ 54820 (unsigned int) + 84
2   AppleMetalGLRenderer            0x00000001c4b67084 gldRenderVertexArray+ 82052 (GLDContextRec*, unsigned int, unsigned int, int, int, unsigned int, void const*, int, void const*) + 1108
3   GLEngine                        0x00000001c4ae3cd0 glDrawElements_ACC_ES2Exec + 416
4   WatchmanDoor                    0x0000000103a12fcc 0x102c50000 + 14430156
5   WatchmanDoor                    0x0000000103a17be0 0x102c50000 + 14449632
6   WatchmanDoor                    0x0000000103ad4bb0 0x102c50000 + 15223728
7   WatchmanDoor                    0x0000000103a1088c 0x102c50000 + 14420108
8   WatchmanDoor                    0x0000000103a9ece8 0x102c50000 + 15002856
9   WatchmanDoor                    0x0000000103a82788 0x102c50000 + 14886792
10  WatchmanDoor                    0x0000000103acfc5c 0x102c50000 + 15203420
11  WatchmanDoor                    0x0000000103a80da8 0x102c50000 + 14880168
12  WatchmanDoor                    0x0000000103a7fc74 0x102c50000 + 14875764
13  QuartzCore                      0x00000001b0a7f368 CA::Display::DisplayLink::dispatch_items+ 70504 (unsigned long long, unsigned long long, unsigned long long) + 516
14  IOKit                           0x00000001ab010628 IODispatchCalloutFromCFMessage + 480
15  CoreFoundation                  0x00000001aa021bc0 __CFMachPortPerform + 172
16  CoreFoundation                  0x00000001aa04b200 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 56
17  CoreFoundation                  0x00000001aa04a90c __CFRunLoopDoSource1 + 444
18  CoreFoundation                  0x00000001aa0456c0 __CFRunLoopRun + 1888
19  CoreFoundation                  0x00000001aa044c34 CFRunLoopRunSpecific + 424
20  GraphicsServices                0x00000001b418e38c GSEventRunModal + 160
21  UIKitCore                       0x00000001ae17722c UIApplicationMain + 1932
22  WatchmanDoor                    0x000000010360fa9c 0x102c50000 + 10222236
23  libdyld.dylib                   0x00000001a9ecc800 start + 4

Thread 1 name:  com.apple.uikit.eventfetch-thread
Thread 1:
0   libsystem_kernel.dylib          0x00000001a9ea0198 mach_msg_trap + 8
1   libsystem_kernel.dylib          0x00000001a9e9f60c mach_msg + 72
2   CoreFoundation                  0x00000001aa04a3b4 __CFRunLoopServiceMachPort + 148
3   CoreFoundation                  0x00000001aa0453e8 __CFRunLoopRun + 1160
4   CoreFoundation                  0x00000001aa044c34 CFRunLoopRunSpecific + 424
5   Foundation                      0x00000001aa387bcc -[NSRunLoop+ 31692 (NSRunLoop) runMode:beforeDate:] + 228
6   Foundation                      0x00000001aa387aac -[NSRunLoop+ 31404 (NSRunLoop) runUntilDate:] + 88
7   UIKitCore                       0x00000001ae219160 -[UIEventFetcher threadMain] + 152
8   Foundation                      0x00000001aa4b69d0 __NSThread__start__ + 848
9   libsystem_pthread.dylib         0x00000001a9de2d98 _pthread_start + 156
10  libsystem_pthread.dylib         0x00000001a9de674c thread_start + 8

Thread 2:
0   libsystem_pthread.dylib         0x00000001a9de6738 start_wqthread + 0

Thread 3 Crashed:
0   libsystem_c.dylib               0x00000001a9d2d6e0 __abort + 144
1   libsystem_c.dylib               0x00000001a9d2d6e0 __abort + 144
2   libsystem_c.dylib               0x00000001a9d2d650 __abort + 0
3   libc++abi.dylib                 0x00000001a9e95cc0 __cxxabiv1::__aligned_malloc_with_fallback+ 76992 (unsigned long) + 0
4   libc++abi.dylib                 0x00000001a9e87e10 demangling_unexpected_handler+ 19984 () + 0
5   libobjc.A.dylib                 0x00000001a9deee80 _objc_terminate+ 24192 () + 124
6   libc++abi.dylib                 0x00000001a9e9514c std::__terminate(void (*)+ 74060 ()) + 16
7   libc++abi.dylib                 0x00000001a9e97bd8 __cxa_get_exception_ptr + 0
8   libc++abi.dylib                 0x00000001a9e97b98 __cxxabiv1::exception_cleanup_func+ 84888 (_Unwind_Reason_Code, _Unwind_Exception*) + 0
9   libobjc.A.dylib                 0x00000001a9deecf8 _objc_exception_destructor+ 23800 (void*) + 0
10  Foundation                      0x00000001aa5a3dac -[NSISEngine tryToOptimizeReturningMutuallyExclusiveConstraints] + 0
11  Foundation                      0x00000001aa3b0c2c -[NSISEngine _optimizeWithoutRebuilding] + 68
12  Foundation                      0x00000001aa3b0b48 -[NSISEngine optimize] + 112
13  Foundation                      0x00000001aa3b07bc -[NSISEngine performPendingChangeNotifications] + 112
14  UIKitCore                       0x00000001ae6132d0 -[UIView+ 15499984 (Hierarchy) layoutSubviews] + 308
15  UIKitCore                       0x00000001ae625c7c -[UIView+ 15576188 (CALayerDelegate) layoutSublayersOfLayer:] + 2144
16  QuartzCore                      0x00000001b0bbf4ac -[CALayer layoutSublayers] + 284
17  QuartzCore                      0x00000001b0bc5604 CA::Layer::layout_if_needed+ 1406468 (CA::Transaction*) + 468
18  QuartzCore                      0x00000001b0bd0148 CA::Layer::layout_and_display_if_needed+ 1450312 (CA::Transaction*) + 140
19  QuartzCore                      0x00000001b0b18e34 CA::Context::commit_transaction+ 699956 (CA::Transaction*, double) + 296
20  QuartzCore                      0x00000001b0b427c4 CA::Transaction::commit+ 870340 () + 676
21  QuartzCore                      0x00000001b0b43764 CA::Transaction::release_thread+ 874340 (void*) + 228
22  libsystem_pthread.dylib         0x00000001a9de5f8c _pthread_tsd_cleanup + 580
23  libsystem_pthread.dylib         0x00000001a9de2df4 _pthread_exit + 80
24  libsystem_pthread.dylib         0x00000001a9de3e64 _pthread_wqthread_legacy_worker_wrap + 0
25  libsystem_pthread.dylib         0x00000001a9de3c04 _pthread_wqthread + 416
26  libsystem_pthread.dylib         0x00000001a9de6740 start_wqthread + 8

Thread 4 name:  com.apple.NSURLConnectionLoader
Thread 4:
0   libsystem_kernel.dylib          0x00000001a9ea0198 mach_msg_trap + 8
1   libsystem_kernel.dylib          0x00000001a9e9f60c mach_msg + 72
2   CoreFoundation                  0x00000001aa04a3b4 __CFRunLoopServiceMachPort + 148
3   CoreFoundation                  0x00000001aa0453e8 __CFRunLoopRun + 1160
4   CoreFoundation                  0x00000001aa044c34 CFRunLoopRunSpecific + 424
5   CFNetwork                       0x00000001ad309c44 0x1ad308000 + 7236
6   Foundation                      0x00000001aa4b69d0 __NSThread__start__ + 848
7   libsystem_pthread.dylib         0x00000001a9de2d98 _pthread_start + 156
8   libsystem_pthread.dylib         0x00000001a9de674c thread_start + 8

Thread 5 name:  com.google.Maps.LabelingBehavior
Thread 5:
0   libsystem_kernel.dylib          0x00000001a9ea0198 mach_msg_trap + 8
1   libsystem_kernel.dylib          0x00000001a9e9f60c mach_msg + 72
2   CoreFoundation                  0x00000001aa04a3b4 __CFRunLoopServiceMachPort + 148
3   CoreFoundation                  0x00000001aa0453e8 __CFRunLoopRun + 1160
4   CoreFoundation                  0x00000001aa044c34 CFRunLoopRunSpecific + 424
5   Foundation                      0x00000001aa387bcc -[NSRunLoop+ 31692 (NSRunLoop) runMode:beforeDate:] + 228
6   WatchmanDoor                    0x0000000103b4e330 0x102c50000 + 15721264
7   Foundation                      0x00000001aa4b69d0 __NSThread__start__ + 848
8   libsystem_pthread.dylib         0x00000001a9de2d98 _pthread_start + 156
9   libsystem_pthread.dylib         0x00000001a9de674c thread_start + 8

Thread 6:
0   libsystem_pthread.dylib         0x00000001a9de6738 start_wqthread + 0

Thread 7:
0   libsystem_pthread.dylib         0x00000001a9de6738 start_wqthread + 0

Thread 8:
0   libsystem_pthread.dylib         0x00000001a9de6738 start_wqthread + 0

Thread 3 crashed with ARM Thread State (64-bit):
    x0: 0x0000000000000000   x1: 0x0000000000000000   x2: 0x0000000000000000   x3: 0x0000000000000001
    x4: 0x0000000000000000   x5: 0x0000000000989680   x6: 0x000000000000006e   x7: 0x0000000000000001
    x8: 0x0000000000000000   x9: 0x0000000000000002  x10: 0x000000002daafd89  x11: 0x000000000000000b
   x12: 0x00000001e48ea080  x13: 0x0000000000000000  x14: 0x0000000000000010  x15: 0x0000000000000000
   x16: 0x0000000000000030  x17: 0x00000002d7ffffff  x18: 0x0000000000000000  x19: 0x000000016d57ec48
   x20: 0x00000001f3404c80  x21: 0x000000016d57ed00  x22: 0x0000000169ba05b0  x23: 0x00000001e4b8c000
   x24: 0x0000000000000001  x25: 0x0000000000000068  x26: 0x000000000000009c  x27: 0x0000000000000068
   x28: 0x000000002b310019   fp: 0x000000016d57ec60   lr: 0x00000001a9d2d6e0
    sp: 0x000000016d57ec30   pc: 0x00000001a9d2d6e0 cpsr: 0x80000000
   esr: 0xf2000001  Address size fault

IMPORTANT UPDATE: I found out that, when the app crashes, my stack view is full of UITRANSITIONVIEW. Don't know if this is related ...

XCode Stack View Screenshoot

SOLUTION [13/05/2020] So I think I found where the error came from. See the answer below.

ios
swift
xcode
google-maps
ios13
asked on Stack Overflow Jan 23, 2020 by Carlos Cáceres González • edited May 15, 2020 by Carlos Cáceres González

2 Answers

1

Obviously the background thread in question (Thread 17) is accessing some UIKit method directly which in general is not allowed (can only be done from the main thread).

Did you dispatch some method on a background thread using GCD's dispatch async?

answered on Stack Overflow Jan 23, 2020 by Werner Altewischer
0

So I think I found where the error came from. When I received the response from my API with the asset list, I create a MapInfoModel like this:

private func generateMapInfo(assets: [APIAssetModel]) -> MapInfoModel {
    var markers = [ClusterMarkerItem]()
    var locations = [CLLocationCoordinate2D]()
    assets.forEach({ asset in
        if nil != asset.lat && nil != asset.lng {
            if asset.lat! > -85 && asset.lat! < 85 {
                let latlng = CLLocationCoordinate2D(latitude: asset.lat!, longitude: asset.lng!)
                for index in 0..<(asset.productsCount ?? 0) {
                    let marker = GMSMarker(position: latlng)
                    marker.title = "\(asset.id)_\(index)"
                    markers.append(ClusterMarkerItem(position: latlng,
                                                     marker: marker,
                                                     asset: asset,
                                                     prod_position: index))
                }
                locations.append(latlng)
            }
        }
    })
    return MapInfoModel(assetList: assets, clusterList: markers, locationList: locations)
}

This method was called from a background thread and, as you can see, I was instantiating a GMSMarker. This doesn't rise any error/warning and the map is loaded and showing the clusters well. But, here is the thing, I suspect that after loading the cluster, Google Maps SDK does something with the GMSMarkers loaded in it, and I suspect that, that action is been called from the thread where GMSMarker was created, so, if it was created from a background thread, XCode will show the error:

This application is modifying the autolayout engine from a background thread

I'm just guessing but, I am waiting for Google to confirm this. So the solution was to instantiate GMSMarker from Main thread (in this case I use DispatchQueue.main.async{}).

UPDATE Google just email me and confirm it. This is what they said:

Our specialists have confirmed that all Marker creation and updates should be happening on the main thread.

This is because all drawing and rendering in iOS has to happen on the main thread, and updates done to the markers translates into that directly.

We will be working on including this information in our official documentation for future references.


User contributions licensed under CC BY-SA 3.0