Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 00afbc90 authored by Jakub Pawlowski's avatar Jakub Pawlowski
Browse files

Fix unregAll throwing Concurrent exception

unregAll does iterate over ContextMap.mApps without synchronization,
and then call unregisterClient, which also iterate over same variable,
and remove element. This cause ConcurrentModificationException. To fix
that, make sure mApps access is always guarded by synchronized block.
Also make mApps into private variable, to make sure no one would make
similar errors in the future.

Bug: 34805290
Test: manual
Change-Id: I6254c809423ecc3901b64f1a2d60324efd1c4834
(cherry-picked from commit 98ca87dd893a7a3d1545e00a2a9a2419d83e22ef)
parent 7f4a31f4
Loading
Loading
Loading
Loading
+13 −1
Original line number Original line Diff line number Diff line
@@ -133,7 +133,7 @@ import com.android.bluetooth.btservice.BluetoothProto;
    }
    }


    /** Our internal application list */
    /** Our internal application list */
    List<App> mApps = new ArrayList<App>();
    private List<App> mApps = new ArrayList<App>();


    /** Internal map to keep track of logging information by app name */
    /** Internal map to keep track of logging information by app name */
    HashMap<String, AppScanStats> mAppScanStats = new HashMap<String, AppScanStats>();
    HashMap<String, AppScanStats> mAppScanStats = new HashMap<String, AppScanStats>();
@@ -199,6 +199,18 @@ import com.android.bluetooth.btservice.BluetoothProto;
        }
        }
    }
    }


    List<Integer> getAllAppsIds() {
        List<Integer> appIds = new ArrayList();
        synchronized (mApps) {
            Iterator<App> i = mApps.iterator();
            while (i.hasNext()) {
                App entry = i.next();
                appIds.add(entry.id);
            }
        }
        return appIds;
    }

    /**
    /**
     * Add a new connection for a given application ID.
     * Add a new connection for a given application ID.
     */
     */
+3 −3
Original line number Original line Diff line number Diff line
@@ -1482,9 +1482,9 @@ public class GattService extends ProfileService {
    }
    }


    void unregAll() {
    void unregAll() {
        for(ClientMap.App app:mClientMap.mApps){
        for (Integer appId : mClientMap.getAllAppsIds()) {
            if (DBG) Log.d(TAG, "unreg:" + app.id);
            if (DBG) Log.d(TAG, "unreg:" + appId);
            unregisterClient(app.id);
            unregisterClient(appId);
        }
        }
    }
    }