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

Commit 685d74e3 authored by weichinweng's avatar weichinweng
Browse files

Don't synchronize two objects at same time.

Since we will synchronize mConnections and mApps when we need operating
them. On getByConnId and remove(int id) function, they will synchronize
one object then call another function to synchronize another object. On
race conditions, it will deadlock when called getByConnId and remove(int
id) at same time.
This patch doesn't synchronize two objects at same time.

Bug: 185730059
Test: atest BluetoothInstrumentationTests
Tag: #stability
Change-Id: Id29e4f49fe96b40b5b19faf295c3965d299bbeba
parent d63d7638
Loading
Loading
Loading
Loading
+11 −2
Original line number Diff line number Diff line
@@ -222,12 +222,13 @@ import java.util.UUID;
     * Remove the context for a given application ID.
     */
    void remove(int id) {
        boolean find = false;
        synchronized (mApps) {
            Iterator<App> i = mApps.iterator();
            while (i.hasNext()) {
                App entry = i.next();
                if (entry.id == id) {
                    removeConnectionsByAppId(id);
                    find = true;
                    entry.unlinkToDeath();
                    entry.appScanStats.isRegistered = false;
                    i.remove();
@@ -235,6 +236,9 @@ import java.util.UUID;
                }
            }
        }
        if (find) {
            removeConnectionsByAppId(id);
        }
    }

    List<Integer> getAllAppsIds() {
@@ -397,14 +401,19 @@ import java.util.UUID;
     * Get an application context by a connection ID.
     */
    App getByConnId(int connId) {
        int appId = -1;
        synchronized (mConnections) {
            Iterator<Connection> ii = mConnections.iterator();
            while (ii.hasNext()) {
                Connection connection = ii.next();
                if (connection.connId == connId) {
                    return getById(connection.appId);
                    appId = connection.appId;
                    break;
                }
            }
        }
        if (appId >= 0) {
            return getById(appId);
        }
        return null;
    }