Repository navigation
Conversation
Store the JavaScript reply proxy before dispatching a modern bridge message so a plugin that resolves synchronously can reply through the modern bridge instead of falling back to WebView JavaScript execution. Add a regression test for the first modern-bridge call and a minimal TextUtils host-test stub so the real postMessage path can run in unit tests.
…nous replies on the modern bridge (#93) * fix(android): allow synchronous replies on the modern bridge Store the JavaScript reply proxy before dispatching a modern bridge message so a plugin that resolves synchronously can reply through the modern bridge instead of falling back to WebView JavaScript execution. Add a regression test for the first modern-bridge call and a minimal TextUtils host-test stub so the real postMessage path can run in unit tests. * style: apply repository formatter Co-authored-by: Martin DONADIEU <martindonadieu@gmail.com> --------- Co-authored-by: Rich Lawrence <rich.lawrence@avos.io> Co-authored-by: Capacitor+ Bot <bot@capgo.app> Co-authored-by: Cursor Agent <cursoragent@cursor.com>
|
This fix is available as a quick patch through Patch ID: plugins: {
CapacitorPatch: {
patches: ['upstream-pr-8383-android'],
strict: true,
},
}Run |
|
Thanks for this fix! I can confirm that the race is real, and I built a small reproduction for it: https://github.com/robingenz/capacitor-android-first-call-repro The race does not only hit the first call of a cold start. It also hits the first plugin call after a page reload. The reply of the plugin is then sent through the reply channel of the old page, and the promise in the new page never settles. The app installs no plugin packages. It only uses the core Results with Capacitor 8.6.2 on an Android 17 (API 37) emulator with WebView 156:
The README has the steps and a patch file that applies this change to |
|
Hi @richavos, thanks for putting this together! We're currently looking into this issue and resolution. One question from our end; do you observe the same behavior occurring on the iOS side? |
Rationale
On Android's modern bridge,
MessageHandlerstoredjavaScriptReplyProxyafter dispatching the message topostMessage(...).That meant a plugin that resolved synchronously during the first modern-bridge call could reach
sendResponseMessage()before the reply proxy was available, causing Capacitor to fall back to the legacyWebViewJavaScript response path.This is unreliable as a fallback, so can result in promises never being settled for plugin calls.
Changes
This change stores the reply proxy before dispatching the message, so that all plugin calls should be settlable, and adds a regression test for this scenario.
Testing
To provide a minimal test, I've added a small
TextUtilshost-test stub so the realpostMessagepath can be run in the test.I've also added two reflection-based methods that attempt to surgically insert only what is required to allow the "normal" bridge call codepath to be run.
The tradeoff here is concision against brittleness - let me know/feel free to adjust the test to fit with the project as a whole.
Change Type
Platforms Affected
Closes #8382