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

Commit 04bd25b

Browse files
authored
Made sure to call the plugin registrant when registering a background isolate (#36383)
1 parent 9ce10ed commit 04bd25b

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

lib/ui/platform_dispatcher.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ class PlatformDispatcher {
586586
/// [token]. This is required if platform channels are to be used on a
587587
/// background isolate.
588588
void registerBackgroundIsolate(RootIsolateToken token) {
589+
DartPluginRegistrant.ensureInitialized();
589590
__registerBackgroundIsolate(token._token);
590591
}
591592
@FfiNative<Void Function(Int64)>('PlatformConfigurationNativeApi::RegisterBackgroundIsolate')

runtime/dart_plugin_registrant_unittests.cc

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,54 @@ TEST_F(DartIsolateTest, DartPluginRegistrantNotFromBackgroundIsolate) {
195195
"_PluginRegistrant.register() was not called on background isolate");
196196
}
197197

198+
TEST_F(DartIsolateTest, DartPluginRegistrantWhenRegisteringBackgroundIsolate) {
199+
ASSERT_FALSE(DartVMRef::IsInstanceRunning());
200+
201+
std::vector<std::string> messages;
202+
fml::AutoResetWaitableEvent latch;
203+
204+
AddNativeCallback(
205+
"PassMessage",
206+
CREATE_NATIVE_ENTRY(([&latch, &messages](Dart_NativeArguments args) {
207+
auto message = tonic::DartConverter<std::string>::FromDart(
208+
Dart_GetNativeArgument(args, 0));
209+
messages.push_back(message);
210+
latch.Signal();
211+
})));
212+
213+
auto settings = CreateSettingsForFixture();
214+
auto did_throw_exception = false;
215+
settings.unhandled_exception_callback = [&](const std::string& error,
216+
const std::string& stack_trace) {
217+
did_throw_exception = true;
218+
return true;
219+
};
220+
221+
auto vm_ref = DartVMRef::Create(settings);
222+
auto thread = CreateNewThread();
223+
TaskRunners task_runners(GetCurrentTestName(), //
224+
thread, //
225+
thread, //
226+
thread, //
227+
thread //
228+
);
229+
230+
auto kernel_path =
231+
fml::paths::JoinPaths({GetFixturesPath(), kKernelFileName});
232+
auto isolate = RunDartCodeInIsolate(
233+
vm_ref, settings, task_runners,
234+
"registerBackgroundIsolateCallsDartPluginRegistrant", {}, kernel_path);
235+
236+
ASSERT_TRUE(isolate);
237+
ASSERT_EQ(isolate->get()->GetPhase(), DartIsolate::Phase::Running);
238+
239+
latch.Wait();
240+
241+
ASSERT_EQ(messages.size(), 1u);
242+
ASSERT_EQ(messages[0],
243+
"_PluginRegistrant.register() was called on background isolate");
244+
}
245+
198246
} // namespace testing
199247
} // namespace flutter
200248

runtime/fixtures/dart_tool/flutter_build/dart_plugin_registrant.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ void dartPluginRegistrantIsolate(SendPort sendPort) {
4141
sendPort.send(didCallRegistrantBeforeEntrypoint);
4242
}
4343

44+
void registerBackgroundIsolate(List args) {
45+
SendPort sendPort = args[0] as SendPort;
46+
RootIsolateToken token = args[1] as RootIsolateToken;
47+
PlatformDispatcher.instance.registerBackgroundIsolate(token);
48+
sendPort.send(didCallRegistrantBeforeEntrypoint);
49+
}
50+
4451
@pragma('vm:entry-point')
4552
void callDartPluginRegistrantFromBackgroundIsolate() async {
4653
ReceivePort receivePort = ReceivePort();
@@ -70,3 +77,16 @@ void dontCallDartPluginRegistrantFromBackgroundIsolate() async {
7077
}
7178
isolate.kill();
7279
}
80+
81+
@pragma('vm:entry-point')
82+
void registerBackgroundIsolateCallsDartPluginRegistrant() async {
83+
ReceivePort receivePort = ReceivePort();
84+
Isolate isolate = await Isolate.spawn(registerBackgroundIsolate, [receivePort.sendPort, RootIsolateToken.instance]);
85+
bool didCallEntrypoint = await receivePort.first;
86+
if (didCallEntrypoint) {
87+
passMessage('_PluginRegistrant.register() was called on background isolate');
88+
} else {
89+
passMessage('_PluginRegistrant.register() was not called on background isolate');
90+
}
91+
isolate.kill();
92+
}

0 commit comments

Comments
 (0)