Skip to content

NSInternalInconsistencyException(Modifications to the layout engine must not be performed from a background thread after it has been accessed from the main thread.) #157091

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

Open
cj3479 opened this issue Oct 17, 2024 · 13 comments
Labels
a: production Issues experienced in live production apps c: crash Stack traces logged to the console c: fatal crash Crashes that terminate the process needs repro info Automated crash report whose cause isn't yet known P2 Important issues not at the top of the work list platform-ios iOS applications specifically team-ios Owned by iOS platform team triaged-ios Triaged by iOS platform team

Comments

@cj3479
Copy link

cj3479 commented Oct 17, 2024

Steps to reproduce

The crash is occasional. I refer to issue #135501 that illustrates the crash will
be solved after 3.16.x. But I meet the occasional crash on flutter 3.22.2

Expected results

NO crash

Actual results

Crash

Code sample

No code

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

crash info

Triggered by Thread:  64 io.flutter.2.raster 


Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Modifications to the layout engine must not be performed from a background thread after it has been accessed from the main thread.'

0  CoreFoundation                 0x00000001a0e5cf20 0x00000001a0dd9000 + 540448
1  libobjc.A.dylib                0x0000000198d072b8 objc_exception_throw + 60
2  CoreAutoLayout                 0x00000001c2108224 0x00000001c20f6000 + 74276
3  CoreAutoLayout                 0x00000001c20fb89c 0x00000001c20f6000 + 22684
4  CoreAutoLayout                 0x00000001c20f8208 0x00000001c20f6000 + 8712
5  CoreAutoLayout                 0x00000001c20f7f94 0x00000001c20f6000 + 8084
6  UIKitCore                      0x00000001a30b7264 0x00000001a305c000 + 373348
7  UIKitCore                      0x00000001a306c918 0x00000001a305c000 + 67864
8  QuartzCore                     0x00000001a24ca26c 0x00000001a244b000 + 520812
9  QuartzCore                     0x00000001a24c9df0 0x00000001a244b000 + 519664
10 QuartzCore                     0x00000001a2524fd8 0x00000001a244b000 + 892888
11 QuartzCore                     0x00000001a2499ee0 0x00000001a244b000 + 323296
12 QuartzCore                     0x00000001a24e3c34 0x00000001a244b000 + 625716
13 CoreFoundation                 0x00000001a0dfd658 0x00000001a0dd9000 + 149080
14 CoreFoundation                 0x00000001a0dfd414 0x00000001a0dd9000 + 148500
15 CoreFoundation                 0x00000001a0e2c54c 0x00000001a0dd9000 + 341324
16 CoreFoundation                 0x00000001a0e2bcd8 CFRunLoopRunSpecific + 608
17 Flutter                        0x0000000112585fa4 fml::MessageLoopDarwin::Run() (../../flutter/fml/platform/darwin/message_loop_darwin.mm:52)
18 Flutter                        0x0000000112585618 __thread_proxy<std::_LIBCPP_ABI_NAMESPACE::tuple<std::_LIBCPP_ABI_NAMESPACE::unique_ptr<std::_LIBCPP_ABI_NAMESPACE::__thread_struct, std::_LIBCPP_ABI_NAMESPACE::default_delete<std::_LIBCPP_ABI_NAMESPACE::__thread_struct> >, (lambda at ../../flutter/fml/thread.cc:79:7)> > (../../flutter/fml/message_loop_impl.cc:0)
19 libsystem_pthread.dylib        0x00000001fd8b606c _pthread_start + 136
20 libsystem_pthread.dylib        0x00000001fd8b10d8 thread_start + 8

Flutter Doctor output

Flutter 3.22.2 
Framework • revision 761747bfc5 (4 个月前) 
Engine • revision edd8546116
Tools • Dart 3.4.3 • DevTools 2.34.3
@danagbemava-nc danagbemava-nc added the in triage Presently being triaged by the triage team label Oct 17, 2024
@danagbemava-nc
Copy link
Member

Hi @cj3479, what device(s) do you see this on? What version of iOS are they running? Can you upgrade to stable 3.24.3 to see if you still experience this issue?

@danagbemava-nc danagbemava-nc added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Oct 17, 2024
@cj3479
Copy link
Author

cj3479 commented Oct 18, 2024

Hi @cj3479, what device(s) do you see this on? What version of iOS are they running? Can you upgrade to stable 3.24.3 to see if you still experience this issue?

The device info:
iPhone 11 (iPhone12,1) AND iPhone 15 Pro (iPhone16,1)
os version :17.7

I upgraded to stable 3.24.3 and I still experience this issue

@github-actions github-actions bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Oct 18, 2024
@danagbemava-nc
Copy link
Member

Hi @cj3479, are you able to reproduce this locally? And if so, can you share a minimal reproducible code sample (preferably without any third-party packages/plugins) so that we can properly investigate this issue?

@danagbemava-nc danagbemava-nc added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Oct 21, 2024
@cj3479
Copy link
Author

cj3479 commented Oct 25, 2024

Hi @cj3479, are you able to reproduce this locally? And if so, can you share a minimal reproducible code sample (preferably without any third-party packages/plugins) so that we can properly investigate this issue?

No,I can't reproduce it. The crash reporting platform reports the crash

@github-actions github-actions bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Oct 25, 2024
@danagbemava-nc
Copy link
Member

danagbemava-nc commented Oct 25, 2024

Labeling for further investigation as this is a production crash without any steps to reproduce.

Attempting to symbolize the stack trace didn't provide any more information than we already have

dart pub global run symbolizer
dart pub global run symbolizer:symbolize https://github.com/flutter/flutter/issues/157091\#issue-2594678468 "flutter#3.22.2 arm64 ios release force"
Building package executable... (1.2s)
Built symbolizer:symbolize.
INFO: 2024-10-25 06:27:45.001244: checking for llvm-{symbolizer,readobj,objdump} in $PATH
INFO: 2024-10-25 06:27:45.016294: checking for llvm-{symbolizer,readobj,objdump} in tools/android-ndk/toolchains/llvm/prebuilt/darwin-x86_64/bin/
INFO: 2024-10-25 06:27:45.033455: checking for llvm-{symbolizer,readobj,objdump} in /Users/deanli/Library/Android/sdk/ndk/27.1.12297006/toolchains/llvm/prebuilt/darwin-x86_64/bin/
INFO: 2024-10-25 06:27:46.686899: downloading /var/folders/59/gngn78896tbgdl1pjnkbk7dh0000gn/T/symbols-cache/edd8546116457bdf1c5bdfb13ecb9463d2bb5ed4-ios-release for EngineBuild(engineHash: edd8546116457bdf1c5bdfb13ecb9463d2bb5ed4, variant: EngineVariant(os: ios, arch: arm64, mode: release))
INFO: 2024-10-25 06:27:46.688139: gsutil cp gs://flutter_infra_release/flutter/edd8546116457bdf1c5bdfb13ecb9463d2bb5ed4/ios-release/Flutter.dSYM.zip /var/folders/59/gngn78896tbgdl1pjnkbk7dh0000gn/T/C22AOR/Flutter.dSYM.zip
INFO: 2024-10-25 06:28:19.789425: looking for load base of EngineBuild(engineHash: edd8546116457bdf1c5bdfb13ecb9463d2bb5ed4, variant: EngineVariant(os: ios, arch: arm64, mode: release)) based on [4602748836, 4602746392]
INFO: 2024-10-25 06:28:19.790319: downloading /var/folders/59/gngn78896tbgdl1pjnkbk7dh0000gn/T/symbols-cache/edd8546116457bdf1c5bdfb13ecb9463d2bb5ed4-ios-release-libflutter for EngineBuild(engineHash: edd8546116457bdf1c5bdfb13ecb9463d2bb5ed4, variant: EngineVariant(os: ios, arch: arm64, mode: release))
INFO: 2024-10-25 06:28:19.790847: gsutil cp gs://flutter_infra_release/flutter/edd8546116457bdf1c5bdfb13ecb9463d2bb5ed4/ios-release/artifacts.zip /var/folders/59/gngn78896tbgdl1pjnkbk7dh0000gn/T/8ww3fI/artifacts.zip
INFO: 2024-10-25 06:28:45.157765: Symbolizing using /var/folders/59/gngn78896tbgdl1pjnkbk7dh0000gn/T/symbols-cache/edd8546116457bdf1c5bdfb13ecb9463d2bb5ed4-ios-release/Flutter.dSYM/Contents/Resources/DWARF/Flutter

--------------------------------------------------------------------------------
symbolized using symbols for edd8546116457bdf1c5bdfb13ecb9463d2bb5ed4 ios-arm64-release

  #00 00000001a0e5cf20 CoreFoundation 0x00000001a0dd9000 + 540448
  #01 0000000198d072b8 libobjc.A.dylib objc_exception_throw + 60
  #02 00000001c2108224 CoreAutoLayout 0x00000001c20f6000 + 74276
  #03 00000001c20fb89c CoreAutoLayout 0x00000001c20f6000 + 22684
  #04 00000001c20f8208 CoreAutoLayout 0x00000001c20f6000 + 8712
  #05 00000001c20f7f94 CoreAutoLayout 0x00000001c20f6000 + 8084
  #06 00000001a30b7264 UIKitCore 0x00000001a305c000 + 373348
  #07 00000001a306c918 UIKitCore 0x00000001a305c000 + 67864
  #08 00000001a24ca26c QuartzCore 0x00000001a244b000 + 520812
  #09 00000001a24c9df0 QuartzCore 0x00000001a244b000 + 519664
  #10 00000001a2524fd8 QuartzCore 0x00000001a244b000 + 892888
  #11 00000001a2499ee0 QuartzCore 0x00000001a244b000 + 323296
  #12 00000001a24e3c34 QuartzCore 0x00000001a244b000 + 625716
  #13 00000001a0dfd658 CoreFoundation 0x00000001a0dd9000 + 149080
  #14 00000001a0dfd414 CoreFoundation 0x00000001a0dd9000 + 148500
  #15 00000001a0e2c54c CoreFoundation 0x00000001a0dd9000 + 341324
  #16 00000001a0e2bcd8 CoreFoundation CFRunLoopRunSpecific + 608
  #17 0000000112585fa4 Flutter (Missing) (../../flutter/fml/platform/darwin/message_loop_darwin.mm:5)
  #18 0000000112585618 Flutter (Missing) (../../flutter/fml/message_loop_impl.cc:)
  #19 00000001fd8b606c libsystem_pthread.dylib _pthread_start + 136
  #20 00000001fd8b10d8 libsystem_pthread.dylib thread_start + 8


--------------------------------------------------------------------------------

@danagbemava-nc danagbemava-nc added c: crash Stack traces logged to the console platform-ios iOS applications specifically a: production Issues experienced in live production apps c: fatal crash Crashes that terminate the process needs repro info Automated crash report whose cause isn't yet known team-ios Owned by iOS platform team and removed in triage Presently being triaged by the triage team labels Oct 25, 2024
@Wtoto

This comment was marked as off-topic.

@HatsuneMikuV
Copy link

[✓] Flutter (Channel stable, 3.24.5, on macOS 15.1.1 24B91 darwin-arm64, locale en-US)
    • Flutter version 3.24.5 on channel stable at /Users/anglemiku/Documents/flutter
    • Upstream repository git@github.com:flutter/flutter.git
    • Framework revision dec2ee5c1f (3 weeks ago), 2024-11-13 11:13:06 -0800
    • Engine revision a18df97ca5
    • Dart version 3.5.4
    • DevTools version 2.37.3
    • Pub download mirror https://pub.dev

We have the same problem and it has always existed. We are swift project + flutter plugins

@jmagman
Copy link
Member

jmagman commented Dec 20, 2024

Based on the similar issue in #66647 and the analysis there, it seems this may be related to transactions happening on the background thread. I suspect this will go away when the raster and main thread are merged by default flutter/engine#53826.
@jonahwilliams the thread merging isn't in stable, but is in master, right?

@jmagman jmagman added P2 Important issues not at the top of the work list triaged-ios Triaged by iOS platform team labels Dec 20, 2024
@jonahwilliams
Copy link
Member

correct

@jamalalayeq
Copy link

We are experiencing the same issue. This is the Sentry details.

@GHkmmm
Copy link

GHkmmm commented Apr 27, 2025

I'm having the same problem.
flutter version: 3.24.3

@XuweiQT
Copy link

XuweiQT commented May 16, 2025

我遇到了同样的问题,但是复现不了它,线上Crash还是在。看如下堆栈:

I encountered the same problem, but I can't reproduce it. Online Crash is still there. Look at the following stack:

NSInternalInconsistencyException
Modifications to the layout engine must not be performed from a background thread after it has been accessed from the main thread.

1 UIKitCore -[UIView(CALayerDelegate) layoutSublayersOfLayer:] 
2 UIKitCore -[_UILabelLayer layoutSublayers] 
3 CA::Layer::layout_if_needed(CA::Transaction*) 
4 CA::Layer::layout_and_display_if_needed(CA::Transaction*) 
5 QuartzCore CA::Context::commit_transaction(CA::Transaction*, double, double*) 
6 QuartzCore CA::Transaction::commit() 
7 QuartzCore CA::Transaction::flush_as_runloop_observer(bool) 
8 CoreFoundation ___CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ +
9 CoreFoundation ___CFRunLoopDoObservers 
10 CoreFoundation ___CFRunLoopRun +
11 CoreFoundation _CFRunLoopRunSpecific 
12 Flutter fml::MessageLoopDarwin::Run()(message_loop_darwin.mm:0)

做了如下处理未能避免这个crash

The following treatment failed to avoid this crash.

PostPrerollResult FlutterPlatformViewsController::PostPrerollAction(
    fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger) {

  if (!HasPlatformViewThisOrNextFrame()) {
    return PostPrerollResult::kSuccess;
  }

  if (!raster_thread_merger->IsMerged()) {
    CancelFrame();
    raster_thread_merger->MergeWithLease(kDefaultMergedLeaseDuration);
    return PostPrerollResult::kSkipAndRetryFrame;
  }

  BeginCATransaction();  // 这里引擎会“保证”在主线程执行。如果不,兜底[CATransaction begin]和[CATransaction commit]在主线程
  raster_thread_merger->ExtendLeaseTo(kDefaultMergedLeaseDuration);
  
  return PostPrerollResult::kSuccess;
}

void FlutterPlatformViewsController::BeginCATransaction() {
    if (catransaction_added_) return;

  if (![NSThread isMainThread]) {
       dispatch_async(dispatch_get_main_queue(), ^{ // !!!
        [CATransaction begin];
        catransaction_added_ = true;
      });
  } else {
      [CATransaction begin];
      catransaction_added_ = true;
  }
}

void FlutterPlatformViewsController::CommitCATransactionIfNeeded() {
  if (catransaction_added_) {
    if (![NSThread isMainThread]) {
        dispatch_async(dispatch_get_main_queue(), ^{ // !!!
              [CATransaction commit];
              catransaction_added_ = false;
            });
    } else {
        [CATransaction commit];
        catransaction_added_ = false;
    }
  }
}

实际上问题可能不在这里。从我在引擎侧打的日志以及线上堆栈来看,线程调度(切换/合并)是没有问题的。A是嵌入PlatformView的Flutter页面,从后台返回前台或从B页面返回A,会经历raster线程到主线程的快速切换;问题是否可能出现在这个切换的瞬间(切换到主线程前)恰好PlatformView有更新(这个更新是从dart UI线程同步过来的?至少不应该是主线程上的),... 这个更新‘
发生’在raster线程上了, 进而到 raster runloop即上面堆栈中的CA::Transaction::commit()

In fact, the problem may not be here. Judging from the logs and online stacks I typed on the engine side, there is no problem with thread scheduling (switching/merging). A is the Flutter page embedded in PlatformView. Returning to the foreground from the background or returning to A from page B will experience a rapid switch from the raster thread to the main thread; whether the problem may occur at the moment of this switch (before switching to the main thread) just happens to PlatformV Iew has an update (is this update synchronised from the dart UI thread? At least it should not be on the main thread),... This update'

It occurs on the raster thread, and then to the raster runloop, that is, CA::Transaction::commit() in the above stack.

@XuweiQT
Copy link

XuweiQT commented May 19, 2025

尝试修改kDefaultMergedLeaseDuration=10值,(如果给20、30或是60)灰度验证中

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a: production Issues experienced in live production apps c: crash Stack traces logged to the console c: fatal crash Crashes that terminate the process needs repro info Automated crash report whose cause isn't yet known P2 Important issues not at the top of the work list platform-ios iOS applications specifically team-ios Owned by iOS platform team triaged-ios Triaged by iOS platform team
Projects
None yet
Development

No branches or pull requests

9 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy