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

Commit 77535ce2 authored by jackqdyulei's avatar jackqdyulei
Browse files

Fix deadlock in LocalBluetoothManager

Following deadlock exist in previous code:
1. Main thread
Device.refresh()->Device.dispatchCallback()->Lock callback->(handle
callback in systemui)->DeviceManager.getDevicesCopy()-> try lock
deviceManager
2. Bg thread
DeviceManager.onUuidChange()->Lock
deviceManager->Device.refresh()->Device.dispatchCallback()-> try lock
callback

This CL remove sychnorized method in DeviceManager to unblock this
deadlock. Since UuidChange can only happen in bg thread, we don't need
this extra lock in DeviceManager.

Fixes: 112483982
Test: Manual
Change-Id: I1a8bb130cfff40358783f5f5da7e50e5f0a42e20
parent 989bb371
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
@@ -420,14 +420,20 @@ public class BluetoothEventManager {
    private class ClassChangedHandler implements Handler {
        public void onReceive(Context context, Intent intent,
                BluetoothDevice device) {
            mDeviceManager.onBtClassChanged(device);
            CachedBluetoothDevice cachedDevice = mDeviceManager.findDevice(device);
            if (cachedDevice != null) {
                cachedDevice.refresh();
            }
        }
    }

    private class UuidChangedHandler implements Handler {
        public void onReceive(Context context, Intent intent,
                BluetoothDevice device) {
            mDeviceManager.onUuidChanged(device);
            CachedBluetoothDevice cachedDevice = mDeviceManager.findDevice(device);
            if (cachedDevice != null) {
                cachedDevice.onUuidChanged();
            }
        }
    }

+0 −14
Original line number Diff line number Diff line
@@ -206,20 +206,6 @@ public class CachedBluetoothDeviceManager {
        }
    }

    public synchronized void onBtClassChanged(BluetoothDevice device) {
        CachedBluetoothDevice cachedDevice = findDevice(device);
        if (cachedDevice != null) {
            cachedDevice.dispatchAttributesChanged();
        }
    }

    public synchronized void onUuidChanged(BluetoothDevice device) {
        CachedBluetoothDevice cachedDevice = findDevice(device);
        if (cachedDevice != null) {
            cachedDevice.onUuidChanged();
        }
    }

    public synchronized void onBluetoothStateChanged(int bluetoothState) {
        // When Bluetooth is turning off, we need to clear the non-bonded devices
        // Otherwise, they end up showing up on the next BT enable
+0 −15
Original line number Diff line number Diff line
@@ -368,21 +368,6 @@ public class CachedBluetoothDeviceManagerTest {
        verify(mHearingAidDeviceManager).updateHearingAidsDevices();
    }

    /**
     * Test to verify onBtClassChanged().
     */
    @Test
    public void onBtClassChanged_validBtClass_classChanged() {
        CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1);
        assertThat(cachedDevice1).isNotNull();
        assertThat(cachedDevice1.getBtClass()).isEqualTo(DEVICE_CLASS_1);

        final BluetoothClass newBluetoothClass = DEVICE_CLASS_2;
        when(mDevice1.getBluetoothClass()).thenReturn(newBluetoothClass);
        mCachedDeviceManager.onBtClassChanged(mDevice1);
        assertThat(cachedDevice1.getBtClass()).isEqualTo(newBluetoothClass);
    }

    /**
     * Test to verify onDeviceDisappeared().
     */