Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve the limitation of FFI-based plugins #123

Closed
swift-kim opened this issue Jun 3, 2021 · 10 comments · Fixed by flutter-tizen/flutter-tizen#516
Closed

Resolve the limitation of FFI-based plugins #123

swift-kim opened this issue Jun 3, 2021 · 10 comments · Fixed by flutter-tizen/flutter-tizen#516
Assignees
Labels
enhancement New feature or request

Comments

@swift-kim
Copy link
Member

FFI-based plugins (path_provider_tizen, shared_preferences_tizen, url_launcher_tizen) currently only work when running in the main isolate and require additional initialization to run in secondary isolates: #122 (review)

@swift-kim swift-kim added bug Something isn't working enhancement New feature or request and removed bug Something isn't working labels Jun 3, 2021
@bbrto21
Copy link
Contributor

bbrto21 commented Jun 30, 2021

to solve this limitation, do we need re-implement plugins based on native?

@swift-kim
Copy link
Member Author

No, we need to check how other platforms tackle this problem but I had not enough time to investigate.

@swift-kim
Copy link
Member Author

  • I tested path_provider_windows on Windows and it worked without problem on secondary isolates. I'm still not exactly sure how the Dart plugin registry for Windows is different from flutter-tizen's.
  • generated_main.dart was not generated by flutter_tools even with Reland the Dart plugin registry flutter/flutter#79669 applied on Windows. I don't know why.
  • Still Reland the Dart plugin registry flutter/flutter#79669 will land to the stable branch soon and it would be better to re-implement flutter-tizen's Dart plugin registry based on the official one. In fact its approach is not quite different from ours, but the main difference is that generateMainDartWithPluginRegistrant (equivalent of _createEntrypoint for Tizen) is part of the build graph.
  • Consider adding implements: attributes to Tizen plugins' pubspecs. It's not quite necessary tho because the attribute doesn't do very much thing.

@swift-kim
Copy link
Member Author

I'll test swift-kim/flutter-tizen@1ed4ee7 when the next stable Flutter version is released.

Related engine PR: flutter/engine#25496

@swift-kim
Copy link
Member Author

The implementation of the third solution in flutter-tizen/flutter-tizen#177 (comment) can be found at: swift-kim/flutter-tizen@6fd8233

The commit should land after the next Flutter stable release.

cc @HakkyuKim

@HakkyuKim
Copy link
Contributor

@swift-kim That was really fast! Thank you!
I'll have a closer look tomorrow.

@swift-kim swift-kim self-assigned this Oct 14, 2021
@swift-kim
Copy link
Member Author

It seems Dart plugin registrants are not yet fully implemented for secondary isolates in the upstream engine: flutter/flutter#81421

Thus the change in #123 (comment) has no effect.

@swift-kim
Copy link
Member Author

I tried invoking InvokeDartPluginRegistrantIfAvailable (Dart_RootLibrary~Dart_Invoke) after initializing secondary isolates but encountered only some segmentation faults. I'm not sure where I can safely put the code (I tried in DartIsolateInitializeCallback and SpawnIsolateTask:Run).

@swift-kim
Copy link
Member Author

My conclusion is that plugins are generally not expected to work properly inside isolates. This applies to other platforms as well, regardless of the underlying implementation mechanism (platform channel or Dart plugin registrant). (There's even a third-party package called flutter_isolate that allows invoking plugins from isolates.)

The only workaround is to call the register function of a plugin (Dart plugin) before invoking its API from an isolate.

Example (path_provider_tizen):

import 'package:flutter/foundation.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path_provider_tizen/path_provider_tizen.dart';

compute((Object? message) async {
  PathProviderPlugin.register();
  await getApplicationDocumentsDirectory();
}, null);

@swift-kim
Copy link
Member Author

Will be fixed by flutter-tizen/flutter-tizen#516.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants