apache / cordova-android

Apache Cordova Android
https://cordova.apache.org/
Apache License 2.0
3.65k stars 1.54k forks source link

plugin onload seems to start plugin before app ready #1715

Open globules-io opened 4 months ago

globules-io commented 4 months ago

Bug Report

Problem

We use a firebasex plugin, which has been working fine but lately we have noticed an issue and believe it could be related to plugin initialization.

    <feature name="FirebasePlugin">
     <param name="android-package" value="org.apache.cordova.firebase.FirebasePlugin" />
     <param name="onload" value="true" />
</feature>

What does actually happen?

It appears that if onload is set to true, then the plugin instance is started too early before the javascript interface is available, and any callback to the JS interface will fail.

See https://github.com/dpa99c/cordova-plugin-firebasex/issues/888

Could be related to https://github.com/apache/cordova-android/pull/1605

Environment, Platform, Device

cordova-android 12.0.1 and 13.0.0 cordova 12.0.0 (cordova-lib@12.0.1)

breautek commented 4 months ago

I do not believe it's related to #1605 which simply makes CoreAndroid (a plugin itself) be loaded via "onload", which is the equivalent of it doing <param name="onload" value="true" /> if it actually had it's own plugin.xml.

CoreAndroid isn't responsible for managing plugins or "starting" any plugins. It's purpose is to provide some core JS apis to handle hardware back button presses, among couple of other niche things. In otherwords, #1605 wouldn't have changed plugin loading behaviour of other plugins which uses onload.

With that being said, I'm not so certain this is a bug with cordova-android either. The onload parameter is to initialise the plugins on platform startup which the PluginManager does so here, invoked by CordovaWebViewImpl.init. This initialization chain starts here.

The init makes and configures the webview and initialises all plugins that has the onload parameter set to true. The loading of the webview app happens later, here. Noting that these parts of the codebase hasn't changed for 10+ years, if you use the blame tool to see when the last changes on those lines occurred.

So for about a decade, onload plugins always were initialised before the webview was fully loaded. Attempting to call JS on plugin initialise while the plugin is initialised onload will likely result in a race condition, depending on how the plugin invokes a JS call.

I acknowledge that the documentation does not detail any of this whatsoever. The android plugin docs has 2 sentences noting the existence of the onload parameter, that's about it. Which is why I took the time to dig through the code base to get an understanding on how plugins gets initialised. If anything this highlights an issue with our documentation, but behaviour wise I do not think this is a bug with cordova-android.

globules-io commented 4 months ago

"Attempting to call JS on plugin initialise while the plugin is initialised onload will likely result in a race condition, depending on how the plugin invokes a JS call." I guess that answers the question. It definitely causes a race condition. That being said, what is the proper workflow for a plugin with onload to know when its JS interface is ready?