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

Commit 7f5e107

Browse files
Revert "Reland "Create root isolate asynchronously (#20142)" (#21747)"
This reverts commit 190fd8e.
1 parent b22809b commit 7f5e107

File tree

8 files changed

+57
-155
lines changed

8 files changed

+57
-155
lines changed

runtime/runtime_controller.cc

Lines changed: 50 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ namespace flutter {
1818

1919
RuntimeController::RuntimeController(RuntimeDelegate& client,
2020
TaskRunners p_task_runners)
21-
: client_(client),
22-
vm_(nullptr),
23-
task_runners_(p_task_runners),
24-
weak_factory_(this) {}
21+
: client_(client), vm_(nullptr), task_runners_(p_task_runners) {}
2522

2623
RuntimeController::RuntimeController(
2724
RuntimeDelegate& p_client,
@@ -55,81 +52,49 @@ RuntimeController::RuntimeController(
5552
platform_data_(std::move(p_platform_data)),
5653
isolate_create_callback_(p_isolate_create_callback),
5754
isolate_shutdown_callback_(p_isolate_shutdown_callback),
58-
persistent_isolate_data_(std::move(p_persistent_isolate_data)),
59-
weak_factory_(this) {
60-
// Create the root isolate as soon as the runtime controller is initialized,
61-
// but not using a synchronous way to avoid blocking the platform thread a
62-
// long time as it is waiting while creating `Shell` on that platform thread.
55+
persistent_isolate_data_(std::move(p_persistent_isolate_data)) {
56+
// Create the root isolate as soon as the runtime controller is initialized.
6357
// It will be run at a later point when the engine provides a run
6458
// configuration and then runs the isolate.
65-
create_and_config_root_isolate_ =
66-
std::async(std::launch::deferred, [self = weak_factory_.GetWeakPtr()]() {
67-
if (!self) {
68-
return;
69-
}
70-
71-
auto strong_root_isolate =
72-
DartIsolate::CreateRootIsolate(
73-
self->vm_->GetVMData()->GetSettings(), //
74-
self->isolate_snapshot_, //
75-
self->task_runners_, //
76-
std::make_unique<PlatformConfiguration>(self.get()), //
77-
self->snapshot_delegate_, //
78-
self->hint_freed_delegate_, //
79-
self->io_manager_, //
80-
self->unref_queue_, //
81-
self->image_decoder_, //
82-
self->advisory_script_uri_, //
83-
self->advisory_script_entrypoint_, //
84-
nullptr, //
85-
self->isolate_create_callback_, //
86-
self->isolate_shutdown_callback_ //
87-
)
88-
.lock();
89-
90-
FML_CHECK(strong_root_isolate) << "Could not create root isolate.";
91-
92-
// The root isolate ivar is weak.
93-
self->root_isolate_ = strong_root_isolate;
94-
95-
strong_root_isolate->SetReturnCodeCallback([self](uint32_t code) {
96-
if (!self) {
97-
return;
98-
}
99-
100-
self->root_isolate_return_code_ = {true, code};
101-
});
102-
103-
if (auto* platform_configuration =
104-
self->GetPlatformConfigurationIfAvailable()) {
105-
tonic::DartState::Scope scope(strong_root_isolate);
106-
platform_configuration->DidCreateIsolate();
107-
if (!self->FlushRuntimeStateToIsolate()) {
108-
FML_DLOG(ERROR) << "Could not setup initial isolate state.";
109-
}
110-
} else {
111-
FML_DCHECK(false)
112-
<< "RuntimeController created without window binding.";
113-
}
114-
115-
FML_DCHECK(Dart_CurrentIsolate() == nullptr);
116-
117-
self->client_.OnRootIsolateCreated();
118-
return;
119-
});
120-
121-
// We're still trying to create the root isolate as soon as possible here on
122-
// the UI thread although it's deferred a little bit by
123-
// std::async(std::launch::deferred, ...). So the callers of `GetRootIsolate`
124-
// should get a quick return after this UI thread task.
125-
task_runners_.GetUITaskRunner()->PostTask(
126-
[self = weak_factory_.GetWeakPtr()]() {
127-
if (!self) {
128-
return;
129-
}
130-
131-
self->GetRootIsolate();
132-
});
59+
auto strong_root_isolate =
60+
DartIsolate::CreateRootIsolate(
61+
vm_->GetVMData()->GetSettings(), //
62+
isolate_snapshot_, //
63+
task_runners_, //
64+
std::make_unique<PlatformConfiguration>(this), //
65+
snapshot_delegate_, //
66+
hint_freed_delegate_, //
67+
io_manager_, //
68+
unref_queue_, //
69+
image_decoder_, //
70+
p_advisory_script_uri, //
71+
p_advisory_script_entrypoint, //
72+
nullptr, //
73+
isolate_create_callback_, //
74+
isolate_shutdown_callback_ //
75+
)
76+
.lock();
77+
78+
FML_CHECK(strong_root_isolate) << "Could not create root isolate.";
79+
80+
// The root isolate ivar is weak.
81+
root_isolate_ = strong_root_isolate;
82+
83+
strong_root_isolate->SetReturnCodeCallback([this](uint32_t code) {
84+
root_isolate_return_code_ = {true, code};
85+
});
86+
87+
if (auto* platform_configuration = GetPlatformConfigurationIfAvailable()) {
88+
tonic::DartState::Scope scope(strong_root_isolate);
89+
platform_configuration->DidCreateIsolate();
90+
if (!FlushRuntimeStateToIsolate()) {
91+
FML_DLOG(ERROR) << "Could not setup initial isolate state.";
92+
}
93+
} else {
94+
FML_DCHECK(false) << "RuntimeController created without window binding.";
95+
}
96+
97+
FML_DCHECK(Dart_CurrentIsolate() == nullptr);
13398
}
13499

135100
RuntimeController::~RuntimeController() {
@@ -145,8 +110,8 @@ RuntimeController::~RuntimeController() {
145110
}
146111
}
147112

148-
bool RuntimeController::IsRootIsolateRunning() {
149-
std::shared_ptr<DartIsolate> root_isolate = GetRootIsolate().lock();
113+
bool RuntimeController::IsRootIsolateRunning() const {
114+
std::shared_ptr<DartIsolate> root_isolate = root_isolate_.lock();
150115
if (root_isolate) {
151116
return root_isolate->GetPhase() == DartIsolate::Phase::Running;
152117
}
@@ -273,7 +238,7 @@ bool RuntimeController::ReportTimings(std::vector<int64_t> timings) {
273238
}
274239

275240
bool RuntimeController::NotifyIdle(int64_t deadline, size_t freed_hint) {
276-
std::shared_ptr<DartIsolate> root_isolate = GetRootIsolate().lock();
241+
std::shared_ptr<DartIsolate> root_isolate = root_isolate_.lock();
277242
if (!root_isolate) {
278243
return false;
279244
}
@@ -333,7 +298,7 @@ bool RuntimeController::DispatchSemanticsAction(int32_t id,
333298

334299
PlatformConfiguration*
335300
RuntimeController::GetPlatformConfigurationIfAvailable() {
336-
std::shared_ptr<DartIsolate> root_isolate = GetRootIsolate().lock();
301+
std::shared_ptr<DartIsolate> root_isolate = root_isolate_.lock();
337302
return root_isolate ? root_isolate->platform_configuration() : nullptr;
338303
}
339304

@@ -395,17 +360,17 @@ RuntimeController::ComputePlatformResolvedLocale(
395360
}
396361

397362
Dart_Port RuntimeController::GetMainPort() {
398-
std::shared_ptr<DartIsolate> root_isolate = GetRootIsolate().lock();
363+
std::shared_ptr<DartIsolate> root_isolate = root_isolate_.lock();
399364
return root_isolate ? root_isolate->main_port() : ILLEGAL_PORT;
400365
}
401366

402367
std::string RuntimeController::GetIsolateName() {
403-
std::shared_ptr<DartIsolate> root_isolate = GetRootIsolate().lock();
368+
std::shared_ptr<DartIsolate> root_isolate = root_isolate_.lock();
404369
return root_isolate ? root_isolate->debug_name() : "";
405370
}
406371

407372
bool RuntimeController::HasLivePorts() {
408-
std::shared_ptr<DartIsolate> root_isolate = GetRootIsolate().lock();
373+
std::shared_ptr<DartIsolate> root_isolate = root_isolate_.lock();
409374
if (!root_isolate) {
410375
return false;
411376
}
@@ -414,20 +379,11 @@ bool RuntimeController::HasLivePorts() {
414379
}
415380

416381
tonic::DartErrorHandleType RuntimeController::GetLastError() {
417-
std::shared_ptr<DartIsolate> root_isolate = GetRootIsolate().lock();
382+
std::shared_ptr<DartIsolate> root_isolate = root_isolate_.lock();
418383
return root_isolate ? root_isolate->GetLastError() : tonic::kNoError;
419384
}
420385

421386
std::weak_ptr<DartIsolate> RuntimeController::GetRootIsolate() {
422-
std::shared_ptr<DartIsolate> root_isolate = root_isolate_.lock();
423-
if (root_isolate) {
424-
return root_isolate_;
425-
}
426-
427-
// Root isolate is not yet created, get it and do some configuration.
428-
FML_DCHECK(create_and_config_root_isolate_.valid());
429-
create_and_config_root_isolate_.get();
430-
431387
return root_isolate_;
432388
}
433389

runtime/runtime_controller.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#ifndef FLUTTER_RUNTIME_RUNTIME_CONTROLLER_H_
66
#define FLUTTER_RUNTIME_RUNTIME_CONTROLLER_H_
77

8-
#include <future>
98
#include <memory>
109
#include <vector>
1110

@@ -349,7 +348,7 @@ class RuntimeController : public PlatformConfigurationClient {
349348
///
350349
/// @return True if root isolate running, False otherwise.
351350
///
352-
virtual bool IsRootIsolateRunning();
351+
virtual bool IsRootIsolateRunning() const;
353352

354353
//----------------------------------------------------------------------------
355354
/// @brief Dispatch the specified platform message to running root
@@ -431,10 +430,7 @@ class RuntimeController : public PlatformConfigurationClient {
431430
/// @brief Get a weak pointer to the root Dart isolate. This isolate may
432431
/// only be locked on the UI task runner. Callers use this
433432
/// accessor to transition to the root isolate to the running
434-
/// phase. Note that it might take times if the isolate is not yet
435-
/// created, which should be done in a subsequence task after
436-
/// constructing `RuntimeController`, or it should get a quick
437-
/// return otherwise.
433+
/// phase.
438434
///
439435
/// @return The root isolate reference.
440436
///
@@ -484,16 +480,11 @@ class RuntimeController : public PlatformConfigurationClient {
484480
std::string advisory_script_entrypoint_;
485481
std::function<void(int64_t)> idle_notification_callback_;
486482
PlatformData platform_data_;
487-
std::future<void> create_and_config_root_isolate_;
488-
// Note that `root_isolate_` is created asynchronously from the constructor of
489-
// `RuntimeController`, be careful to use it directly while it might have not
490-
// been created yet. Call `GetRootIsolate()` instead which guarantees that.
491483
std::weak_ptr<DartIsolate> root_isolate_;
492484
std::pair<bool, uint32_t> root_isolate_return_code_ = {false, 0};
493485
const fml::closure isolate_create_callback_;
494486
const fml::closure isolate_shutdown_callback_;
495487
std::shared_ptr<const fml::Mapping> persistent_isolate_data_;
496-
fml::WeakPtrFactory<RuntimeController> weak_factory_;
497488

498489
PlatformConfiguration* GetPlatformConfigurationIfAvailable();
499490

runtime/runtime_delegate.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ class RuntimeDelegate {
3232

3333
virtual FontCollection& GetFontCollection() = 0;
3434

35-
virtual void OnRootIsolateCreated() = 0;
36-
3735
virtual void UpdateIsolateDescription(const std::string isolate_name,
3836
int64_t isolate_port) = 0;
3937

shell/common/engine.cc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -501,10 +501,6 @@ void Engine::HandlePlatformMessage(fml::RefPtr<PlatformMessage> message) {
501501
}
502502
}
503503

504-
void Engine::OnRootIsolateCreated() {
505-
delegate_.OnRootIsolateCreated();
506-
}
507-
508504
void Engine::UpdateIsolateDescription(const std::string isolate_name,
509505
int64_t isolate_port) {
510506
delegate_.UpdateIsolateDescription(isolate_name, isolate_port);

shell/common/engine.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,6 @@ class Engine final : public RuntimeDelegate,
189189
///
190190
virtual void OnPreEngineRestart() = 0;
191191

192-
//--------------------------------------------------------------------------
193-
/// @brief Notifies the shell that the root isolate is created.
194-
/// Currently, this information is to add to the service
195-
/// protocol list of available root isolates running in the VM
196-
/// and their names so that the appropriate isolate can be
197-
/// selected in the tools for debugging and instrumentation.
198-
///
199-
virtual void OnRootIsolateCreated() = 0;
200-
201192
//--------------------------------------------------------------------------
202193
/// @brief Notifies the shell of the name of the root isolate and its
203194
/// port when that isolate is launched, restarted (in the
@@ -808,9 +799,6 @@ class Engine final : public RuntimeDelegate,
808799
// |RuntimeDelegate|
809800
void HandlePlatformMessage(fml::RefPtr<PlatformMessage> message) override;
810801

811-
// |RuntimeDelegate|
812-
void OnRootIsolateCreated() override;
813-
814802
// |RuntimeDelegate|
815803
void UpdateIsolateDescription(const std::string isolate_name,
816804
int64_t isolate_port) override;

shell/common/engine_unittests.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class MockDelegate : public Engine::Delegate {
2727
MOCK_METHOD1(OnEngineHandlePlatformMessage,
2828
void(fml::RefPtr<PlatformMessage>));
2929
MOCK_METHOD0(OnPreEngineRestart, void());
30-
MOCK_METHOD0(OnRootIsolateCreated, void());
3130
MOCK_METHOD2(UpdateIsolateDescription, void(const std::string, int64_t));
3231
MOCK_METHOD1(SetNeedsReportTimings, void(bool));
3332
MOCK_METHOD1(ComputePlatformResolvedLocale,
@@ -50,7 +49,6 @@ class MockRuntimeDelegate : public RuntimeDelegate {
5049
void(SemanticsNodeUpdates, CustomAccessibilityActionUpdates));
5150
MOCK_METHOD1(HandlePlatformMessage, void(fml::RefPtr<PlatformMessage>));
5251
MOCK_METHOD0(GetFontCollection, FontCollection&());
53-
MOCK_METHOD0(OnRootIsolateCreated, void());
5452
MOCK_METHOD2(UpdateIsolateDescription, void(const std::string, int64_t));
5553
MOCK_METHOD1(SetNeedsReportTimings, void(bool));
5654
MOCK_METHOD1(ComputePlatformResolvedLocale,
@@ -62,7 +60,7 @@ class MockRuntimeController : public RuntimeController {
6260
public:
6361
MockRuntimeController(RuntimeDelegate& client, TaskRunners p_task_runners)
6462
: RuntimeController(client, p_task_runners) {}
65-
MOCK_METHOD0(IsRootIsolateRunning, bool());
63+
MOCK_CONST_METHOD0(IsRootIsolateRunning, bool());
6664
MOCK_METHOD1(DispatchPlatformMessage, bool(fml::RefPtr<PlatformMessage>));
6765
};
6866

shell/common/shell.cc

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,8 @@ bool Shell::Setup(std::unique_ptr<PlatformView> platform_view,
564564

565565
is_setup_ = true;
566566

567+
vm_->GetServiceProtocol()->AddHandler(this, GetServiceProtocolDescription());
568+
567569
PersistentCache::GetCacheForProcess()->AddWorkerTaskRunner(
568570
task_runners_.GetIOTaskRunner());
569571

@@ -1148,23 +1150,6 @@ void Shell::OnPreEngineRestart() {
11481150
latch.Wait();
11491151
}
11501152

1151-
// |Engine::Delegate|
1152-
void Shell::OnRootIsolateCreated() {
1153-
if (is_added_to_service_protocol_) {
1154-
return;
1155-
}
1156-
auto description = GetServiceProtocolDescription();
1157-
fml::TaskRunner::RunNowOrPostTask(
1158-
task_runners_.GetPlatformTaskRunner(),
1159-
[self = weak_factory_.GetWeakPtr(),
1160-
description = std::move(description)]() {
1161-
if (self) {
1162-
self->vm_->GetServiceProtocol()->AddHandler(self.get(), description);
1163-
}
1164-
});
1165-
is_added_to_service_protocol_ = true;
1166-
}
1167-
11681153
// |Engine::Delegate|
11691154
void Shell::UpdateIsolateDescription(const std::string isolate_name,
11701155
int64_t isolate_port) {
@@ -1310,15 +1295,9 @@ bool Shell::HandleServiceProtocolMessage(
13101295
// |ServiceProtocol::Handler|
13111296
ServiceProtocol::Handler::Description Shell::GetServiceProtocolDescription()
13121297
const {
1313-
FML_DCHECK(task_runners_.GetUITaskRunner()->RunsTasksOnCurrentThread());
1314-
1315-
if (!weak_engine_) {
1316-
return ServiceProtocol::Handler::Description();
1317-
}
1318-
13191298
return {
1320-
weak_engine_->GetUIIsolateMainPort(),
1321-
weak_engine_->GetUIIsolateName(),
1299+
engine_->GetUIIsolateMainPort(),
1300+
engine_->GetUIIsolateName(),
13221301
};
13231302
}
13241303

shell/common/shell.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,6 @@ class Shell final : public PlatformView::Delegate,
411411
>
412412
service_protocol_handlers_;
413413
bool is_setup_ = false;
414-
bool is_added_to_service_protocol_ = false;
415414
uint64_t next_pointer_flow_id_ = 0;
416415

417416
bool first_frame_rasterized_ = false;
@@ -538,9 +537,6 @@ class Shell final : public PlatformView::Delegate,
538537
// |Engine::Delegate|
539538
void OnPreEngineRestart() override;
540539

541-
// |Engine::Delegate|
542-
void OnRootIsolateCreated() override;
543-
544540
// |Engine::Delegate|
545541
void UpdateIsolateDescription(const std::string isolate_name,
546542
int64_t isolate_port) override;

0 commit comments

Comments
 (0)