Revert "Reland "iOS: Migrate FlutterEngine to ARC" (#55937)" (#55954)

There are still a couple closures where on engine shutdown, the last
live reference to FlutterEngine may be on a thread other than the
platform thread.

Specifically, the profiling data capture callback can result in the last
live reference to a FlutterEngine being on a profiling thread, resulting
in an assertion failure in the destructor of the PlatformViewsController
held by FlutterEngine, because PlatformViewsController holds a
WeakPtrFactory whose destructor asserts that it be on the platform
thread.

See:

https://github.com/flutter/engine/blob/ad9e4fef451a73427285826455db5fbdde502bd7/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm#L511-L515

Backtrace of such a crash:
```
=================================================================
Main Thread Checker: UI API called on a background thread: -[UIView removeFromSuperview]
PID: 46919, TID: 333147, Thread name: FlutterEngine.166.profiler, Queue name: com.apple.root.default-qos.overcommit, QoS: 0
Backtrace:
4   libios_test_flutter.dylib           0x000000014b658aed -[FlutterTextInputPlugin dealloc] + 27
5   libobjc.A.dylib                     0x00007ff800055228 _ZN11objc_object17sidetable_releaseEbb + 204
6   libios_test_flutter.dylib           0x000000014b63df5a -[FlutterEngine .cxx_destruct] + 135
7   libobjc.A.dylib                     0x00007ff800035766 _ZL27object_cxxDestructFromClassP11objc_objectP10objc_class + 83
8   libobjc.A.dylib                     0x00007ff80004ddfc objc_destructInstance + 61
9   CoreFoundation                      0x00007ff8004a286b -[NSObject(NSObject) __dealloc_zombie] + 159
10  libios_test_flutter.dylib           0x000000014b635fea -[FlutterEngine dealloc] + 334
11  libobjc.A.dylib                     0x00007ff800055228 _ZN11objc_object17sidetable_releaseEbb + 204
12  libios_test_flutter.dylib           0x000000014b63eda9 _ZNSt3_fl10__function6__funcIZ30-[FlutterEngine startProfiler]E3$_0NS_9allocatorIS2_EEFN7flutter13ProfileSampleEvEEclEv + 69
13  libios_test_flutter.dylib           0x000000014bcd8eaa _ZNSt3_fl10__function6__funcIZNK7flutter16SamplingProfiler16SampleRepeatedlyEN3fml9TimeDeltaEE3$_0NS_9allocatorIS6_EEFvvEEclEv + 40
14  libios_test_flutter.dylib           0x000000014b868786 _ZN3fml15MessageLoopImpl10FlushTasksENS_9FlushTypeE + 156
15  libios_test_flutter.dylib           0x000000014b86ecca _ZN3fml17MessageLoopDarwin11OnTimerFireEP16__CFRunLoopTimerPS0_ + 26
16  CoreFoundation                      0x00007ff8003ea4a5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
17  CoreFoundation                      0x00007ff8003ea032 __CFRunLoopDoTimer + 801
18  CoreFoundation                      0x00007ff8003e97b6 __CFRunLoopDoTimers + 243
19  CoreFoundation                      0x00007ff8003e4028 __CFRunLoopRun + 2108
20  CoreFoundation                      0x00007ff8003e3409 CFRunLoopRunSpecific + 557
21  libios_test_flutter.dylib           0x000000014b86ee07 _ZN3fml17MessageLoopDarwin3RunEv + 65
22  libios_test_flutter.dylib           0x000000014b8686a4 _ZN3fml15MessageLoopImpl5DoRunEv + 22
23  libios_test_flutter.dylib           0x000000014b86dce5 _ZNSt3_fl10__function6__funcIZN3fml6ThreadC1ERKNS_8functionIFvRKNS3_12ThreadConfigEEEES7_E3$_0NS_9allocatorISC_EEFvvEEclEv + 135
24  libios_test_flutter.dylib           0x000000014b86da87 _ZZN3fml12ThreadHandleC1EONSt3_fl8functionIFvvEEEEN3$_08__invokeEPv + 27
25  libsystem_pthread.dylib             0x000000010d6a818b _pthread_start + 99
26  libsystem_pthread.dylib             0x000000010d6a3ae3 thread_start + 15
2024-10-18 09:31:27.549111-0700 IosUnitTests[46919:333147] [reports] Main Thread Checker: UI API called on a background thread: -[UIView removeFromSuperview]
PID: 46919, TID: 333147, Thread name: FlutterEngine.166.profiler, Queue name: com.apple.root.default-qos.overcommit, QoS: 0
Backtrace:
4   libios_test_flutter.dylib           0x000000014b658aed -[FlutterTextInputPlugin dealloc] + 27
5   libobjc.A.dylib                     0x00007ff800055228 _ZN11objc_object17sidetable_releaseEbb + 204
6   libios_test_flutter.dylib           0x000000014b63df5a -[FlutterEngine .cxx_destruct] + 135
7   libobjc.A.dylib                     0x00007ff800035766 _ZL27object_cxxDestructFromClassP11objc_objectP10objc_class + 83
8   libobjc.A.dylib                     0x00007ff80004ddfc objc_destructInstance + 61
9   CoreFoundation                      0x00007ff8004a286b -[NSObject(NSObject) __dealloc_zombie] + 159
10  libios_test_flutter.dylib           0x000000014b635fea -[FlutterEngine dealloc] + 334
11  libobjc.A.dylib                     0x00007ff800055228 _ZN11objc_object17sidetable_releaseEbb + 204
12  libios_test_flutter.dylib           0x000000014b63eda9 _ZNSt3_fl10__function6__funcIZ30-[FlutterEngine startProfiler]E3$_0NS_9allocatorIS2_EEFN7flutter13ProfileSampleEvEEclEv + 69
13  libios_test_flutter.dylib           0x000000014bcd8eaa _ZNSt3_fl10__function6__funcIZNK7flutter16SamplingProfiler16SampleRepeatedlyEN3fml9TimeDeltaEE3$_0NS_9allocatorIS6_EEFvvEEclEv + 40
14  libios_test_flutter.dylib           0x000000014b868786 _ZN3fml15MessageLoopImpl10FlushTasksENS_9FlushTypeE + 156
15  libios_test_flutter.dylib           0x000000014b86ecca _ZN3fml17MessageLoopDarwin11OnTimerFireEP16__CFRunLoopTimerPS0_ + 26
16  CoreFoundation                      0x00007ff8003ea4a5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
17  CoreFoundation                      0x00007ff8003ea032 __CFRunLoopDoTimer + 801
18  CoreFoundation                      0x00007ff8003e97b6 __CFRunLoopDoTimers + 243
19  CoreFoundation                      0x00007ff8003e4028 __CFRunLoopRun + 2108
20  CoreFoundation                      0x00007ff8003e3409 CFRunLoopRunSpecific + 557
21  libios_test_flutter.dylib           0x000000014b86ee07 _ZN3fml17MessageLoopDarwin3RunEv + 65
22  libios_test_flutter.dylib           0x000000014b8686a4 _ZN3fml15MessageLoopImpl5DoRunEv + 22
23  libios_test_flutter.dylib           0x000000014b86dce5 _ZNSt3_fl10__function6__funcIZN3fml6ThreadC1ERKNS_8functionIFvRKNS3_12ThreadConfigEEEES7_E3$_0NS_9allocatorISC_EEFvvEEclEv + 135
24  libios_test_flutter.dylib           0x000000014b86da87 _ZZN3fml12ThreadHandleC1EONSt3_fl8functionIFvvEEEEN3$_08__invokeEPv + 27
25  libsystem_pthread.dylib             0x000000010d6a818b _pthread_start + 99
26  libsystem_pthread.dylib             0x000000010d6a3ae3 thread_start + 15
IosUnitTests(47009,0x10ec76240) malloc: enabling scribbling to detect mods to free blocks
IosUnitTests(47009) MallocStackLogging: could not tag MSL-related memory as no_footprint, so those pages will be included in process footprint - (null)
IosUnitTests(47009) MallocStackLogging: stack logs being written to /private/tmp/stack-logs.47009.1057b6000.IosUnitTests.0o4HgN.index
IosUnitTests(47009) MallocStackLogging: recording malloc and VM allocation stacks to disk using standard recorder
IosUnitTests(47009) MallocStackLogging: process 46919 no longer exists, stack logs deleted from /tmp/stack-logs.46919.10d1fc000.IosUnitTests.kFionm.index
```

This reverts commit 02fc8a455d0c4b70a70d36e7f4329f5ad706596d.

## Pre-launch Checklist

- [X] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [X] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [X] I read and followed the [Flutter Style Guide] and the [C++,
Objective-C, Java style guides].
- [X] I listed at least one issue that this PR fixes in the description
above.
- [X] I added new tests to check the change I am making or feature I am
adding, or the PR is [test-exempt]. See [testing the engine] for
instructions on writing and running engine tests.
- [X] I updated/added relevant documentation (doc comments with `///`).
- [X] I signed the [CLA].
- [X] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#overview
[Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene
[test-exempt]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo
[C++, Objective-C, Java style guides]:
https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
[testing the engine]:
https://github.com/flutter/flutter/wiki/Testing-the-engine
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes
[Discord]: https://github.com/flutter/flutter/wiki/Chat
https://dart.googlesource.com/external/github.com/flutter/engine/+/752565630d5b8e51590e437da04279320846d0fc
2 files changed
tree: c694237f67c63e50e6c3daeace1e61f681aea8f3
  1. ci/
  2. tools/
  3. .gitignore
  4. commits.json
  5. DEPS
  6. OWNERS
  7. README.md
README.md

Monorepo

A gclient solution for checking out Dart and Flutter source trees

Monorepo is:

  • Optimized for Tip-of-Tree testing: The Monorepo DEPS used to check out Dart and Flutter dependencies comes from the Flutter engine DEPS with updated dependencies from Dart.

Checking out Monorepo

With depot_tools installed and on your path, create a directory for your monorepo checkout and run these commands to create a gclient solution in that directory:

mkdir monorepo
cd monorepo
gclient config --unmanaged https://dart.googlesource.com/monorepo
gclient sync -D

This gives you a checkout in the monorepo directory that contains:

monorepo/
  DEPS - the DEPS used for this gclient checkout
  commits.json - the pinned commits for Dart, flutter/engine,
                 and flutter/flutter
  tools/ - scripts used to create monorepo DEPS
engine/src/ - the flutter/buildroot repo
    flutter/ - the flutter/engine repo
    out/ - the build directory, where Flutter engine builds are created
    third_party/ - Flutter dependencies checked out by DEPS
      dart/ - the Dart SDK checkout.
        third_party - Dart dependencies, also used by Flutter
flutter/ - the flutter/flutter repo

Building Flutter engine

Flutter's instructions for building the engine are at Compiling the engine

They can be followed closely, with a few changes:

  • Googlers working on Dart do not need to switch to Fuchsia's Goma RBE, except for Windows. The GOMA_DIR enviroment variable can just point to the .cipd_bin directory in a depot_tools installation, and just goma_ctl ensure_start is sufficient.
  • The --no-prebuilt-dart-sdk option has to be added to every gn command, so that the build is set up to build and use a local Dart SDK.
  • The --full-dart-sdk option must be added to gn for the host build target if you will be building web or desktop apps.

Example build commands that work on linux:

MONOREPO_PATH=$PWD
if [[ ! $PATH =~ (^|:)$MONOREPO_PATH/flutter/bin(:|$) ]]; then
  PATH=$MONOREPO_PATH/flutter/bin:$PATH
fi

export GOMA_DIR=$(dirname $(command -v gclient))/.cipd_bin
goma_ctl ensure_start

pushd engine/src
flutter/tools/gn --goma --no-prebuilt-dart-sdk --unoptimized --full-dart-sdk
autoninja -C out/host_debug_unopt
popd

Building Flutter apps

The Flutter commands used to build and run apps will use the locally built Flutter engine and Dart SDK, instead of the one downloaded by the Flutter tool, if the --local-engine option is provided.

For example, to build and run the Flutter spinning square sample on the web platform,

MONOREPO_PATH=$PWD
cd flutter/examples/layers
flutter --local-engine=host_debug_unopt \
  -d chrome run widgets/spinning_square.dart
cd $MONOREPO_PATH

To build for desktop, specify the desktop platform device in flutter run as -d macos or -d linux or -d windows. You may also need to run the command

flutter create --platforms=windows,macos,linux

on existing apps, such as sample apps. New apps created with flutter create already include these support files. Details of desktop support are at Desktop Support for Flutter

Testing

Tests in the Flutter source tree can be run with the flutter test command, run in the directory of a package containing tests. For example:

MONOREPO_PATH=$PWD
cd flutter/packages/flutter
flutter test --local-engine=host_debug_unopt
cd $MONOREPO_PATH

Troubleshooting

Please file an issue or email the dart-engprod team with any problems with or questions about using monorepo.

We will update this documentation to address them.

  • flutter commands may download the engine and Dart SDK files for the configured channel, even though they will be using the local engine and its SDK.

Windows

  • On Windows, gclient sync needs to be run in an administrator session, because some installed dependencies create symlinks.