Add missing "try ... finally" safeguards

Safeguards for code protected by ReentrantReadWriteLock.

Bug: 28734075
Bug: 28799467
Change-Id: Ib7f598a92e8df6bd855ca48cdd094c1c73a935f2
(cherry picked from commit e957a8a0b4100d001f79c866e7904d2426ac8da0)
diff --git a/core/java/android/bluetooth/BluetoothAdapter.java b/core/java/android/bluetooth/BluetoothAdapter.java
index 15b16f7..36e041a 100644
--- a/core/java/android/bluetooth/BluetoothAdapter.java
+++ b/core/java/android/bluetooth/BluetoothAdapter.java
@@ -2029,12 +2029,15 @@
             public void onBluetoothServiceDown() {
                 if (VDBG) Log.d(TAG, "onBluetoothServiceDown: " + mService);
 
-                mServiceLock.writeLock().lock();
-                mService = null;
-                if (mLeScanClients != null) mLeScanClients.clear();
-                if (sBluetoothLeAdvertiser != null) sBluetoothLeAdvertiser.cleanup();
-                if (sBluetoothLeScanner != null) sBluetoothLeScanner.cleanup();
-                mServiceLock.writeLock().unlock();
+                try {
+                    mServiceLock.writeLock().lock();
+                    mService = null;
+                    if (mLeScanClients != null) mLeScanClients.clear();
+                    if (sBluetoothLeAdvertiser != null) sBluetoothLeAdvertiser.cleanup();
+                    if (sBluetoothLeScanner != null) sBluetoothLeScanner.cleanup();
+                } finally {
+                    mServiceLock.writeLock().unlock();
+                }
 
                 synchronized (mProxyServiceStateCallbacks) {
                     for (IBluetoothManagerCallback cb : mProxyServiceStateCallbacks ){
diff --git a/services/core/java/com/android/server/BluetoothManagerService.java b/services/core/java/com/android/server/BluetoothManagerService.java
index e97f6a3..6000568 100644
--- a/services/core/java/com/android/server/BluetoothManagerService.java
+++ b/services/core/java/com/android/server/BluetoothManagerService.java
@@ -1539,14 +1539,17 @@
                                                     BluetoothAdapter.STATE_OFF);
                         sendBluetoothServiceDownCallback();
 
-                        mBluetoothLock.writeLock().lock();
-                        if (mBluetooth != null) {
-                            mBluetooth = null;
-                            // Unbind
-                            mContext.unbindService(mConnection);
+                        try {
+                            mBluetoothLock.writeLock().lock();
+                            if (mBluetooth != null) {
+                                mBluetooth = null;
+                                // Unbind
+                                mContext.unbindService(mConnection);
+                            }
+                            mBluetoothGatt = null;
+                        } finally {
+                            mBluetoothLock.writeLock().unlock();
                         }
-                        mBluetoothGatt = null;
-                        mBluetoothLock.writeLock().unlock();
 
                         SystemClock.sleep(100);
 
@@ -1851,14 +1854,17 @@
 
         sendBluetoothServiceDownCallback();
 
-        mBluetoothLock.writeLock().lock();
-        if (mBluetooth != null) {
-            mBluetooth = null;
-            // Unbind
-            mContext.unbindService(mConnection);
+        try {
+            mBluetoothLock.writeLock().lock();
+            if (mBluetooth != null) {
+                mBluetooth = null;
+                // Unbind
+                mContext.unbindService(mConnection);
+            }
+            mBluetoothGatt = null;
+        } finally {
+            mBluetoothLock.writeLock().unlock();
         }
-        mBluetoothGatt = null;
-        mBluetoothLock.writeLock().unlock();
 
         mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
         mState = BluetoothAdapter.STATE_OFF;