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

Commit defa8be

Browse files
authored
Isolates launched by the engine instance use the settings of that instance. (#22052)
This regression was introduced in #21820 for sound-null safety. The settings used to launch the VM were incorrectly used to determine the isolate lifecycle callbacks. Since the first shell/engine in the process also starts the VM, these objects are usually identical. However, for subsequent engine shell/engine launches, the callbacks attached to the new settings object would be ignored. The unit-test harness is also structured in such a way that each test case tears down the VM before the next. So all existing tests created a bespoke VM for the test run, and, the tests that did create multiple isolates did not also test attaching callbacks to the settings object. Fixes #22041
1 parent f459a86 commit defa8be

File tree

5 files changed

+56
-1
lines changed

5 files changed

+56
-1
lines changed

runtime/dart_isolate_unittests.cc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,5 +402,33 @@ TEST_F(DartIsolateTest,
402402
ASSERT_EQ(create_callback_count, 1u);
403403
}
404404

405+
TEST_F(DartIsolateTest,
406+
IsolateCreateCallbacksTakeInstanceSettingsInsteadOfVMSettings) {
407+
ASSERT_FALSE(DartVMRef::IsInstanceRunning());
408+
auto vm_settings = CreateSettingsForFixture();
409+
auto vm_ref = DartVMRef::Create(vm_settings);
410+
auto instance_settings = vm_settings;
411+
size_t create_callback_count = 0u;
412+
instance_settings.root_isolate_create_callback =
413+
[&create_callback_count](const auto& isolate) {
414+
ASSERT_EQ(isolate.GetPhase(), DartIsolate::Phase::Ready);
415+
create_callback_count++;
416+
ASSERT_NE(::Dart_CurrentIsolate(), nullptr);
417+
};
418+
TaskRunners task_runners(GetCurrentTestName(), //
419+
GetCurrentTaskRunner(), //
420+
GetCurrentTaskRunner(), //
421+
GetCurrentTaskRunner(), //
422+
GetCurrentTaskRunner() //
423+
);
424+
{
425+
auto isolate = RunDartCodeInIsolate(vm_ref, instance_settings, task_runners,
426+
"main", {}, GetFixturesPath());
427+
ASSERT_TRUE(isolate);
428+
ASSERT_EQ(isolate->get()->GetPhase(), DartIsolate::Phase::Running);
429+
}
430+
ASSERT_EQ(create_callback_count, 1u);
431+
}
432+
405433
} // namespace testing
406434
} // namespace flutter

runtime/runtime_controller.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ tonic::DartErrorHandleType RuntimeController::GetLastError() {
342342
}
343343

344344
bool RuntimeController::LaunchRootIsolate(
345+
const Settings& settings,
345346
std::optional<std::string> dart_entrypoint,
346347
std::optional<std::string> dart_entrypoint_library,
347348
std::unique_ptr<IsolateConfiguration> isolate_configuration) {
@@ -352,7 +353,7 @@ bool RuntimeController::LaunchRootIsolate(
352353

353354
auto strong_root_isolate =
354355
DartIsolate::CreateRunningRootIsolate(
355-
vm_->GetVMData()->GetSettings(), //
356+
settings, //
356357
isolate_snapshot_, //
357358
task_runners_, //
358359
std::make_unique<PlatformConfiguration>(this), //

runtime/runtime_controller.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ class RuntimeController : public PlatformConfigurationClient {
144144
/// runtime controller, `Clone` this runtime controller and
145145
/// Launch an isolate in that runtime controller instead.
146146
///
147+
/// @param[in] settings The per engine instance settings.
147148
/// @param[in] dart_entrypoint The dart entrypoint. If
148149
/// `std::nullopt` or empty, `main` will
149150
/// be attempted.
@@ -156,6 +157,7 @@ class RuntimeController : public PlatformConfigurationClient {
156157
/// `DartIsolate::Phase::Running` phase.
157158
///
158159
[[nodiscard]] bool LaunchRootIsolate(
160+
const Settings& settings,
159161
std::optional<std::string> dart_entrypoint,
160162
std::optional<std::string> dart_entrypoint_library,
161163
std::unique_ptr<IsolateConfiguration> isolate_configuration);

shell/common/engine.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ Engine::RunStatus Engine::Run(RunConfiguration configuration) {
160160
}
161161

162162
if (!runtime_controller_->LaunchRootIsolate(
163+
settings_, //
163164
configuration.GetEntrypoint(), //
164165
configuration.GetEntrypointLibrary(), //
165166
configuration.TakeIsolateConfiguration()) //

shell/common/shell_unittests.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,5 +2185,28 @@ TEST_F(ShellTest, OnServiceProtocolSetAssetBundlePathWorks) {
21852185
DestroyShell(std::move(shell));
21862186
}
21872187

2188+
TEST_F(ShellTest, EngineRootIsolateLaunchesDontTakeVMDataSettings) {
2189+
ASSERT_FALSE(DartVMRef::IsInstanceRunning());
2190+
// Make sure the shell launch does not kick off the creation of the VM
2191+
// instance by already creating one upfront.
2192+
auto vm_settings = CreateSettingsForFixture();
2193+
auto vm_ref = DartVMRef::Create(vm_settings);
2194+
ASSERT_TRUE(DartVMRef::IsInstanceRunning());
2195+
2196+
auto settings = vm_settings;
2197+
fml::AutoResetWaitableEvent isolate_create_latch;
2198+
settings.root_isolate_create_callback = [&](const auto& isolate) {
2199+
isolate_create_latch.Signal();
2200+
};
2201+
auto shell = CreateShell(settings);
2202+
ASSERT_TRUE(ValidateShell(shell.get()));
2203+
auto configuration = RunConfiguration::InferFromSettings(settings);
2204+
ASSERT_TRUE(configuration.IsValid());
2205+
RunEngine(shell.get(), std::move(configuration));
2206+
ASSERT_TRUE(DartVMRef::IsInstanceRunning());
2207+
DestroyShell(std::move(shell));
2208+
isolate_create_latch.Wait();
2209+
}
2210+
21882211
} // namespace testing
21892212
} // namespace flutter

0 commit comments

Comments
 (0)