Skip to content

fix(android): allow synchronous replies on the modern bridge - #8383

Open
richavos wants to merge 1 commit into
ionic-team:mainfrom
richavos:fix-sync-first-plugin-call
Open

richavos wants to merge 1 commit into
ionic-team:mainfrom
richavos:fix-sync-first-plugin-call

Conversation

@richavos

@richavos richavos commented Mar 9, 2026

Copy link
Copy Markdown

Rationale

On Android's modern bridge, MessageHandler stored javaScriptReplyProxy after dispatching the message to postMessage(...).

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 legacy WebView JavaScript 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 TextUtils host-test stub so the real postMessage path 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

  • Fix
  • Feature
  • Refactor
  • Breaking Change
  • Documentation

Platforms Affected

  • Android
  • iOS
  • Web

Closes #8382

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.
cursor Bot pushed a commit to Cap-go/capacitor-plus that referenced this pull request Aug 13, 2026
…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>
@riderx

riderx commented Aug 26, 2026

Copy link
Copy Markdown

This fix is available as a quick patch through @capgo/capacitor-patch.

Patch ID: upstream-pr-8383-android

plugins: {
  CapacitorPatch: {
    patches: ['upstream-pr-8383-android'],
    strict: true,
  },
}

Run npx cap sync after installing @capgo/capacitor-patch.

@robingenz

Copy link
Copy Markdown
Contributor

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 WebView plugin. On every page load it calls Capacitor.Plugins.WebView.getServerBasePath() as its first bridge call, waits up to 10 seconds, and reloads the page with window.location.reload().

Results with Capacitor 8.6.2 on an Android 17 (API 37) emulator with WebView 156:

Bridge Rounds Not settled
Stock 300 89
With the change of this PR 300 0

The README has the steps and a patch file that applies this change to node_modules, so anyone can see the count drop to zero. It would be great to get this merged.

@eric-horodyski

Copy link
Copy Markdown
Member

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?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: first Android modern-bridge call can miss the reply proxy if synchronous

4 participants