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

Aperta
#8,727 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
1-2 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
android, java
Ambito
mobile

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
TypeScript
Stelle
17.1k
Fork
1.3k
Merge medio
1g 22h
PR unite (30g)
26

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di ionic-team/capacitor

Tutte le issue di ionic-team/capacitor

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.