Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix accessing trait collection outside main queue #90

Merged

Conversation

luispadron
Copy link
Collaborator

@luispadron luispadron commented May 24, 2022

Summary

With iOS 15 devices I am getting a UI access outside of main thread crash:

Main Thread Checker: UI API called on a background thread: -[UIView AS_traitCollection]
PID: 70497, TID: 20879363, Thread name: (none), Queue name: com.apple.root.user-initiated-qos, QoS: 25
Backtrace:
4   FormPresenter-Unit-SnapshotTests    0x000000010d30f738 -[UIView(DynamicTypeSnapshotting) AS_traitCollection] + 52
5   UIKitCore                           0x0000000184a891ac +[UIKeyboardCache enabled] + 40
6   UIKitCore                           0x0000000184a894f4 -[UIKeyboardCache cachedImageForKey:fromLayout:] + 52
7   UIKitCore                           0x0000000184a4f900 __37-[UIKBKeyplaneView prepareForDisplay]_block_invoke + 224
8   libdispatch.dylib                   0x0000000180108db0 _dispatch_call_block_and_release + 24
9   libdispatch.dylib                   0x000000018010a5a4 _dispatch_client_callout + 16
10  libdispatch.dylib                   0x000000018011b4ac _dispatch_root_queue_drain + 736
11  libdispatch.dylib                   0x000000018011bd28 _dispatch_worker_thread2 + 160
12  libsystem_pthread.dylib             0x00000001c99ccb04 _pthread_wqthread + 224
13  libsystem_pthread.dylib             0x00000001c99cb904 start_wqthread + 8
2022-05-24 11:12:14.345711-0400 AppHost-FormPresenter-Unit-Tests[70497:20879363] [reports] Main Thread Checker: UI API called on a background thread: -[UIView AS_traitCollection]
PID: 70497, TID: 20879363, Thread name: (none), Queue name: com.apple.root.user-initiated-qos, QoS: 25
Backtrace:
4   FormPresenter-Unit-SnapshotTests    0x000000010d30f738 -[UIView(DynamicTypeSnapshotting) AS_traitCollection] + 52
5   UIKitCore                           0x0000000184a891ac +[UIKeyboardCache enabled] + 40
6   UIKitCore                           0x0000000184a894f4 -[UIKeyboardCache cachedImageForKey:fromLayout:] + 52
7   UIKitCore                           0x0000000184a4f900 __37-[UIKBKeyplaneView prepareForDisplay]_block_invoke + 224
8   libdispatch.dylib                   0x0000000180108db0 _dispatch_call_block_and_release + 24
9   libdispatch.dylib                   0x000000018010a5a4 _dispatch_client_callout + 16
10  libdispatch.dylib                   0x000000018011b4ac _dispatch_root_queue_drain + 736
11  libdispatch.dylib                   0x000000018011bd28 _dispatch_worker_thread2 + 160
12  libsystem_pthread.dylib             0x00000001c99ccb04 _pthread_wqthread + 224
13  libsystem_pthread.dylib             0x00000001c99cb904 start_wqthread + 8

This PR corrects this crash by ensuring we access AS_traitCollection via the main queue.

This is a workaround while we figure out why this is now crashing on iOS 15+ simulator devices. From our investigation we found that UIKit is calling traitCollection from a background queue and thus causing this to crash. Without swizzling this crash does not occur.

@luispadron luispadron closed this May 24, 2022
@luispadron luispadron reopened this Jun 6, 2022
@NickEntin
Copy link
Collaborator

NickEntin commented Jun 8, 2022

Two thoughts:

  1. Should we limit this behavior to iOS 15+, since it doesn't seem to affect older versions?
  2. Let's add a comment in the code explaining why we're changing threading behavior here.

@luispadron luispadron force-pushed the fix/trait-collection-main-queue branch from 0d59bfe to 4885665 Compare June 9, 2022 17:31
@luispadron
Copy link
Collaborator Author

luispadron commented Jun 9, 2022

Two thoughts:

  1. Should we limit this behavior to iOS 15+, since it doesn't seem to affect older versions?
  2. Let's add a comment in the code explaining why we're changing threading behavior here.

@NickEntin

Good call on #1, added and availability check!
For #2 I added a comment!

@luispadron luispadron force-pushed the fix/trait-collection-main-queue branch from 4885665 to 71d7170 Compare June 15, 2022 18:26
@NickEntin NickEntin merged commit 71d7288 into cashapp:master Jun 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants