Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

[Impeller] its not safe to presentWithTransaction from a background thread. #45182

Merged
merged 1 commit into from
Aug 29, 2023

Conversation

jonahwilliams
Copy link
Member

Sad Trombone. Because Flutter runs from a background thread, if we touch a CATransaction while there is a main thread transaction occuring then we'll trigger a framework error.

Fixes flutter/flutter#133147

@jonahwilliams jonahwilliams marked this pull request as ready for review August 28, 2023 19:38
@jonahwilliams jonahwilliams requested a review from luckysmg August 28, 2023 19:38
@jonahwilliams
Copy link
Member Author

There are a number of alternative solutions to this, all of which are fairly risky, so instead we should start with the safe option.

// presented. If there is a non-Flutter UIView active, such as in add2app or a
// presentViewController page transition, then this will cause CoreAnimation assertion errors and
// exit the app.
layer.presentsWithTransaction = [[NSThread currentThread] isMainThread];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wont' this re-introduce the bug we cherry picked a fix for into 3.10?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way we could add some tests in this? Explicitly start a transaction on the main thread, then try to commit one on the background?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add tests in the framework repo. The assertion error cannot be triggered on Simulators IIRC

@jonahwilliams
Copy link
Member Author

The actual error is:

[LayoutConstraints] Unsupported layout off the main thread for UITextEffectsWindow with no associated or ancestor view controller
This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.

If you trigger animations on the main thread like pushing a new view controller with an animation (what the video player does). when we set presentsWithTransaction, that implicitly creates a CATransaction for the background thread (though we can see the same effect by manually creating the transaction). When we [present drawable], that marks the UIViews up tot the root dirty, which triggers a layout, and then triggers the layout off main thread error.

@luckysmg
Copy link
Contributor

luckysmg commented Aug 29, 2023

I think this LGTM.

Also, found some old context:
flutter/flutter#66647
flutter/flutter#66647 (comment)

@jonahwilliams
Copy link
Member Author

Going to land this now while we work on figuring out a better fix going forward.

@jonahwilliams jonahwilliams added the autosubmit Merge PR when tree becomes green via auto submit App label Aug 29, 2023
@auto-submit auto-submit bot merged commit b3b1f52 into flutter:main Aug 29, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Aug 29, 2023
auto-submit bot pushed a commit to flutter/flutter that referenced this pull request Aug 29, 2023
…133580)

flutter/engine@d1e6eb0...73cc3fb

2023-08-29 skia-flutter-autoroll@skia.org Roll Skia from b7e826d5324f to 15de7f9c3b3b (3 revisions) (flutter/engine#45231)
2023-08-29 jonahwilliams@google.com [Impeller] its not safe to presentWithTransaction from a background thread. (flutter/engine#45182)
2023-08-29 skia-flutter-autoroll@skia.org Roll Skia from b0c1b2868129 to b7e826d5324f (4 revisions) (flutter/engine#45229)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-engine-flutter-autoroll
Please CC aaclarke@google.com,rmistry@google.com,zra@google.com on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://bugs.chromium.org/p/skia/issues/entry?template=Autoroller+Bug

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
gaaclarke pushed a commit to gaaclarke/engine that referenced this pull request Aug 30, 2023
…hread. (flutter#45182)

Sad Trombone. Because Flutter runs from a background thread, if we touch a CATransaction while there is a main thread transaction occuring then we'll trigger a framework error.

Fixes flutter/flutter#133147
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
autosubmit Merge PR when tree becomes green via auto submit App platform-ios
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Image-Picker: Pick Video from gallery causes crash on iOS and Flutter 3.13.0
3 participants