[Bug]: Android Bridge.savedCalls / savedPermissionCallIds are HashMaps accessed from several threads without synchronization

オープン
#8,727 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
1〜2日
初心者へのやさしさ
48/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
android, java
領域
mobile

調査の方向性

Start with android/capacitor/src/main/java/com/getcapacitor/Bridge.java and trace the listed access points: saveCall, getSavedCall, releaseCall, getPermissionCall, savePermissionCall, and reset(). Check how the permission callback reaches these methods in Plugin.java, MessageHandler.java, and PluginCall.java; the issue suggests guarding shared state or using a concurrent map while protecting the permission ID lists. Done means both call storage and permission ID access are safe across the named threads.

索引モデルが issue の本文から書いたものです。

説明

triage
Capacitor Version
@capacitor/android: 8.6.0
@capacitor/core: 8.6.0
Also checked: android/capacitor/src/main/java/com/getcapacitor/Bridge.java on main (2026-11-17), same code.
Other API Details
N/A - all versions
Platforms Affected
  • iOS
  • Android
  • Web
Current Behavior

Bridge.savedCalls (Bridge.java:151) and Bridge.savedPermissionCallIds (:154) are plain
HashMaps, and the savedCalls field is neither final nor volatile. They are read and written
from more than one thread with no lock:

Thread Access
CapacitorPlugins handler thread (taskHandler) saveCall after a kept-alive call returns (callPluginMethod, Bridge.java:849-851; e.g. every addListener); savePermissionCall (:990-997, from Plugin.requestPermissionForAliases); getSavedCall + releaseCall in Plugin.removeListener (Plugin.java:748-756)
main thread reset() replaces savedCalls (Bridge.java:576-577, called from BridgeWebViewClient.onPageStarted); the permission result callback Plugin.triggerPermissionCallback (Plugin.java:133-134) runs getPermissionCall (polls the LinkedList in savedPermissionCallIds, then reads savedCalls), and the call it resolves is then removed by PluginCall.release -> Bridge.releaseCall (MessageHandler.java:136-138, PluginCall.java:360-362)
any thread a plugin resolves on releaseCall via MessageHandler.sendResponseMessage for every call that is not kept alive

Most of the off-thread releaseCalls remove a key that was never saved (calls that are not kept
alive are not put in the map), so they only read the map. The pair that writes concurrently is:

  1. The main thread removes a saved permission call after the permission result is delivered
    (HashMap.remove of a present key), while
  2. the CapacitorPlugins thread runs saveCall for another call, e.g. an addListener from the
    page (HashMap.put, possibly resizing).

savedPermissionCallIds has the same shape: put/add on the plugin thread, poll on the main
thread.

The likely effect of the race is a lost entry: a later removeListener then gets null from getSavedCall and leaves the listener call in the plugin's eventListeners, or a released call stays in savedCalls.

Expected Behavior

savedCalls and savedPermissionCallIds are safe to access from the threads that Capacitor
itself uses for them (the plugin handler thread and the main thread), and from the thread a plugin
resolves on.

Possible change:

  • Guard saveCall, getSavedCall, releaseCall, getPermissionCall, savePermissionCall and
    the map replacement in reset() with one lock (e.g. synchronized on a private final object),
    or
  • use ConcurrentHashMap for savedCalls, make the field final and have reset() call
    clear() instead of assigning a new map. ConcurrentHashMap rejects null keys, so
    saveCall / releaseCall would need a null check on callbackId; savedPermissionCallIds
    holds LinkedLists that are mutated in place, so it would still need a lock.
Project Reproduction

N/A - any multi-threaded project on Android could potentially encounter this

Additional Information

On iOS, CapacitorBridge.storedCalls is a ConcurrentDictionary and its reset uses
withLock, so the Android side appears to be the only one affected. The permission result
callback runs on the main thread because AndroidX delivers ActivityResultCallbacks there.

主要言語
TypeScript
スター
17.1k
フォーク
1.3k
平均マージ
1日 22時間
マージ済み PR(30日)
26

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

ionic-team/capacitor のほかの issue

ionic-team/capacitor の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。