Allow embedders to control Dart VM lifecycle on engine shutdown. (flutter/engine#10652)
This exposes the `Settings::leak_vm` flag to custom embedders. All embedder unit-tests now shut down the VM on the shutdown of the last engine in the process. The mechanics of VM shutdown are already tested in the Shell unit-tests harness in the DartLifecycleUnittests set of of assertions. This just exposes that functionality to custom embedders. Since it is part of the public stable API, I also switched the name of the field to be something less snarky than the field in private shell settings.
This commit is contained in:
@@ -375,6 +375,7 @@ FlutterEngineResult FlutterEngineRun(size_t version,
|
||||
|
||||
settings.icu_data_path = icu_data_path;
|
||||
settings.assets_path = args->assets_path;
|
||||
settings.leak_vm = !SAFE_ACCESS(args, shutdown_dart_vm_when_done, false);
|
||||
|
||||
if (!flutter::DartVM::IsRunningPrecompiledCode()) {
|
||||
// Verify the assets path contains Dart 2 kernel assets.
|
||||
|
||||
@@ -695,6 +695,25 @@ typedef struct {
|
||||
// optional argument allows for the specification of task runner interfaces to
|
||||
// event loops managed by the embedder on threads it creates.
|
||||
const FlutterCustomTaskRunners* custom_task_runners;
|
||||
|
||||
// All `FlutterEngine` instances in the process share the same Dart VM. When
|
||||
// the first engine is launched, it starts the Dart VM as well. It used to be
|
||||
// the case that it was not possible to shutdown the Dart VM cleanly and start
|
||||
// it back up in the process in a safe manner. This issue has since been
|
||||
// patched. Unfortunately, applications already began to make use of the fact
|
||||
// that shutting down the Flutter engine instance left a running VM in the
|
||||
// process. Since a Flutter engine could be launched on any thread,
|
||||
// applications would "warm up" the VM on another thread by launching
|
||||
// an engine with no isolates and then shutting it down immediately. The main
|
||||
// Flutter application could then be started on the main thread without having
|
||||
// to incur the Dart VM startup costs at that time. With the new behavior,
|
||||
// this "optimization" immediately becomes massive performance pessimization
|
||||
// as the VM would be started up in the "warm up" phase, shut down there and
|
||||
// then started again on the main thread. Changing this behavior was deemed to
|
||||
// be an unacceptable breaking change. Embedders that wish to shutdown the
|
||||
// Dart VM when the last engine is terminated in the process should opt into
|
||||
// this behavior by setting this flag to true.
|
||||
bool shutdown_dart_vm_when_done;
|
||||
} FlutterProjectArgs;
|
||||
|
||||
FLUTTER_EXPORT
|
||||
|
||||
@@ -12,6 +12,7 @@ EmbedderConfigBuilder::EmbedderConfigBuilder(
|
||||
InitializationPreference preference)
|
||||
: context_(context) {
|
||||
project_args_.struct_size = sizeof(project_args_);
|
||||
project_args_.shutdown_dart_vm_when_done = true;
|
||||
project_args_.platform_message_callback =
|
||||
[](const FlutterPlatformMessage* message, void* context) {
|
||||
reinterpret_cast<EmbedderContext*>(context)->PlatformMessageCallback(
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include "flutter/fml/message_loop.h"
|
||||
#include "flutter/fml/synchronization/waitable_event.h"
|
||||
#include "flutter/fml/thread.h"
|
||||
#include "flutter/runtime/dart_vm.h"
|
||||
#include "flutter/shell/platform/embedder/tests/embedder_config_builder.h"
|
||||
#include "flutter/shell/platform/embedder/tests/embedder_test.h"
|
||||
#include "flutter/testing/testing.h"
|
||||
@@ -465,5 +466,26 @@ TEST_F(EmbedderTest, InvalidPlatformMessages) {
|
||||
ASSERT_EQ(result, kInvalidArguments);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/// Asserts behavior of FlutterProjectArgs::shutdown_dart_vm_when_done (which is
|
||||
/// set to true by default in these unit-tests).
|
||||
///
|
||||
TEST_F(EmbedderTest, VMShutsDownWhenNoEnginesInProcess) {
|
||||
auto& context = GetEmbedderContext();
|
||||
EmbedderConfigBuilder builder(context);
|
||||
|
||||
const auto launch_count = DartVM::GetVMLaunchCount();
|
||||
|
||||
{
|
||||
auto engine = builder.LaunchEngine();
|
||||
ASSERT_EQ(launch_count + 1u, DartVM::GetVMLaunchCount());
|
||||
}
|
||||
|
||||
{
|
||||
auto engine = builder.LaunchEngine();
|
||||
ASSERT_EQ(launch_count + 2u, DartVM::GetVMLaunchCount());
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
} // namespace flutter
|
||||
|
||||
Reference in New Issue
Block a user