跪拜 Guibai
← Back to the summary

Flutter Fixes a Sneaky Multi-Engine Bug: FFI/JNI Plugins Now Know Their Engine ID

Flutter's FFI/JNI Plugins Finally Can Distinguish Between Different FlutterEngines

Actually, I think this problem is quite niche; if you don't encounter it, you wouldn't need it, but it's still quite practical. PR #195881: Expose engineId on native plugin bindings and registrars now allows distinguishing FlutterEngine directly via Dart through FFI/JNI.

Because previously it was only possible through MethodChannel, i.e., multi-Engine isolation was essentially natural, but now that we are pushing Native Interop, switching to FFI/JNI has actually removed this support.

In short:

So it is quite necessary to let native plugins also get the corresponding engineId during registration and destruction. This was completely fine before; it's a bug that only appears with FFI.

This problem actually originated from Flutter issue #194920. For example, if your Flutter uses shared_preferences and also integrates WorkManager to execute background tasks, you can run into this problem:

The problem lies in the following execution sequence:

Error typically:

ArgumentError:
No instance with no name has been registered.

This is ridiculous: when a background task ends, it has nothing to do with the main interface, but it unregisters the native API that the main interface is still using. And it doesn't only happen with WorkManager; FCM background Engines, Add-to-App, multi-Engine warm-up, and other scenarios can all encounter similar problems.

Why didn't the old MethodChannel have this problem? The key is BinaryMessenger. Previously, using Pigeon + MethodChannel, the call chain was roughly:

That is, each Engine has its own message channel, so Dart A's requests are naturally handed to pluginA. But after switching to Native Interop, Dart can directly call JNI/FFI:

This loses a crucial piece of information: which FlutterEngine is this call from? The original Kotlin generated code for Pigeon was something like:

val ApiInstances:
    MutableMap<String, ApiRegistrar> = mutableMapOf()

Here the String is the instance name. If two Engines both register the default name, they operate on the same key.

You might think, just assign a different name to each Engine, right? That's a lot of talk, but it can partially work around the issue. The problems are:

So engineId is more appropriate because it's a Flutter Engine concept.

The main changes this time are:

Platform New API Source
Android FlutterPluginBinding.getEngineId() FlutterEngine.getEngineId()
iOS FlutterBaseRegistrar.engineIdentifier FlutterEngine.engineIdentifier
macOS FlutterPluginRegistrar.engineIdentifier FlutterEngine.engineIdentifier

So the problem is simple, but it's actually quite practical, because I have multi-Engine scenarios, and if FFI can load plugins based on this, it gives much more flexibility:

But I really didn't expect that switching from MethodChannel to FFI could have this kind of pitfall awaiting, and Pigeon's bug hasn't been merged yet. You can only say there are always surprises.

Comments

Top 2 from juejin.cn, machine-translated. The original thread is authoritative.

flyfox 1 likes

Boss, any useful tools you've built with Flutter? I recently used AI to write a mini-program: specialises in resolving couple fights and family discord [melon-eating crowd]

栈与烟火

Learning from master Xiao Guo