[Bug]: Plugin call after navigation can be answered before reply proxy is updated
Capacitor Version
@capacitor/cli: 8.4.0 @capacitor/core: 8.4.0 @capacitor/android: 8.4.0
Other API Details
npm: 11.0.0
node: 22.13.0Platforms Affected
- iOS
- Android
- Web
Current Behavior
After a reload or navigation, a fast plugin call (e.g. a Preferences read, which is how the bug appeared for us) can potentially never resolve as it uses the previous proxy instead of the updated one. The cause are these lines in MessageHandler.java:
if (isMainFrame) {
postMessage(message.getData());
javaScriptReplyProxy = replyProxy;
}In case of a fast plugin call, sendResponseMessage() is reached before the proxy is updated, and therefore still uses the previous proxy and is silently dropped. Later calls work, and there's also no issue upon first page load as, at this point, javaScriptReplyProxy is null and the old evaluateJavascript path is used instead.
Expected Behavior
All plugin calls of the new page should go through the updated proxy.
Project Reproduction
Reproduced in a private repo
Additional Information
The bug is avoidable with android.useLegacyBridge = true as this routes every response through evaluateJavascriptand avoids the proxy entirely.
I've locally fixed the bug by swapping the two lines above, i.e., making sure the proxy is updated before the message post.
Source: ionic-team/capacitor